Skip to content

Commit

Permalink
add support for array/object/lists/maps in enum validator #158
Browse files Browse the repository at this point in the history
  • Loading branch information
elbakerino committed Dec 30, 2021
1 parent 6c16f5f commit 1019f4d
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 10 deletions.
2 changes: 0 additions & 2 deletions packages/demo/src/material-ui/material-ui-custom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ const storeKeys = List()
const WidgetTextField = applyPluginStack(StringRenderer)

const FileUpload: React.ComponentType<WidgetProps & WithValue> = ({storeKeys, onChange, schema, required, value}) => {
console.log('value', value && value.toJS ? value.toJS() : value)
return <div>
<input type={'file'} onChange={e => {
const formData = new FormData()
Expand Down Expand Up @@ -177,7 +176,6 @@ const CountrySelect: React.ComponentType<WidgetProps<{}, MuiWidgetBinding> & Wit
fetch('https://restcountries.com/v3.1/subregion/europe', {method: 'GET'})
.then(r => r.json())
.then(data => {
console.log(data)
setCountries(List(data.map((d: { name: { common: string } }) => d.name.common)))
setLoading(PROGRESS_DONE)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OrderedMap, List } from 'immutable'
import { OrderedMap, List, Map } from 'immutable'
import {
validateEnum, valueValidatorEnum, ERROR_ENUM_MISMATCH,
} from '@ui-schema/ui-schema/Validators/ValueValidator'
Expand Down Expand Up @@ -102,8 +102,86 @@ describe('validateEnum', () => {
[],
[],
false,
], [
List([List([])]),
[],
true,
], [
[[]],
[],
true,
], [
List([List(['a', 'b']), List([1, 2])]),
['a', 'b'],
true,
], [
List([List(['a', 'b']), List([1, 2])]),
['b', 'a'],
false,
], [
List([List(['a', 'b']), List([1, 2])]),
[1, 2],
true,
], [
List([List(['a', 'b']), List([1, 2])]),
[2, 1],
false,
], [
[['a', 'b']],
['a', 'b'],
true,
], [
[['a', 'b']],
['b', 'a'],
false,
], [
[{}],
{},
true,
], [
[],
{},
false,
], [
[{a: 1}, {b: 2}],
{a: 1},
true,
], [
[{a: 1}, {b: 2}],
{b: 2},
true,
], [
List([OrderedMap({a: 1, b: 2}), Map({c: 3, d: 4})]),
OrderedMap({a: 1, b: 2}),
true,
], [
// note: even when the order is correct, an `OrderedMap` can be the same as a `Map`
List([OrderedMap({a: 1, b: 2}), Map({c: 3, d: 4})]),
OrderedMap({c: 3, d: 4}),
false,
], [
List([OrderedMap({a: 1, b: 2}), Map({c: 3, d: 4})]),
OrderedMap({d: 4, c: 3}),
false,
], [
List([OrderedMap({a: 1, b: 2}), Map({c: 3, d: 4})]),
Map({c: 3, d: 4}),
true,
], [
// note: even when the order is correct, an `Map` can be the same as a `OrderedMap`
List([OrderedMap({a: 1, b: 2}), Map({c: 3, d: 4})]),
Map({a: 1, b: 2}),
false,
], [
List([OrderedMap({a: 1, b: 2}), Map({c: 3, d: 4})]),
OrderedMap({b: 2, a: 1}),
false,
], [
List([OrderedMap({a: 1, b: 2}), Map({c: 3, d: 4})]),
Map({d: 4, c: 3}),
true,
],
])('validateEnum(%j, %j, %j): %j', (_enum: any, value: any, expected: boolean) => {
])('validateEnum(%j, %j): %j', (_enum: any, value: any, expected: boolean) => {
expect(validateEnum(_enum, value)).toBe(expected)
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
import {List, Map} from 'immutable';
import {fromJS, List, Map} from 'immutable';

export const ERROR_ENUM_MISMATCH = 'enum-mismatch';

export const validateEnum = (_enum, value) => {
if(typeof _enum === 'undefined' || typeof value === 'undefined') return true;

if(Array.isArray(_enum)) {
_enum = List(fromJS(_enum))
}

if(value !== null && typeof value === 'object') {
if(Array.isArray(value)) {
value = List(fromJS(value))
} else if(!List.isList(value) && !Map.isMap(value)) {
value = Map(fromJS(value))
}
if(List.isList(value) || Map.isMap(value)) {
for(const enm of _enum) {
if(value.equals(enm)) {
return true
}
}
return false
}
}

if(List.isList(_enum)) {
if(!_enum.contains(value)) {
return false;
}
} else if(Array.isArray(_enum)) {
if(-1 === _enum.indexOf(value)) {
return false;
}
}

// todo: add missing `array`/`object` compare
return true;
};

Expand Down

0 comments on commit 1019f4d

Please sign in to comment.