Skip to content

Commit 3956670

Browse files
global validation
1 parent a883494 commit 3956670

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

src/json-ui-element.ts

+29-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import { get, isEmpty, isNull, isUndefined, set } from "lodash";
1111
import { inferDescription, inferTitle, inferType } from "./parser/infer";
1212
import { ROOT_PATH, DEFAULT_VALUES, PATH_SEPARATOR } from "./constants";
1313
import { ChangeEventDetails } from "./utils/dispatch-change";
14-
import { humanizeKey } from "./utils/humanize";
14+
import { humanizeKey, humanizeValue } from "./utils/humanize";
15+
import { ajvFactory } from "./parser/ajv";
16+
import { ValidateFunction } from "ajv";
1517

1618
@customElement("json-ui-element")
1719
export class JsonUiElement extends LitElement {
@@ -36,6 +38,9 @@ export class JsonUiElement extends LitElement {
3638
JSONSchema7
3739
>;
3840

41+
@state()
42+
ajvValidateFn?: ValidateFunction;
43+
3944
@state()
4045
oneOfIndex = 0;
4146

@@ -54,11 +59,12 @@ export class JsonUiElement extends LitElement {
5459
): void {
5560
// Always resolve the value because nested values may have changed.
5661
this.resolvedValue = this.path ? get(this.value, this.path) : this.value;
57-
console.log('resolvedValue', this.resolvedValue, this.value, this.path)
5862
this.dispatchEvent(new CustomEvent("change", { detail: this.value }));
5963

6064
this.resolvedSchemas ??= {} as any;
6165

66+
if (this.schema && this.ajvValidateFn) this.ajvValidateFn!(this.value);
67+
6268
// Determine the depth of changes to optimize the schema resolution process.
6369
// Note: oneOf and anyOf are currently only supported at the top level!
6470
const updateLevel = [
@@ -80,6 +86,8 @@ export class JsonUiElement extends LitElement {
8086
);
8187

8288
if (updateLevel === 0) {
89+
this.ajvValidateFn = ajvFactory().compile(this.schema);
90+
this.ajvValidateFn!(this.value);
8391
this.resolvedSchemas.resolvedRefs = resolveRefs(this.schema);
8492
this.resolvedSchemas.resolvedAllOf = allOf(
8593
this.resolvedSchemas.resolvedRefs
@@ -105,13 +113,6 @@ export class JsonUiElement extends LitElement {
105113
this.resolvedSchemas.navigated,
106114
this.resolvedValue
107115
);
108-
109-
console.log(
110-
"oneOfIndex inferred",
111-
this.oneOfIndex,
112-
this.resolvedSchemas.navigated,
113-
this.resolvedValue
114-
);
115116
}
116117

117118
if (updateLevel <= 2) {
@@ -156,7 +157,7 @@ export class JsonUiElement extends LitElement {
156157

157158
return html`
158159
<div class="grid grid-cols-1 gap-8">
159-
${this.renderHeader()}
160+
${this.renderHeader()} ${this.renderErrors()}
160161
${navigated.oneOf &&
161162
html`<one-of-element
162163
@change=${(ev: CustomEvent<number>) => (this.oneOfIndex = ev.detail)}
@@ -224,6 +225,24 @@ export class JsonUiElement extends LitElement {
224225
`;
225226
}
226227

228+
private renderErrors() {
229+
const errors = this.ajvValidateFn?.errors ?? [];
230+
console.log(errors);
231+
return html`
232+
<div class="flex flex-col p-8 ring-2 ring-red-500 gap-4">
233+
${errors.map(
234+
(e) =>
235+
html`<div class="text-red-500 flex flex-col">
236+
${humanizeValue(e).map(
237+
([key, val]) =>
238+
html` <div><strong>${key}</strong>: ${val}</div> `
239+
)}
240+
</div>`
241+
)}
242+
</div>
243+
`;
244+
}
245+
227246
private renderNextButton() {
228247
return nothing;
229248
// return html` <button>HIII</button> `;

src/ui/number-element.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class NumberElement extends LitElement {
7676

7777
private handleChange(ev: Event) {
7878
const target = ev.target as HTMLInputElement;
79-
const value = target.value;
79+
const value = target.valueAsNumber;
8080
this.dispatchEvent(
8181
new CustomEvent("change", {
8282
detail: { value },
@@ -105,9 +105,9 @@ export class NumberElement extends LitElement {
105105
${this.icon}
106106
</div>
107107
<input
108-
type="text"
108+
type="number"
109109
@change=${this.handleChange}
110-
.value=${this.value ?? ""}
110+
.value=${this.value}
111111
class="px-3 py-2 focus:ring-0 bg-transparent border-none text-[0.8125rem] focus:outline-none w-full"
112112
/>
113113
</div>

0 commit comments

Comments
 (0)