7
7
*/
8
8
import { JsonObject , JsonValue , isJsonObject } from '../interface' ;
9
9
import { 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' ;
59
11
60
12
export function addUndefinedDefaults (
61
13
value : JsonValue ,
@@ -66,7 +18,7 @@ export function addUndefinedDefaults(
66
18
return value ;
67
19
}
68
20
69
- const types = findTypes ( schema ) ;
21
+ const types = getTypesOfSchema ( schema ) ;
70
22
if ( types . size === 0 ) {
71
23
return value ;
72
24
}
@@ -110,6 +62,8 @@ export function addUndefinedDefaults(
110
62
for ( const propName of Object . getOwnPropertyNames ( schema . properties ) ) {
111
63
if ( propName in newValue ) {
112
64
continue ;
65
+ } else if ( propName == '$schema' ) {
66
+ continue ;
113
67
}
114
68
115
69
// TODO: Does not currently handle more complex schemas (oneOf/anyOf/etc.)
0 commit comments