Skip to content

Commit 987bb23

Browse files
committed
Fix #87: Don't mutate original schema when dealing with const
1 parent e0c6845 commit 987bb23

File tree

3 files changed

+70
-43
lines changed

3 files changed

+70
-43
lines changed

src/data.js

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export function getBlankObject(schema, getRef) {
1212
let value = schema_keys[key];
1313

1414
let isRef = value.hasOwnProperty('$ref');
15+
let isConst = value.hasOwnProperty('const');
1516

1617
if (isRef)
1718
value = getRef(value['$ref']);
@@ -24,24 +25,27 @@ export function getBlankObject(schema, getRef) {
2425
value = value.oneOf[0];
2526
else if (value.hasOwnProperty('anyOf'))
2627
value = value.anyOf[0];
27-
else if (value.hasOwnProperty('const')) {
28-
value.default = value.const;
29-
value.type = actualType(value.const);
30-
}
3128

3229
type = normalizeKeyword(value.type);
3330
}
3431

32+
let default_ = value.default;
33+
34+
if (isConst) {
35+
type = actualType(value.const);
36+
default_ = value.const;
37+
}
38+
3539
if (type === 'array')
3640
keys[key] = isRef ? [] : getBlankArray(value, getRef);
3741
else if (type === 'object')
3842
keys[key] = getBlankObject(value, getRef);
3943
else if (type === 'boolean')
40-
keys[key] = value.default === false ? false : (value.default || null);
44+
keys[key] = default_ === false ? false : (default_ || null);
4145
else if (type === 'integer' || type === 'number')
42-
keys[key] = value.default === 0 ? 0 : (value.default || null);
46+
keys[key] = default_ === 0 ? 0 : (default_ || null);
4347
else
44-
keys[key] = value.default || '';
48+
keys[key] = default_ || '';
4549
}
4650

4751
if (schema.hasOwnProperty('oneOf'))
@@ -75,7 +79,7 @@ export function getBlankArray(schema, getRef) {
7579
return items;
7680

7781
if (schema.items.hasOwnProperty('$ref')) {
78-
// :TODO: this will most probably mutate the original schema
82+
// :TODO: this mutates the original schema
7983
// but i'll fix it later
8084
schema.items = getRef(schema.items['$ref']);
8185
}
@@ -89,11 +93,8 @@ export function getBlankArray(schema, getRef) {
8993
type = getSchemaType(schema.items.anyOf[0]);
9094
else if (Array.isArray(schema.items['allOf']))
9195
type = getSchemaType(schema.items.allOf[0]);
92-
else if (schema.items.hasOwnProperty('const')) {
96+
else if (schema.items.hasOwnProperty('const'))
9397
type = actualType(schema.items.const);
94-
schema.items.type = type;
95-
schema.items.default = schema.items.const;
96-
}
9798
}
9899

99100
if (type === 'array') {
@@ -117,16 +118,21 @@ export function getBlankArray(schema, getRef) {
117118
if (schema.items.widget === 'multiselect')
118119
return items;
119120

121+
let default_ = schema.items.default;
122+
123+
if (schema.items.hasOwnProperty('const'))
124+
default_ = schema.items.const;
125+
120126
if (type === 'boolean') {
121127
while (items.length < minItems)
122-
items.push(schema.items.default === false ? false : (schema.items.default || null));
128+
items.push(default_ === false ? false : (default_ || null));
123129
} else if (type === 'integer' || type === 'number') {
124130
while (items.length < minItems)
125-
items.push(schema.items.default === 0 ? 0 : (schema.items.default || null));
131+
items.push(default_ === 0 ? 0 : (default_ || null));
126132
} else {
127133
// string, etc.
128134
while (items.length < minItems)
129-
items.push(schema.items.default || '');
135+
items.push(default_ || '');
130136
}
131137

132138
return items;
@@ -163,13 +169,15 @@ export function getBlankData(schema, getRef) {
163169
if (schema.hasOwnProperty('$ref'))
164170
schema = getRef(schema['$ref']);
165171

172+
let type = getSchemaType(schema);
173+
174+
let default_ = schema.default;
175+
166176
if (schema.hasOwnProperty('const')) {
167-
schema.type = actualType(schema.const);
168-
schema.default = schema.const;
177+
type = actualType(schema.const);
178+
default_ = schema.const;
169179
}
170180

171-
let type = getSchemaType(schema);
172-
173181
if (type === 'array')
174182
return getBlankArray(schema, getRef);
175183
else if (type === 'object')
@@ -181,11 +189,11 @@ export function getBlankData(schema, getRef) {
181189
else if (type === 'anyOf')
182190
return getBlankAnyOf(schema, getRef);
183191
else if (type === 'boolean')
184-
return schema.default === false ? false : (schema.default || null);
192+
return default_ === false ? false : (default_ || null);
185193
else if (type === 'integer' || type === 'number')
186-
return schema.default === 0 ? 0 : (schema.default || null);
194+
return default_ === 0 ? 0 : (default_ || null);
187195
else // string, etc.
188-
return schema.default || '';
196+
return default_ || '';
189197
}
190198

191199

@@ -201,12 +209,17 @@ function getSyncedArray(data, schema, getRef) {
201209
schema.items = getRef(schema.items['$ref'])
202210
}
203211

212+
let type;
213+
let default_;
214+
204215
if (schema.items.hasOwnProperty('const')) {
205-
schema.items.default = schema.items.const;
206-
schema.items.type = actualType(schema.items.const);
216+
type = actualType(schema.items.const);
217+
default_ = schema.items.const;
218+
} else {
219+
type = normalizeKeyword(schema.items.type);
220+
default_ = schema.items.defualt;
207221
}
208222

209-
let type = normalizeKeyword(schema.items.type);
210223
let minItems = schema.minItems || schema.min_items || 0;
211224

212225
while (data.length < minItems)
@@ -230,11 +243,11 @@ function getSyncedArray(data, schema, getRef) {
230243

231244
if (item === FILLER) {
232245
if (type === 'integer' || type === 'number')
233-
newData[i] = schema.items.default === 0 ? 0 : (schema.items.default || null);
246+
newData[i] = default_ === 0 ? 0 : (default_ || null);
234247
else if (type === 'boolean')
235-
newData[i] = schema.items.default === false ? false : (schema.items.default || null);
248+
newData[i] = default_ === false ? false : (default_ || null);
236249
else
237-
newData[i] = schema.items.default || '';
250+
newData[i] = default_ || '';
238251
}
239252
}
240253

@@ -275,24 +288,28 @@ function getSyncedObject(data, schema, getRef) {
275288
if (isRef)
276289
schemaValue = getRef(schemaValue['$ref']);
277290

291+
let type;
292+
let default_;
293+
278294
if (schemaValue.hasOwnProperty('const')) {
279-
schemaValue.default = schemaValue.const;
280-
schemaValue.type = actualType(schemaValue.const);
295+
type = actualType(schemaValue.const);
296+
default_ = schemaValue.const;
297+
} else {
298+
let type = getSchemaType(schemaValue);
299+
default_ = schemaValue.default;
281300
}
282-
283-
let type = getSchemaType(schemaValue);
284301

285302
if (!data.hasOwnProperty(key)) {
286303
if (type === 'array')
287304
newData[key] = getSyncedArray([], schemaValue, getRef);
288305
else if (type === 'object')
289306
newData[key] = getSyncedObject({}, schemaValue, getRef);
290307
else if (type === 'boolean')
291-
newData[key] = schemaValue.default === false ? false : (schemaValue.default || null);
308+
newData[key] = default_ === false ? false : (default_ || null);
292309
else if (type === 'integer' || type === 'number')
293-
newData[key] = schemaValue.default === 0 ? 0 : (schemaValue.default || null);
310+
newData[key] = default_ === 0 ? 0 : (default_ || null);
294311
else
295-
newData[key] = schemaValue.default || '';
312+
newData[key] = default_ || '';
296313
} else {
297314
if (type === 'array')
298315
newData[key] = getSyncedArray(data[key], schemaValue, getRef);
@@ -305,11 +322,11 @@ function getSyncedObject(data, schema, getRef) {
305322

306323
if (data[key] === '') {
307324
if (type === 'integer' || type === 'number')
308-
newData[key] = schemaValue.default === 0 ? 0 : (schemaValue.default || null);
325+
newData[key] = default_ === 0 ? 0 : (default_ || null);
309326
else if (type === 'boolean')
310-
newData[key] = schemaValue.default === false ? false : (schemaValue.default || null);
327+
newData[key] = default_ === false ? false : (default_ || null);
311328
else
312-
newData[key] = schemaValue.default || '';
329+
newData[key] = default_ || '';
313330
} else {
314331
newData[key] = data[key];
315332
}

src/ui.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,22 @@ function FormField(props) {
4747
if (props.schema.handler)
4848
inputProps.handler = props.schema.handler;
4949

50-
let type = normalizeKeyword(props.schema.type);
50+
let type;
51+
52+
if (props.schema.hasOwnProperty('const')) {
53+
type = actualType(props.schema.const);
54+
inputProps.readOnly = true;
55+
} else {
56+
type = normalizeKeyword(props.schema.type);
57+
}
58+
5159
let choices = getKeyword(props.schema, 'choices', 'enum');
5260

5361
if (choices) {
5462
inputProps.options = choices;
5563
type = 'select';
5664
}
5765

58-
if (props.schema.hasOwnProperty('const'))
59-
inputProps.readOnly = true;
60-
6166
if (props.schema.widget) {
6267
if (props.schema.widget === 'multiselect' && props.parentType !== 'array') {
6368
// pass

src/util.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ export function getSchemaType(schema) {
6565
6666
If data is given, it will try to use that to guess the type.
6767
*/
68-
let type = normalizeKeyword(schema.type);
68+
let type;
69+
70+
if (schema.hasOwnProperty('const'))
71+
type = actualType(schema.const);
72+
else
73+
type = normalizeKeyword(schema.type);
6974

7075
if (!type) {
7176
if (schema.hasOwnProperty('properties') ||

0 commit comments

Comments
 (0)