-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.mjs
37 lines (31 loc) · 819 Bytes
/
schema.mjs
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
import { LET_BE_FREE, NOT_NULLABLE } from './symbols.mjs';
import { KeySchema } from './keySchema.mjs';
const _getEmptyObject = () => ({});
const _mapObjToNewObj = (obj, reducerFn) => Object
.keys(obj)
.reduce(
reducerFn,
_getEmptyObject()
)
export const updateSchema = (obj, schema, key) => {
const value = obj[key];
if (key in schema) {
schema[key].value = (value instanceof KeySchema) ? value.value : value;
} else if (value instanceof KeySchema) {
schema[key] = value;
} else {
schema[key] = new KeySchema(value);
}
return schema
};
export const createSchema = (obj) => _mapObjToNewObj(
obj,
updateSchema.bind(null, obj),
);
export const getValuesFromSchema = (schema) => _mapObjToNewObj(
schema,
(obj, key) => ({
...obj,
[key]: schema[key].value
})
);