11const structs = [ 'allOf' , 'anyOf' , 'oneOf' , 'not' , 'items' , 'additionalProperties' ] ;
22
3+ function InvalidTypeError ( message ) {
4+ this . name = 'InvalidTypeError' ;
5+ this . message = message ;
6+ }
7+
8+ InvalidTypeError . prototype = new Error ( ) ;
9+
310function convert ( schema , options ) {
411 options = options || { } ;
512 options . cloneSchema = ! ( options . cloneSchema === false ) ;
@@ -8,12 +15,18 @@ function convert(schema, options) {
815 schema = JSON . parse ( JSON . stringify ( schema ) ) ;
916 }
1017
11- delete schema [ '$schema' ] ;
18+ schema = removeRootKeywords ( schema ) ;
1219 schema = convertSchema ( schema ) ;
1320
1421 return schema ;
1522}
1623
24+ function removeRootKeywords ( schema ) {
25+ delete schema [ '$schema' ] ;
26+ delete schema [ 'id' ] ;
27+ return schema ;
28+ }
29+
1730function convertSchema ( schema ) {
1831 let i = 0 ;
1932 let j = 0 ;
@@ -47,7 +60,10 @@ function convertSchema(schema) {
4760
4861 }
4962
63+ validateType ( schema . type ) ;
64+
5065 schema = convertTypes ( schema ) ;
66+ schema = convertDependencies ( schema ) ;
5167
5268 if ( typeof schema [ 'patternProperties' ] === 'object' ) {
5369 schema = convertPatternProperties ( schema ) ;
@@ -56,11 +72,19 @@ function convertSchema(schema) {
5672 return schema ;
5773}
5874
75+ function validateType ( type ) {
76+ const validTypes = [ 'null' , 'boolean' , 'object' , 'array' , 'number' , 'string' , 'integer' ] ;
77+ const types = Array . isArray ( type ) ? type : [ type ] ;
78+ types . forEach ( type => {
79+ if ( validTypes . indexOf ( type ) < 0 && type !== undefined )
80+ throw new InvalidTypeError ( 'Type "' + type + '" is not a valid type' ) ;
81+ } ) ;
82+ }
83+
5984function convertProperties ( properties ) {
60- var key
61- , property
62- , props = { }
63- ;
85+ let key = { } ;
86+ let property = { } ;
87+ let props = { } ;
6488
6589 for ( key in properties ) {
6690 property = properties [ key ] ;
@@ -70,6 +94,51 @@ function convertProperties(properties) {
7094 return props ;
7195}
7296
97+ function convertDependencies ( schema ) {
98+ const deps = schema . dependencies ;
99+ if ( typeof deps !== 'object' ) {
100+ return schema ;
101+ }
102+
103+ // Turns the dependencies keyword into an allOf of oneOf's
104+ // "dependencies": {
105+ // "post-office-box": ["street-address"]
106+ // },
107+ //
108+ // becomes
109+ //
110+ // "allOf": [
111+ // {
112+ // "oneOf": [
113+ // {"not": {"required": ["post-office-box"]}},
114+ // {"required": ["post-office-box", "street-address"]}
115+ // ]
116+ // }
117+ //
118+
119+ delete schema [ 'dependencies' ] ;
120+ if ( ! Array . isArray ( schema . allOf ) ) {
121+ schema . allOf = [ ] ;
122+ }
123+
124+ for ( const key in deps ) {
125+ const foo = {
126+ 'oneOf' : [
127+ {
128+ 'not' : {
129+ 'required' : [ key ]
130+ }
131+ } ,
132+ {
133+ 'required' : [ ] . concat ( key , deps [ key ] )
134+ }
135+ ]
136+ } ;
137+ schema . allOf . push ( foo ) ;
138+ }
139+ return schema ;
140+ }
141+
73142function convertTypes ( schema ) {
74143 var newType ;
75144
0 commit comments