Skip to content

Commit 689ac76

Browse files
committed
Bump version 2.8.3 and update dist files
1 parent 8c9bc68 commit 689ac76

File tree

5 files changed

+95
-8
lines changed

5 files changed

+95
-8
lines changed

dist/react-json-form.cjs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,18 @@ function getKey(obj, key, default_value) {
180180
let val = obj[key];
181181
return typeof val !== 'undefined' ? val : default_value;
182182
}
183+
function valueInChoices(schema, value) {
184+
/* Checks whether the given value is in schema choices or not.
185+
If schema doesn't have choices, returns true.
186+
*/
187+
let choices = getKeyword(schema, 'choices', 'enum');
188+
if (!choices) return true;
189+
let found = choices.find(choice => {
190+
if (typeof choice == 'object') choice = choice.value;
191+
return value == choice;
192+
});
193+
return found !== undefined ? true : false;
194+
}
183195
/* Set operations */
184196

185197
function isEqualset(a, b) {
@@ -323,6 +335,9 @@ function getSyncedArray(data, schema, getRef) {
323335
if (item === FILLER) item = {};
324336
newData[i] = getSyncedObject(item, schema.items, getRef);
325337
} else {
338+
// if the current value is not in choices, we reset to blank
339+
if (!valueInChoices(schema.items, newData[i])) item = FILLER;
340+
326341
if (item === FILLER) {
327342
if (type === 'integer' || type === 'number') newData[i] = schema.items.default === 0 ? 0 : schema.items.default || null;else if (type === 'boolean') newData[i] = schema.items.default === false ? false : schema.items.default || null;else newData[i] = schema.items.default || '';
328343
}
@@ -358,8 +373,11 @@ function getSyncedObject(data, schema, getRef) {
358373
if (type === 'array') newData[key] = getSyncedArray([], schemaValue, getRef);else if (type === 'object') newData[key] = getSyncedObject({}, schemaValue, getRef);else if (type === 'boolean') newData[key] = schemaValue.default === false ? false : schemaValue.default || null;else if (type === 'integer' || type === 'number') newData[key] = schemaValue.default === 0 ? 0 : schemaValue.default || null;else newData[key] = schemaValue.default || '';
359374
} else {
360375
if (type === 'array') newData[key] = getSyncedArray(data[key], schemaValue, getRef);else if (type === 'object') newData[key] = getSyncedObject(data[key], schemaValue, getRef);else {
376+
// if the current value is not in choices, we reset to blank
377+
if (!valueInChoices(schemaValue, data[key])) data[key] = '';
378+
361379
if (data[key] === '') {
362-
if (type === 'integer' || type === 'number') newData[key] = schemaValue.default === 0 ? 0 : schemaValue.default || null;else if (type === 'boolean') newData[key] = schemaValue.default === false ? false : schemaValue.default || null;
380+
if (type === 'integer' || type === 'number') newData[key] = schemaValue.default === 0 ? 0 : schemaValue.default || null;else if (type === 'boolean') newData[key] = schemaValue.default === false ? false : schemaValue.default || null;else newData[key] = schemaValue.default || '';
363381
} else {
364382
newData[key] = data[key];
365383
}
@@ -964,7 +982,7 @@ function FormSelectInput(_ref4) {
964982
}, props), /*#__PURE__*/React__default["default"].createElement("option", {
965983
disabled: true,
966984
value: "",
967-
key: '__placehlder'
985+
key: '__placeholder'
968986
}, "Select..."), options.map((option, i) => {
969987
let title, inputValue;
970988

@@ -3906,6 +3924,12 @@ function DataValidator(schema) {
39063924
return;
39073925
if (schema.minLength && data.length < parseInt(schema.minLength)) this.addError(coords, 'This value must be at least ' + schema.minLength + ' characters long.');
39083926
if ((schema.maxLength || schema.maxLength == 0) && data.length > parseInt(schema.maxLength)) this.addError(coords, 'This value may not be longer than ' + schema.maxLength + ' characters.');
3927+
3928+
if (!valueInChoices(schema, data)) {
3929+
this.addError(coords, 'Invalid choice "' + data + '"');
3930+
return;
3931+
}
3932+
39093933
let format = normalizeKeyword(schema.format);
39103934
let format_validator;
39113935

@@ -3981,6 +4005,11 @@ function DataValidator(schema) {
39814005
if ((schema.exclusiveMinimum || schema.exclusiveMinimum === 0) && data <= schema.exclusiveMinimum) this.addError(coords, 'This value must be greater than ' + schema.exclusiveMinimum);
39824006
if ((schema.exclusiveMaximum || schema.exclusiveMaximum === 0) && data >= schema.exclusiveMaximum) this.addError(coords, 'This value must be less than ' + schema.exclusiveMaximum);
39834007
if ((schema.multipleOf || schema.multipleOf === 0) && data * 100 % (schema.multipleOf * 100) / 100) this.addError(coords, 'This value must be a multiple of ' + schema.multipleOf);
4008+
4009+
if (!valueInChoices(schema, data)) {
4010+
this.addError(coords, 'Invalid choice "' + data + '"');
4011+
return;
4012+
}
39844013
};
39854014

39864015
this.validateEmail = function (schema, data, coords) {

dist/react-json-form.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/react-json-form.modern.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,18 @@ function getKey(obj, key, default_value) {
174174
let val = obj[key];
175175
return typeof val !== 'undefined' ? val : default_value;
176176
}
177+
function valueInChoices(schema, value) {
178+
/* Checks whether the given value is in schema choices or not.
179+
If schema doesn't have choices, returns true.
180+
*/
181+
let choices = getKeyword(schema, 'choices', 'enum');
182+
if (!choices) return true;
183+
let found = choices.find(choice => {
184+
if (typeof choice == 'object') choice = choice.value;
185+
return value == choice;
186+
});
187+
return found !== undefined ? true : false;
188+
}
177189
/* Set operations */
178190

179191
function isEqualset(a, b) {
@@ -317,6 +329,9 @@ function getSyncedArray(data, schema, getRef) {
317329
if (item === FILLER) item = {};
318330
newData[i] = getSyncedObject(item, schema.items, getRef);
319331
} else {
332+
// if the current value is not in choices, we reset to blank
333+
if (!valueInChoices(schema.items, newData[i])) item = FILLER;
334+
320335
if (item === FILLER) {
321336
if (type === 'integer' || type === 'number') newData[i] = schema.items.default === 0 ? 0 : schema.items.default || null;else if (type === 'boolean') newData[i] = schema.items.default === false ? false : schema.items.default || null;else newData[i] = schema.items.default || '';
322337
}
@@ -352,8 +367,11 @@ function getSyncedObject(data, schema, getRef) {
352367
if (type === 'array') newData[key] = getSyncedArray([], schemaValue, getRef);else if (type === 'object') newData[key] = getSyncedObject({}, schemaValue, getRef);else if (type === 'boolean') newData[key] = schemaValue.default === false ? false : schemaValue.default || null;else if (type === 'integer' || type === 'number') newData[key] = schemaValue.default === 0 ? 0 : schemaValue.default || null;else newData[key] = schemaValue.default || '';
353368
} else {
354369
if (type === 'array') newData[key] = getSyncedArray(data[key], schemaValue, getRef);else if (type === 'object') newData[key] = getSyncedObject(data[key], schemaValue, getRef);else {
370+
// if the current value is not in choices, we reset to blank
371+
if (!valueInChoices(schemaValue, data[key])) data[key] = '';
372+
355373
if (data[key] === '') {
356-
if (type === 'integer' || type === 'number') newData[key] = schemaValue.default === 0 ? 0 : schemaValue.default || null;else if (type === 'boolean') newData[key] = schemaValue.default === false ? false : schemaValue.default || null;
374+
if (type === 'integer' || type === 'number') newData[key] = schemaValue.default === 0 ? 0 : schemaValue.default || null;else if (type === 'boolean') newData[key] = schemaValue.default === false ? false : schemaValue.default || null;else newData[key] = schemaValue.default || '';
357375
} else {
358376
newData[key] = data[key];
359377
}
@@ -958,7 +976,7 @@ function FormSelectInput(_ref4) {
958976
}, props), /*#__PURE__*/React$1.createElement("option", {
959977
disabled: true,
960978
value: "",
961-
key: '__placehlder'
979+
key: '__placeholder'
962980
}, "Select..."), options.map((option, i) => {
963981
let title, inputValue;
964982

@@ -3900,6 +3918,12 @@ function DataValidator(schema) {
39003918
return;
39013919
if (schema.minLength && data.length < parseInt(schema.minLength)) this.addError(coords, 'This value must be at least ' + schema.minLength + ' characters long.');
39023920
if ((schema.maxLength || schema.maxLength == 0) && data.length > parseInt(schema.maxLength)) this.addError(coords, 'This value may not be longer than ' + schema.maxLength + ' characters.');
3921+
3922+
if (!valueInChoices(schema, data)) {
3923+
this.addError(coords, 'Invalid choice "' + data + '"');
3924+
return;
3925+
}
3926+
39033927
let format = normalizeKeyword(schema.format);
39043928
let format_validator;
39053929

@@ -3975,6 +3999,11 @@ function DataValidator(schema) {
39753999
if ((schema.exclusiveMinimum || schema.exclusiveMinimum === 0) && data <= schema.exclusiveMinimum) this.addError(coords, 'This value must be greater than ' + schema.exclusiveMinimum);
39764000
if ((schema.exclusiveMaximum || schema.exclusiveMaximum === 0) && data >= schema.exclusiveMaximum) this.addError(coords, 'This value must be less than ' + schema.exclusiveMaximum);
39774001
if ((schema.multipleOf || schema.multipleOf === 0) && data * 100 % (schema.multipleOf * 100) / 100) this.addError(coords, 'This value must be a multiple of ' + schema.multipleOf);
4002+
4003+
if (!valueInChoices(schema, data)) {
4004+
this.addError(coords, 'Invalid choice "' + data + '"');
4005+
return;
4006+
}
39784007
};
39794008

39804009
this.validateEmail = function (schema, data, coords) {

dist/react-json-form.module.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,18 @@ function getKey(obj, key, default_value) {
174174
let val = obj[key];
175175
return typeof val !== 'undefined' ? val : default_value;
176176
}
177+
function valueInChoices(schema, value) {
178+
/* Checks whether the given value is in schema choices or not.
179+
If schema doesn't have choices, returns true.
180+
*/
181+
let choices = getKeyword(schema, 'choices', 'enum');
182+
if (!choices) return true;
183+
let found = choices.find(choice => {
184+
if (typeof choice == 'object') choice = choice.value;
185+
return value == choice;
186+
});
187+
return found !== undefined ? true : false;
188+
}
177189
/* Set operations */
178190

179191
function isEqualset(a, b) {
@@ -317,6 +329,9 @@ function getSyncedArray(data, schema, getRef) {
317329
if (item === FILLER) item = {};
318330
newData[i] = getSyncedObject(item, schema.items, getRef);
319331
} else {
332+
// if the current value is not in choices, we reset to blank
333+
if (!valueInChoices(schema.items, newData[i])) item = FILLER;
334+
320335
if (item === FILLER) {
321336
if (type === 'integer' || type === 'number') newData[i] = schema.items.default === 0 ? 0 : schema.items.default || null;else if (type === 'boolean') newData[i] = schema.items.default === false ? false : schema.items.default || null;else newData[i] = schema.items.default || '';
322337
}
@@ -352,8 +367,11 @@ function getSyncedObject(data, schema, getRef) {
352367
if (type === 'array') newData[key] = getSyncedArray([], schemaValue, getRef);else if (type === 'object') newData[key] = getSyncedObject({}, schemaValue, getRef);else if (type === 'boolean') newData[key] = schemaValue.default === false ? false : schemaValue.default || null;else if (type === 'integer' || type === 'number') newData[key] = schemaValue.default === 0 ? 0 : schemaValue.default || null;else newData[key] = schemaValue.default || '';
353368
} else {
354369
if (type === 'array') newData[key] = getSyncedArray(data[key], schemaValue, getRef);else if (type === 'object') newData[key] = getSyncedObject(data[key], schemaValue, getRef);else {
370+
// if the current value is not in choices, we reset to blank
371+
if (!valueInChoices(schemaValue, data[key])) data[key] = '';
372+
355373
if (data[key] === '') {
356-
if (type === 'integer' || type === 'number') newData[key] = schemaValue.default === 0 ? 0 : schemaValue.default || null;else if (type === 'boolean') newData[key] = schemaValue.default === false ? false : schemaValue.default || null;
374+
if (type === 'integer' || type === 'number') newData[key] = schemaValue.default === 0 ? 0 : schemaValue.default || null;else if (type === 'boolean') newData[key] = schemaValue.default === false ? false : schemaValue.default || null;else newData[key] = schemaValue.default || '';
357375
} else {
358376
newData[key] = data[key];
359377
}
@@ -958,7 +976,7 @@ function FormSelectInput(_ref4) {
958976
}, props), /*#__PURE__*/React$1.createElement("option", {
959977
disabled: true,
960978
value: "",
961-
key: '__placehlder'
979+
key: '__placeholder'
962980
}, "Select..."), options.map((option, i) => {
963981
let title, inputValue;
964982

@@ -3900,6 +3918,12 @@ function DataValidator(schema) {
39003918
return;
39013919
if (schema.minLength && data.length < parseInt(schema.minLength)) this.addError(coords, 'This value must be at least ' + schema.minLength + ' characters long.');
39023920
if ((schema.maxLength || schema.maxLength == 0) && data.length > parseInt(schema.maxLength)) this.addError(coords, 'This value may not be longer than ' + schema.maxLength + ' characters.');
3921+
3922+
if (!valueInChoices(schema, data)) {
3923+
this.addError(coords, 'Invalid choice "' + data + '"');
3924+
return;
3925+
}
3926+
39033927
let format = normalizeKeyword(schema.format);
39043928
let format_validator;
39053929

@@ -3975,6 +3999,11 @@ function DataValidator(schema) {
39753999
if ((schema.exclusiveMinimum || schema.exclusiveMinimum === 0) && data <= schema.exclusiveMinimum) this.addError(coords, 'This value must be greater than ' + schema.exclusiveMinimum);
39764000
if ((schema.exclusiveMaximum || schema.exclusiveMaximum === 0) && data >= schema.exclusiveMaximum) this.addError(coords, 'This value must be less than ' + schema.exclusiveMaximum);
39774001
if ((schema.multipleOf || schema.multipleOf === 0) && data * 100 % (schema.multipleOf * 100) / 100) this.addError(coords, 'This value must be a multiple of ' + schema.multipleOf);
4002+
4003+
if (!valueInChoices(schema, data)) {
4004+
this.addError(coords, 'Invalid choice "' + data + '"');
4005+
return;
4006+
}
39784007
};
39794008

39804009
this.validateEmail = function (schema, data, coords) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bhch/react-json-form",
3-
"version": "2.8.2",
3+
"version": "2.8.3",
44
"description": "Create forms using JSON Schema",
55
"publishConfig": {
66
"access": "public"

0 commit comments

Comments
 (0)