77 */
88import { JsonObject , JsonValue , isJsonObject } from '../interface' ;
99import { JsonPointer } from './interface' ;
10-
11- const allTypes = [ 'string' , 'integer' , 'number' , 'object' , 'array' , 'boolean' , 'null' ] ;
12-
13- function findTypes ( schema : JsonObject ) : Set < string > {
14- if ( ! schema ) {
15- return new Set ( ) ;
16- }
17-
18- let potentials : Set < string > ;
19- if ( typeof schema . type === 'string' ) {
20- potentials = new Set ( [ schema . type ] ) ;
21- } else if ( Array . isArray ( schema . type ) ) {
22- potentials = new Set ( schema . type as string [ ] ) ;
23- } else {
24- potentials = new Set ( allTypes ) ;
25- }
26-
27- if ( isJsonObject ( schema . not ) ) {
28- const notTypes = findTypes ( schema . not ) ;
29- potentials = new Set ( [ ...potentials ] . filter ( p => ! notTypes . has ( p ) ) ) ;
30- }
31-
32- if ( Array . isArray ( schema . allOf ) ) {
33- for ( const sub of schema . allOf ) {
34- const types = findTypes ( sub as JsonObject ) ;
35- potentials = new Set ( [ ...potentials ] . filter ( p => types . has ( p ) ) ) ;
36- }
37- }
38-
39- if ( Array . isArray ( schema . oneOf ) ) {
40- let options = new Set < string > ( ) ;
41- for ( const sub of schema . oneOf ) {
42- const types = findTypes ( sub as JsonObject ) ;
43- options = new Set ( [ ...options , ...types ] ) ;
44- }
45- potentials = new Set ( [ ...potentials ] . filter ( p => options . has ( p ) ) ) ;
46- }
47-
48- if ( Array . isArray ( schema . anyOf ) ) {
49- let options = new Set < string > ( ) ;
50- for ( const sub of schema . anyOf ) {
51- const types = findTypes ( sub as JsonObject ) ;
52- options = new Set ( [ ...options , ...types ] ) ;
53- }
54- potentials = new Set ( [ ...potentials ] . filter ( p => options . has ( p ) ) ) ;
55- }
56-
57- return potentials ;
58- }
10+ import { getTypesOfSchema } from './utility' ;
5911
6012export function addUndefinedDefaults (
6113 value : JsonValue ,
@@ -66,7 +18,7 @@ export function addUndefinedDefaults(
6618 return value ;
6719 }
6820
69- const types = findTypes ( schema ) ;
21+ const types = getTypesOfSchema ( schema ) ;
7022 if ( types . size === 0 ) {
7123 return value ;
7224 }
@@ -110,6 +62,8 @@ export function addUndefinedDefaults(
11062 for ( const propName of Object . getOwnPropertyNames ( schema . properties ) ) {
11163 if ( propName in newValue ) {
11264 continue ;
65+ } else if ( propName == '$schema' ) {
66+ continue ;
11367 }
11468
11569 // TODO: Does not currently handle more complex schemas (oneOf/anyOf/etc.)
0 commit comments