@@ -97,7 +97,7 @@ export abstract class SchemaTreeNode<T> implements SchemaNode {
97
97
get itemPrototype ( ) : SchemaTreeNode < any > | null { return null ; }
98
98
99
99
abstract get ( ) : T ;
100
- set ( v : T , force = false ) {
100
+ set ( v : T , init = false , force = false ) {
101
101
if ( ! this . readOnly ) {
102
102
throw new MissingImplementationError ( ) ;
103
103
}
@@ -220,10 +220,10 @@ export class OneOfSchemaTreeNode extends NonLeafSchemaTreeNode<any> {
220
220
}
221
221
222
222
this . _currentTypeHolder = proto ;
223
- this . _currentTypeHolder . set ( v , true ) ;
223
+ this . _currentTypeHolder . set ( v , false , true ) ;
224
224
}
225
225
226
- set ( v : any , force = false ) {
226
+ set ( v : any , init = false , force = false ) {
227
227
return this . _set ( v , false , force ) ;
228
228
}
229
229
@@ -338,7 +338,6 @@ export class ArraySchemaTreeNode extends NonLeafSchemaTreeNode<Array<any>> {
338
338
const schema = this . _schema ;
339
339
const forward = this . _forward ;
340
340
341
- this . _defined = ! ! value ;
342
341
this . _value = Object . create ( null ) ;
343
342
this . _dirty = this . _dirty || ! init ;
344
343
@@ -361,8 +360,8 @@ export class ArraySchemaTreeNode extends NonLeafSchemaTreeNode<Array<any>> {
361
360
}
362
361
}
363
362
364
- set ( v : any , force = false ) {
365
- return this . _set ( v , false , force ) ;
363
+ set ( v : any , init = false , force = false ) {
364
+ return this . _set ( v , init , force ) ;
366
365
}
367
366
368
367
isCompatible ( v : any ) { return Array . isArray ( v ) ; }
@@ -395,7 +394,7 @@ export class RootSchemaTreeNode extends ObjectSchemaTreeNode {
395
394
396
395
/** A leaf in the schema tree. Must contain a single primitive value. */
397
396
export abstract class LeafSchemaTreeNode < T > extends SchemaTreeNode < T > {
398
- private _default : T ;
397
+ protected _default : T ;
399
398
400
399
constructor ( metaData : TreeNodeConstructorArgument < T > ) {
401
400
super ( metaData ) ;
@@ -410,13 +409,13 @@ export abstract class LeafSchemaTreeNode<T> extends SchemaTreeNode<T> {
410
409
return this . _forward . get ( ) ;
411
410
}
412
411
if ( ! this . defined ) {
413
- return this . _default !== undefined ? this . _default : undefined ;
412
+ return 'default' in this . _schema ? this . _default : undefined ;
414
413
}
415
414
return this . _value === undefined
416
415
? undefined
417
416
: ( this . _value === null ? null : this . convert ( this . _value ) ) ;
418
417
}
419
- set ( v : T , force = false ) {
418
+ set ( v : T , init = false , force = false ) {
420
419
if ( this . readOnly && ! force ) {
421
420
throw new SettingReadOnlyPropertyError ( ) ;
422
421
}
@@ -428,7 +427,7 @@ export abstract class LeafSchemaTreeNode<T> extends SchemaTreeNode<T> {
428
427
}
429
428
}
430
429
431
- this . dirty = true ;
430
+ this . dirty = ! init ;
432
431
this . _value = convertedValue ;
433
432
}
434
433
@@ -438,7 +437,10 @@ export abstract class LeafSchemaTreeNode<T> extends SchemaTreeNode<T> {
438
437
}
439
438
440
439
get defaultValue ( ) : T {
441
- return 'default' in this . _schema ? this . _default : null ;
440
+ return this . hasDefault ? this . _default : null ;
441
+ }
442
+ get hasDefault ( ) {
443
+ return 'default' in this . _schema ;
442
444
}
443
445
444
446
abstract convert ( v : any ) : T ;
@@ -462,20 +464,20 @@ class StringSchemaTreeNode extends LeafSchemaTreeNode<string> {
462
464
463
465
464
466
class EnumSchemaTreeNode extends StringSchemaTreeNode {
465
- private _enumValues : string [ ] ;
466
-
467
467
constructor ( metaData : TreeNodeConstructorArgument < string > ) {
468
468
super ( metaData ) ;
469
469
470
470
if ( ! Array . isArray ( metaData . schema [ 'enum' ] ) ) {
471
471
throw new InvalidSchema ( ) ;
472
472
}
473
- this . _enumValues = [ ] . concat ( metaData . schema [ 'enum' ] ) ;
474
- this . set ( metaData . value , true ) ;
473
+ if ( this . hasDefault && ! this . _isInEnum ( this . _default ) ) {
474
+ throw new InvalidSchema ( ) ;
475
+ }
476
+ this . set ( metaData . value , true , true ) ;
475
477
}
476
478
477
479
protected _isInEnum ( value : string ) {
478
- return this . _enumValues . some ( v => v === value ) ;
480
+ return this . _schema [ 'enum' ] . some ( ( v : string ) => v === value ) ;
479
481
}
480
482
481
483
isCompatible ( v : any ) {
0 commit comments