This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
/
schema.js
142 lines (111 loc) · 4.56 KB
/
schema.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* Copyright 2016 aixigo AG
* Released under the MIT license.
* http://laxarjs.org/license
*/
define( [
'../utilities/object'
], function( object ) {
'use strict';
function transformV3V4Recursively( schema, parentKey, parentSchema, originalParentSchema ) {
var resultingSchema = {};
Object.keys( schema ).forEach( function( key ) {
var value = schema[ key ];
switch( key ) {
case 'required':
if( value !== true ) {
break;
}
if( isNamedProperty( parentKey, originalParentSchema ) && !( 'default' in schema ) ) {
if( !( 'required' in parentSchema ) ) {
parentSchema.required = [];
}
parentSchema.required.push( parentKey );
}
break;
case 'items':
resultingSchema[ key ] = transformV3V4Recursively( value, key, resultingSchema, schema );
break;
case 'additionalProperties':
if( typeof value === 'object' ) {
resultingSchema[ key ] = transformV3V4Recursively( value, key, resultingSchema, schema );
}
else {
resultingSchema[ key ] = value;
}
break;
case 'properties':
case 'patternProperties':
resultingSchema[ key ] = {};
object.forEach( value, function( patternSchema, pattern ) {
resultingSchema[ key ][ pattern ] =
transformV3V4Recursively( patternSchema, pattern, resultingSchema, schema );
} );
break;
default:
resultingSchema[ key ] = value;
}
} );
// LaxarJS specific: transform "not required" to "allow null"
if( isNamedProperty( parentKey, originalParentSchema ) && !schema.required ) {
var propertyType = resultingSchema.type;
if( typeof propertyType === 'string' && propertyType !== 'null' ) {
resultingSchema.type = [ propertyType, 'null' ];
}
else if( Array.isArray( propertyType ) && propertyType.indexOf( 'null' ) === -1 ) {
propertyType.push( 'null' );
}
}
return resultingSchema;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
function prohibitAdditionalPropertiesRecursively( schema ) {
if( ( 'properties' in schema || 'patternProperties' in schema ) &&
!( 'additionalProperties' in schema ) ) {
schema.additionalProperties = false;
}
if( 'properties' in schema ) {
Object.keys( schema.properties ).forEach( function( name ) {
prohibitAdditionalPropertiesRecursively( schema.properties[ name ] );
} );
}
if( 'additionalProperties' in schema && typeof schema.additionalProperties === 'object' ) {
prohibitAdditionalPropertiesRecursively( schema.additionalProperties );
}
if( 'patternProperties' in schema ) {
Object.keys( schema.patternProperties ).forEach( function( pattern ) {
prohibitAdditionalPropertiesRecursively( schema.patternProperties[ pattern ] );
} );
}
if( 'items' in schema ) {
prohibitAdditionalPropertiesRecursively( schema.items );
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
function isNamedProperty( key, parentSchema ) {
return parentSchema &&
schemaAllowsType( parentSchema, 'object' ) &&
object.path( parentSchema, 'properties.' + key );
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
function schemaAllowsType( schema, type ) {
var schemaType = schema.type;
if( typeof schemaType === 'string' ) {
return schemaType === type;
}
if( Array.isArray( schemaType ) ) {
return schemaType.indexOf( type ) !== -1;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
return {
transformV3ToV4: function( schema ) {
return transformV3V4Recursively( schema );
},
////////////////////////////////////////////////////////////////////////////////////////////////////////
prohibitAdditionalProperties: function( schema ) {
prohibitAdditionalPropertiesRecursively( schema );
}
};
} );