@@ -11,7 +11,9 @@ import { get, isEmpty, isNull, isUndefined, set } from "lodash";
11
11
import { inferDescription , inferTitle , inferType } from "./parser/infer" ;
12
12
import { ROOT_PATH , DEFAULT_VALUES , PATH_SEPARATOR } from "./constants" ;
13
13
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" ;
15
17
16
18
@customElement ( "json-ui-element" )
17
19
export class JsonUiElement extends LitElement {
@@ -36,6 +38,9 @@ export class JsonUiElement extends LitElement {
36
38
JSONSchema7
37
39
> ;
38
40
41
+ @state ( )
42
+ ajvValidateFn ?: ValidateFunction ;
43
+
39
44
@state ( )
40
45
oneOfIndex = 0 ;
41
46
@@ -54,11 +59,12 @@ export class JsonUiElement extends LitElement {
54
59
) : void {
55
60
// Always resolve the value because nested values may have changed.
56
61
this . resolvedValue = this . path ? get ( this . value , this . path ) : this . value ;
57
- console . log ( 'resolvedValue' , this . resolvedValue , this . value , this . path )
58
62
this . dispatchEvent ( new CustomEvent ( "change" , { detail : this . value } ) ) ;
59
63
60
64
this . resolvedSchemas ??= { } as any ;
61
65
66
+ if ( this . schema && this . ajvValidateFn ) this . ajvValidateFn ! ( this . value ) ;
67
+
62
68
// Determine the depth of changes to optimize the schema resolution process.
63
69
// Note: oneOf and anyOf are currently only supported at the top level!
64
70
const updateLevel = [
@@ -80,6 +86,8 @@ export class JsonUiElement extends LitElement {
80
86
) ;
81
87
82
88
if ( updateLevel === 0 ) {
89
+ this . ajvValidateFn = ajvFactory ( ) . compile ( this . schema ) ;
90
+ this . ajvValidateFn ! ( this . value ) ;
83
91
this . resolvedSchemas . resolvedRefs = resolveRefs ( this . schema ) ;
84
92
this . resolvedSchemas . resolvedAllOf = allOf (
85
93
this . resolvedSchemas . resolvedRefs
@@ -105,13 +113,6 @@ export class JsonUiElement extends LitElement {
105
113
this . resolvedSchemas . navigated ,
106
114
this . resolvedValue
107
115
) ;
108
-
109
- console . log (
110
- "oneOfIndex inferred" ,
111
- this . oneOfIndex ,
112
- this . resolvedSchemas . navigated ,
113
- this . resolvedValue
114
- ) ;
115
116
}
116
117
117
118
if ( updateLevel <= 2 ) {
@@ -156,7 +157,7 @@ export class JsonUiElement extends LitElement {
156
157
157
158
return html `
158
159
< div class ="grid grid-cols-1 gap-8 ">
159
- ${ this . renderHeader ( ) }
160
+ ${ this . renderHeader ( ) } ${ this . renderErrors ( ) }
160
161
${ navigated . oneOf &&
161
162
html `< one-of-element
162
163
@change =${ ( ev : CustomEvent < number > ) => ( this . oneOfIndex = ev . detail ) }
@@ -224,6 +225,24 @@ export class JsonUiElement extends LitElement {
224
225
` ;
225
226
}
226
227
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
+
227
246
private renderNextButton ( ) {
228
247
return nothing ;
229
248
// return html` <button>HIII</button> `;
0 commit comments