tinybase 9.5.0-beta.0 → 9.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/@types/_internal/store/with-schemas/index.d.ts +43 -8
- package/@types/mergeable-store/with-schemas/index.d.ts +30 -11
- package/@types/queries/index.d.ts +11 -11
- package/@types/queries/with-schemas/index.d.ts +17 -3
- package/@types/schematizers/index.d.ts +6 -4
- package/@types/schematizers/schematizer-arktype/index.d.ts +6 -6
- package/@types/schematizers/schematizer-arktype/with-schemas/index.d.ts +112 -1
- package/@types/schematizers/schematizer-effect/index.d.ts +9 -11
- package/@types/schematizers/schematizer-effect/with-schemas/index.d.ts +114 -1
- package/@types/schematizers/schematizer-typebox/index.d.ts +7 -7
- package/@types/schematizers/schematizer-typebox/with-schemas/index.d.ts +114 -1
- package/@types/schematizers/schematizer-valibot/index.d.ts +6 -6
- package/@types/schematizers/schematizer-valibot/with-schemas/index.d.ts +112 -1
- package/@types/schematizers/schematizer-yup/index.d.ts +5 -4
- package/@types/schematizers/schematizer-yup/with-schemas/index.d.ts +112 -1
- package/@types/schematizers/schematizer-zod/index.d.ts +6 -6
- package/@types/schematizers/schematizer-zod/with-schemas/index.d.ts +112 -1
- package/@types/schematizers/with-schemas/index.d.ts +113 -4
- package/@types/store/index.d.ts +175 -16
- package/@types/store/with-schemas/index.d.ts +238 -55
- package/@types/synchronizers/with-schemas/index.d.ts +12 -2
- package/@types/ui-react/with-schemas/index.d.ts +576 -7
- package/@types/ui-react-dom/index.d.ts +10 -10
- package/@types/ui-react-dom/with-schemas/index.d.ts +94 -10
- package/@types/ui-react-dom-charts/index.d.ts +7 -7
- package/@types/ui-react-dom-charts/with-schemas/index.d.ts +7 -7
- package/@types/ui-react-inspector/index.d.ts +1 -1
- package/@types/ui-react-inspector/with-schemas/index.d.ts +1 -1
- package/@types/ui-solid/with-schemas/index.d.ts +340 -7
- package/@types/ui-solid-dom/index.d.ts +10 -10
- package/@types/ui-solid-dom/with-schemas/index.d.ts +94 -10
- package/@types/ui-solid-inspector/index.d.ts +1 -1
- package/@types/ui-solid-inspector/with-schemas/index.d.ts +1 -1
- package/@types/ui-svelte/with-schemas/index.d.ts +30 -0
- package/@types/ui-svelte-dom/index.d.ts +10 -10
- package/@types/ui-svelte-dom/with-schemas/index.d.ts +94 -10
- package/@types/ui-svelte-inspector/index.d.ts +1 -1
- package/@types/ui-svelte-inspector/with-schemas/index.d.ts +1 -1
- package/agents.md +4 -0
- package/index.js +76 -22
- package/mergeable-store/index.js +76 -22
- package/mergeable-store/with-schemas/index.js +76 -22
- package/min/index.js +1 -1
- package/min/index.js.gz +0 -0
- package/min/mergeable-store/index.js +1 -1
- package/min/mergeable-store/index.js.gz +0 -0
- package/min/mergeable-store/with-schemas/index.js +1 -1
- package/min/mergeable-store/with-schemas/index.js.gz +0 -0
- package/min/omni/index.js +1 -1
- package/min/omni/index.js.gz +0 -0
- package/min/omni/with-schemas/index.js +1 -1
- package/min/omni/with-schemas/index.js.gz +0 -0
- package/min/schematizers/index.js +1 -1
- package/min/schematizers/index.js.gz +0 -0
- package/min/schematizers/schematizer-arktype/index.js +1 -1
- package/min/schematizers/schematizer-arktype/index.js.gz +0 -0
- package/min/schematizers/schematizer-arktype/with-schemas/index.js +1 -1
- package/min/schematizers/schematizer-arktype/with-schemas/index.js.gz +0 -0
- package/min/schematizers/schematizer-effect/index.js +1 -1
- package/min/schematizers/schematizer-effect/index.js.gz +0 -0
- package/min/schematizers/schematizer-effect/with-schemas/index.js +1 -1
- package/min/schematizers/schematizer-effect/with-schemas/index.js.gz +0 -0
- package/min/schematizers/schematizer-typebox/index.js +1 -1
- package/min/schematizers/schematizer-typebox/index.js.gz +0 -0
- package/min/schematizers/schematizer-typebox/with-schemas/index.js +1 -1
- package/min/schematizers/schematizer-typebox/with-schemas/index.js.gz +0 -0
- package/min/schematizers/schematizer-valibot/index.js +1 -1
- package/min/schematizers/schematizer-valibot/index.js.gz +0 -0
- package/min/schematizers/schematizer-valibot/with-schemas/index.js +1 -1
- package/min/schematizers/schematizer-valibot/with-schemas/index.js.gz +0 -0
- package/min/schematizers/schematizer-yup/index.js +1 -1
- package/min/schematizers/schematizer-yup/index.js.gz +0 -0
- package/min/schematizers/schematizer-yup/with-schemas/index.js +1 -1
- package/min/schematizers/schematizer-yup/with-schemas/index.js.gz +0 -0
- package/min/schematizers/schematizer-zod/index.js +1 -1
- package/min/schematizers/schematizer-zod/index.js.gz +0 -0
- package/min/schematizers/schematizer-zod/with-schemas/index.js +1 -1
- package/min/schematizers/schematizer-zod/with-schemas/index.js.gz +0 -0
- package/min/schematizers/with-schemas/index.js +1 -1
- package/min/schematizers/with-schemas/index.js.gz +0 -0
- package/min/store/index.js +1 -1
- package/min/store/index.js.gz +0 -0
- package/min/store/with-schemas/index.js +1 -1
- package/min/store/with-schemas/index.js.gz +0 -0
- package/min/ui-react-inspector/index.js +1 -1
- package/min/ui-react-inspector/index.js.gz +0 -0
- package/min/ui-react-inspector/with-schemas/index.js +1 -1
- package/min/ui-react-inspector/with-schemas/index.js.gz +0 -0
- package/min/ui-solid-inspector/index.js +1 -1
- package/min/ui-solid-inspector/index.js.gz +0 -0
- package/min/ui-solid-inspector/with-schemas/index.js +1 -1
- package/min/ui-solid-inspector/with-schemas/index.js.gz +0 -0
- package/min/ui-svelte-inspector/index.js +1 -1
- package/min/ui-svelte-inspector/index.js.gz +0 -0
- package/min/ui-svelte-inspector/with-schemas/index.js +1 -1
- package/min/ui-svelte-inspector/with-schemas/index.js.gz +0 -0
- package/min/with-schemas/index.js +1 -1
- package/min/with-schemas/index.js.gz +0 -0
- package/omni/index.js +76 -22
- package/omni/with-schemas/index.js +76 -22
- package/package.json +5 -5
- package/readme.md +3 -3
- package/releases.md +32 -2
- package/schematizers/index.js +54 -6
- package/schematizers/schematizer-arktype/index.js +99 -19
- package/schematizers/schematizer-arktype/with-schemas/index.js +99 -19
- package/schematizers/schematizer-effect/index.js +101 -11
- package/schematizers/schematizer-effect/with-schemas/index.js +101 -11
- package/schematizers/schematizer-typebox/index.js +87 -11
- package/schematizers/schematizer-typebox/with-schemas/index.js +87 -11
- package/schematizers/schematizer-valibot/index.js +125 -21
- package/schematizers/schematizer-valibot/with-schemas/index.js +125 -21
- package/schematizers/schematizer-yup/index.js +58 -14
- package/schematizers/schematizer-yup/with-schemas/index.js +58 -14
- package/schematizers/schematizer-zod/index.js +131 -12
- package/schematizers/schematizer-zod/with-schemas/index.js +131 -12
- package/schematizers/with-schemas/index.js +54 -6
- package/store/index.js +76 -22
- package/store/with-schemas/index.js +76 -22
- package/ui-react-inspector/index.js +76 -22
- package/ui-react-inspector/with-schemas/index.js +76 -22
- package/ui-solid-inspector/index.js +76 -22
- package/ui-solid-inspector/with-schemas/index.js +76 -22
- package/ui-svelte/index.js +1 -1
- package/ui-svelte/with-schemas/index.js +1 -1
- package/ui-svelte-dom/index.js +2 -2
- package/ui-svelte-dom/with-schemas/index.js +2 -2
- package/ui-svelte-inspector/index.js +78 -24
- package/ui-svelte-inspector/with-schemas/index.js +78 -24
- package/with-schemas/index.js +76 -22
|
@@ -6,26 +6,36 @@ const NUMBER = getTypeOf(0);
|
|
|
6
6
|
const OBJECT = 'object';
|
|
7
7
|
const ARRAY = 'array';
|
|
8
8
|
const TYPE = 'type';
|
|
9
|
+
const ENUM = 'enum';
|
|
9
10
|
const DEFAULT = 'default';
|
|
10
11
|
const ALLOW_NULL = 'allowNull';
|
|
11
12
|
const NULL = 'null';
|
|
12
13
|
const ANY_OF = 'anyOf';
|
|
13
14
|
const REQUIRED = 'required';
|
|
15
|
+
const UNDEFINED = '\uFFFC';
|
|
16
|
+
const JSON_PREFIX = '\uFFFD';
|
|
14
17
|
|
|
15
18
|
const getIfNotFunction = (predicate) => (value, then, otherwise) =>
|
|
16
19
|
predicate(value)
|
|
17
20
|
? /* istanbul ignore next */
|
|
18
21
|
otherwise?.()
|
|
19
22
|
: then(value);
|
|
23
|
+
const isFiniteNumber = isFinite;
|
|
20
24
|
const isNullish = (thing) => thing == null;
|
|
21
25
|
const isUndefined = (thing) => thing === void 0;
|
|
26
|
+
const isNull = (thing) => thing === null;
|
|
22
27
|
const ifNotNullish = getIfNotFunction(isNullish);
|
|
23
28
|
const ifNotUndefined = getIfNotFunction(isUndefined);
|
|
29
|
+
const isTypeStringOrBoolean = (type) => type == STRING || type == BOOLEAN;
|
|
30
|
+
const isString = (thing) => getTypeOf(thing) == STRING;
|
|
31
|
+
const isArray = (thing) => Array.isArray(thing);
|
|
24
32
|
const size = (thing) => thing.length;
|
|
25
33
|
|
|
26
34
|
const arrayHas = (array, value) => array.includes(value);
|
|
35
|
+
const arrayIndexOf = (array, value) => array.indexOf(value);
|
|
27
36
|
const arrayEvery = (array, cb) => array.every(cb);
|
|
28
37
|
const arrayForEach = (array, cb) => array.forEach(cb);
|
|
38
|
+
const arrayMap = (array, cb) => array.map(cb);
|
|
29
39
|
const arrayFilter = (array, cb) => array.filter(cb);
|
|
30
40
|
const arrayFind = (array, cb) => array.find(cb);
|
|
31
41
|
|
|
@@ -63,6 +73,44 @@ const objForEach = (obj, cb) =>
|
|
|
63
73
|
const objSize = (obj) => size(objIds(obj));
|
|
64
74
|
const objIsEmpty = (obj) => isObject(obj) && objSize(obj) == 0;
|
|
65
75
|
|
|
76
|
+
const getCellOrValueType = (cellOrValue) => {
|
|
77
|
+
if (isNull(cellOrValue)) {
|
|
78
|
+
return NULL;
|
|
79
|
+
}
|
|
80
|
+
if (isArray(cellOrValue)) {
|
|
81
|
+
return ARRAY;
|
|
82
|
+
}
|
|
83
|
+
if (isObject(cellOrValue)) {
|
|
84
|
+
return OBJECT;
|
|
85
|
+
}
|
|
86
|
+
const type = getTypeOf(cellOrValue);
|
|
87
|
+
return isTypeStringOrBoolean(type) ||
|
|
88
|
+
(type == NUMBER && isFiniteNumber(cellOrValue))
|
|
89
|
+
? type
|
|
90
|
+
: void 0;
|
|
91
|
+
};
|
|
92
|
+
const isJsonType = (type) => type == OBJECT || type == ARRAY;
|
|
93
|
+
const isCellOrValueSchemaType = (type) =>
|
|
94
|
+
isTypeStringOrBoolean(type) || type == NUMBER || isJsonType(type);
|
|
95
|
+
const isCellOrValueSchemaTypes = (types) =>
|
|
96
|
+
isArray(types)
|
|
97
|
+
? !isUndefined(types[0]) && arrayEvery(types, isCellOrValueSchemaType)
|
|
98
|
+
: isCellOrValueSchemaType(types);
|
|
99
|
+
const isEncodedJson = (value) => isString(value) && value[0] == JSON_PREFIX;
|
|
100
|
+
const isReservedString = (value, encoded = 0) =>
|
|
101
|
+
value === UNDEFINED || (!encoded && isEncodedJson(value));
|
|
102
|
+
|
|
103
|
+
const getTypeOrTypeUnion = (types) => {
|
|
104
|
+
if (!arrayEvery(types, isCellOrValueSchemaType)) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const uniqueTypes = arrayFilter(
|
|
108
|
+
types,
|
|
109
|
+
(type, index) => arrayIndexOf(types, type) == index,
|
|
110
|
+
);
|
|
111
|
+
return uniqueTypes[1] ? uniqueTypes : uniqueTypes[0];
|
|
112
|
+
};
|
|
113
|
+
|
|
66
114
|
const createCustomSchematizer = (
|
|
67
115
|
unwrapSchema,
|
|
68
116
|
getProperties,
|
|
@@ -72,16 +120,27 @@ const createCustomSchematizer = (
|
|
|
72
120
|
const [unwrapped, defaultValue, allowNull, unwrappedRequired = required] =
|
|
73
121
|
unwrapSchema(schema, void 0, void 0, required);
|
|
74
122
|
const type = unwrapped?.type;
|
|
123
|
+
const enumValues = unwrapped?.[ENUM];
|
|
124
|
+
const isEnum =
|
|
125
|
+
isUndefined(type) &&
|
|
126
|
+
isArray(enumValues) &&
|
|
127
|
+
!isUndefined(enumValues[0]) &&
|
|
128
|
+
arrayEvery(enumValues, (enumValue) => {
|
|
129
|
+
const enumType = getCellOrValueType(enumValue);
|
|
130
|
+
return (
|
|
131
|
+
!isNull(enumValue) &&
|
|
132
|
+
!isUndefined(enumType) &&
|
|
133
|
+
!isJsonType(enumType) &&
|
|
134
|
+
!isReservedString(enumValue)
|
|
135
|
+
);
|
|
136
|
+
});
|
|
75
137
|
if (
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
type !== BOOLEAN &&
|
|
79
|
-
type !== OBJECT &&
|
|
80
|
-
type !== ARRAY
|
|
138
|
+
!isEnum &&
|
|
139
|
+
(!isUndefined(enumValues) || !isCellOrValueSchemaTypes(type))
|
|
81
140
|
) {
|
|
82
141
|
return void 0;
|
|
83
142
|
}
|
|
84
|
-
const cellOrValueSchema = {[TYPE]: type};
|
|
143
|
+
const cellOrValueSchema = isEnum ? {[ENUM]: enumValues} : {[TYPE]: type};
|
|
85
144
|
ifNotUndefined(defaultValue, (defaultValue2) => {
|
|
86
145
|
cellOrValueSchema[DEFAULT] = defaultValue2;
|
|
87
146
|
});
|
|
@@ -132,6 +191,7 @@ const createCustomSchematizer = (
|
|
|
132
191
|
};
|
|
133
192
|
|
|
134
193
|
const TYPEBOX_OPTIONAL = 'Symbol(TypeBox.Optional)';
|
|
194
|
+
const CONST = 'const';
|
|
135
195
|
const isTypeBoxOptional = (schema) =>
|
|
136
196
|
!isUndefined(
|
|
137
197
|
arrayFind(
|
|
@@ -152,15 +212,31 @@ const unwrapSchema = (
|
|
|
152
212
|
const firstNonNullType = nonNullTypes[0];
|
|
153
213
|
if (
|
|
154
214
|
firstNonNullType &&
|
|
155
|
-
arrayEvery(nonNullTypes, (type) => type?.
|
|
215
|
+
arrayEvery(nonNullTypes, (type) => !isUndefined(type?.[CONST]))
|
|
156
216
|
) {
|
|
157
|
-
return
|
|
158
|
-
|
|
217
|
+
return [
|
|
218
|
+
{[ENUM]: arrayMap(nonNullTypes, (type) => type[CONST])},
|
|
159
219
|
defaultValue ?? schema?.[DEFAULT],
|
|
160
|
-
hasNull || allowNull,
|
|
220
|
+
hasNull || (allowNull ?? false),
|
|
161
221
|
required,
|
|
162
|
-
|
|
222
|
+
];
|
|
163
223
|
}
|
|
224
|
+
return [
|
|
225
|
+
{
|
|
226
|
+
type: getTypeOrTypeUnion(arrayMap(nonNullTypes, (type) => type?.type)),
|
|
227
|
+
},
|
|
228
|
+
defaultValue ?? schema?.[DEFAULT],
|
|
229
|
+
hasNull || (allowNull ?? false),
|
|
230
|
+
required,
|
|
231
|
+
];
|
|
232
|
+
}
|
|
233
|
+
if (!isUndefined(schema?.[CONST])) {
|
|
234
|
+
return [
|
|
235
|
+
{[ENUM]: [schema[CONST]]},
|
|
236
|
+
defaultValue ?? schema?.[DEFAULT],
|
|
237
|
+
allowNull ?? false,
|
|
238
|
+
required,
|
|
239
|
+
];
|
|
164
240
|
}
|
|
165
241
|
return [
|
|
166
242
|
schema,
|
|
@@ -6,29 +6,40 @@ const NUMBER = getTypeOf(0);
|
|
|
6
6
|
const OBJECT = 'object';
|
|
7
7
|
const ARRAY = 'array';
|
|
8
8
|
const TYPE = 'type';
|
|
9
|
+
const ENUM = 'enum';
|
|
9
10
|
const DEFAULT = 'default';
|
|
10
11
|
const ALLOW_NULL = 'allowNull';
|
|
12
|
+
const NULL = 'null';
|
|
11
13
|
const FALLBACK = 'fallback';
|
|
12
14
|
const NULLABLE = 'nullable';
|
|
13
15
|
const OPTIONAL = 'optional';
|
|
14
16
|
const REQUIRED = 'required';
|
|
15
17
|
const WRAPPED = 'wrapped';
|
|
16
18
|
const RECORD = 'record';
|
|
19
|
+
const UNDEFINED = '\uFFFC';
|
|
20
|
+
const JSON_PREFIX = '\uFFFD';
|
|
17
21
|
|
|
18
22
|
const getIfNotFunction = (predicate) => (value, then, otherwise) =>
|
|
19
23
|
predicate(value)
|
|
20
24
|
? /* istanbul ignore next */
|
|
21
25
|
otherwise?.()
|
|
22
26
|
: then(value);
|
|
27
|
+
const isFiniteNumber = isFinite;
|
|
23
28
|
const isNullish = (thing) => thing == null;
|
|
24
29
|
const isUndefined = (thing) => thing === void 0;
|
|
30
|
+
const isNull = (thing) => thing === null;
|
|
25
31
|
const ifNotNullish = getIfNotFunction(isNullish);
|
|
26
32
|
const ifNotUndefined = getIfNotFunction(isUndefined);
|
|
33
|
+
const isTypeStringOrBoolean = (type) => type == STRING || type == BOOLEAN;
|
|
27
34
|
const isString = (thing) => getTypeOf(thing) == STRING;
|
|
35
|
+
const isArray = (thing) => Array.isArray(thing);
|
|
28
36
|
const size = (thing) => thing.length;
|
|
29
37
|
|
|
38
|
+
const arrayIndexOf = (array, value) => array.indexOf(value);
|
|
30
39
|
const arrayEvery = (array, cb) => array.every(cb);
|
|
31
40
|
const arrayForEach = (array, cb) => array.forEach(cb);
|
|
41
|
+
const arrayFilter = (array, cb) => array.filter(cb);
|
|
42
|
+
const arrayPush = (array, ...values) => array.push(...values);
|
|
32
43
|
|
|
33
44
|
const object = Object;
|
|
34
45
|
const getPrototypeOf = (obj) => object.getPrototypeOf(obj);
|
|
@@ -64,6 +75,44 @@ const objForEach = (obj, cb) =>
|
|
|
64
75
|
const objSize = (obj) => size(objIds(obj));
|
|
65
76
|
const objIsEmpty = (obj) => isObject(obj) && objSize(obj) == 0;
|
|
66
77
|
|
|
78
|
+
const getCellOrValueType = (cellOrValue) => {
|
|
79
|
+
if (isNull(cellOrValue)) {
|
|
80
|
+
return NULL;
|
|
81
|
+
}
|
|
82
|
+
if (isArray(cellOrValue)) {
|
|
83
|
+
return ARRAY;
|
|
84
|
+
}
|
|
85
|
+
if (isObject(cellOrValue)) {
|
|
86
|
+
return OBJECT;
|
|
87
|
+
}
|
|
88
|
+
const type = getTypeOf(cellOrValue);
|
|
89
|
+
return isTypeStringOrBoolean(type) ||
|
|
90
|
+
(type == NUMBER && isFiniteNumber(cellOrValue))
|
|
91
|
+
? type
|
|
92
|
+
: void 0;
|
|
93
|
+
};
|
|
94
|
+
const isJsonType = (type) => type == OBJECT || type == ARRAY;
|
|
95
|
+
const isCellOrValueSchemaType = (type) =>
|
|
96
|
+
isTypeStringOrBoolean(type) || type == NUMBER || isJsonType(type);
|
|
97
|
+
const isCellOrValueSchemaTypes = (types) =>
|
|
98
|
+
isArray(types)
|
|
99
|
+
? !isUndefined(types[0]) && arrayEvery(types, isCellOrValueSchemaType)
|
|
100
|
+
: isCellOrValueSchemaType(types);
|
|
101
|
+
const isEncodedJson = (value) => isString(value) && value[0] == JSON_PREFIX;
|
|
102
|
+
const isReservedString = (value, encoded = 0) =>
|
|
103
|
+
value === UNDEFINED || (!encoded && isEncodedJson(value));
|
|
104
|
+
|
|
105
|
+
const getTypeOrTypeUnion = (types) => {
|
|
106
|
+
if (!arrayEvery(types, isCellOrValueSchemaType)) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const uniqueTypes = arrayFilter(
|
|
110
|
+
types,
|
|
111
|
+
(type, index) => arrayIndexOf(types, type) == index,
|
|
112
|
+
);
|
|
113
|
+
return uniqueTypes[1] ? uniqueTypes : uniqueTypes[0];
|
|
114
|
+
};
|
|
115
|
+
|
|
67
116
|
const createCustomSchematizer = (
|
|
68
117
|
unwrapSchema,
|
|
69
118
|
getProperties,
|
|
@@ -73,16 +122,27 @@ const createCustomSchematizer = (
|
|
|
73
122
|
const [unwrapped, defaultValue, allowNull, unwrappedRequired = required] =
|
|
74
123
|
unwrapSchema(schema, void 0, void 0, required);
|
|
75
124
|
const type = unwrapped?.type;
|
|
125
|
+
const enumValues = unwrapped?.[ENUM];
|
|
126
|
+
const isEnum =
|
|
127
|
+
isUndefined(type) &&
|
|
128
|
+
isArray(enumValues) &&
|
|
129
|
+
!isUndefined(enumValues[0]) &&
|
|
130
|
+
arrayEvery(enumValues, (enumValue) => {
|
|
131
|
+
const enumType = getCellOrValueType(enumValue);
|
|
132
|
+
return (
|
|
133
|
+
!isNull(enumValue) &&
|
|
134
|
+
!isUndefined(enumType) &&
|
|
135
|
+
!isJsonType(enumType) &&
|
|
136
|
+
!isReservedString(enumValue)
|
|
137
|
+
);
|
|
138
|
+
});
|
|
76
139
|
if (
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
type !== BOOLEAN &&
|
|
80
|
-
type !== OBJECT &&
|
|
81
|
-
type !== ARRAY
|
|
140
|
+
!isEnum &&
|
|
141
|
+
(!isUndefined(enumValues) || !isCellOrValueSchemaTypes(type))
|
|
82
142
|
) {
|
|
83
143
|
return void 0;
|
|
84
144
|
}
|
|
85
|
-
const cellOrValueSchema = {[TYPE]: type};
|
|
145
|
+
const cellOrValueSchema = isEnum ? {[ENUM]: enumValues} : {[TYPE]: type};
|
|
86
146
|
ifNotUndefined(defaultValue, (defaultValue2) => {
|
|
87
147
|
cellOrValueSchema[DEFAULT] = defaultValue2;
|
|
88
148
|
});
|
|
@@ -134,6 +194,7 @@ const createCustomSchematizer = (
|
|
|
134
194
|
|
|
135
195
|
const LITERAL = 'literal';
|
|
136
196
|
const PICKLIST = 'picklist';
|
|
197
|
+
const VALIBOT_UNION = 'union';
|
|
137
198
|
const unwrapSchema = (schema, defaultValue, allowNull, required = true) => {
|
|
138
199
|
const type = schema?.type;
|
|
139
200
|
return type === OPTIONAL
|
|
@@ -152,26 +213,69 @@ const unwrapSchema = (schema, defaultValue, allowNull, required = true) => {
|
|
|
152
213
|
allowNull ?? false,
|
|
153
214
|
required,
|
|
154
215
|
]
|
|
155
|
-
: type === PICKLIST
|
|
216
|
+
: type === PICKLIST
|
|
156
217
|
? [
|
|
157
|
-
{[
|
|
218
|
+
{[ENUM]: schema.options},
|
|
158
219
|
defaultValue ?? schema?.[FALLBACK],
|
|
159
220
|
allowNull ?? false,
|
|
160
221
|
required,
|
|
161
222
|
]
|
|
162
|
-
: type ===
|
|
163
|
-
?
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
223
|
+
: type === VALIBOT_UNION
|
|
224
|
+
? unwrapUnion(schema, defaultValue, allowNull, required)
|
|
225
|
+
: type === LITERAL
|
|
226
|
+
? [
|
|
227
|
+
{[ENUM]: [schema.literal]},
|
|
228
|
+
defaultValue ?? schema?.[FALLBACK],
|
|
229
|
+
allowNull ?? false,
|
|
230
|
+
required,
|
|
231
|
+
]
|
|
232
|
+
: [
|
|
233
|
+
schema,
|
|
234
|
+
defaultValue ?? schema?.[FALLBACK],
|
|
235
|
+
allowNull ?? false,
|
|
236
|
+
required,
|
|
237
|
+
];
|
|
238
|
+
};
|
|
239
|
+
const unwrapUnion = (schema, defaultValue, allowNull, required = true) => {
|
|
240
|
+
const nonNullOptions = arrayFilter(
|
|
241
|
+
schema.options,
|
|
242
|
+
(option) => option.type !== NULL,
|
|
243
|
+
);
|
|
244
|
+
const hasNull = nonNullOptions.length !== schema.options.length;
|
|
245
|
+
const enumValues = [];
|
|
246
|
+
const allEnums = arrayEvery(nonNullOptions, (option) => {
|
|
247
|
+
const values =
|
|
248
|
+
option.type === LITERAL
|
|
249
|
+
? [option.literal]
|
|
250
|
+
: option.type === PICKLIST
|
|
251
|
+
? option.options
|
|
252
|
+
: void 0;
|
|
253
|
+
if (isUndefined(values)) {
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
arrayForEach(values, (value) => arrayPush(enumValues, value));
|
|
257
|
+
return true;
|
|
258
|
+
});
|
|
259
|
+
const types = [];
|
|
260
|
+
if (!allEnums) {
|
|
261
|
+
arrayForEach(nonNullOptions, (option) => {
|
|
262
|
+
if (option.type === LITERAL) {
|
|
263
|
+
arrayPush(types, getCellOrValueType(option.literal));
|
|
264
|
+
} else if (option.type === PICKLIST) {
|
|
265
|
+
arrayForEach(option.options, (value) =>
|
|
266
|
+
arrayPush(types, getCellOrValueType(value)),
|
|
267
|
+
);
|
|
268
|
+
} else {
|
|
269
|
+
arrayPush(types, option.type === RECORD ? OBJECT : option.type);
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
return [
|
|
274
|
+
allEnums ? {[ENUM]: enumValues} : {type: getTypeOrTypeUnion(types)},
|
|
275
|
+
defaultValue ?? schema?.[FALLBACK],
|
|
276
|
+
allowNull || hasNull,
|
|
277
|
+
required,
|
|
278
|
+
];
|
|
175
279
|
};
|
|
176
280
|
const getProperties = (schema) => schema?.entries;
|
|
177
281
|
const createValibotSchematizer = () =>
|
|
@@ -6,29 +6,40 @@ const NUMBER = getTypeOf(0);
|
|
|
6
6
|
const OBJECT = 'object';
|
|
7
7
|
const ARRAY = 'array';
|
|
8
8
|
const TYPE = 'type';
|
|
9
|
+
const ENUM = 'enum';
|
|
9
10
|
const DEFAULT = 'default';
|
|
10
11
|
const ALLOW_NULL = 'allowNull';
|
|
12
|
+
const NULL = 'null';
|
|
11
13
|
const FALLBACK = 'fallback';
|
|
12
14
|
const NULLABLE = 'nullable';
|
|
13
15
|
const OPTIONAL = 'optional';
|
|
14
16
|
const REQUIRED = 'required';
|
|
15
17
|
const WRAPPED = 'wrapped';
|
|
16
18
|
const RECORD = 'record';
|
|
19
|
+
const UNDEFINED = '\uFFFC';
|
|
20
|
+
const JSON_PREFIX = '\uFFFD';
|
|
17
21
|
|
|
18
22
|
const getIfNotFunction = (predicate) => (value, then, otherwise) =>
|
|
19
23
|
predicate(value)
|
|
20
24
|
? /* istanbul ignore next */
|
|
21
25
|
otherwise?.()
|
|
22
26
|
: then(value);
|
|
27
|
+
const isFiniteNumber = isFinite;
|
|
23
28
|
const isNullish = (thing) => thing == null;
|
|
24
29
|
const isUndefined = (thing) => thing === void 0;
|
|
30
|
+
const isNull = (thing) => thing === null;
|
|
25
31
|
const ifNotNullish = getIfNotFunction(isNullish);
|
|
26
32
|
const ifNotUndefined = getIfNotFunction(isUndefined);
|
|
33
|
+
const isTypeStringOrBoolean = (type) => type == STRING || type == BOOLEAN;
|
|
27
34
|
const isString = (thing) => getTypeOf(thing) == STRING;
|
|
35
|
+
const isArray = (thing) => Array.isArray(thing);
|
|
28
36
|
const size = (thing) => thing.length;
|
|
29
37
|
|
|
38
|
+
const arrayIndexOf = (array, value) => array.indexOf(value);
|
|
30
39
|
const arrayEvery = (array, cb) => array.every(cb);
|
|
31
40
|
const arrayForEach = (array, cb) => array.forEach(cb);
|
|
41
|
+
const arrayFilter = (array, cb) => array.filter(cb);
|
|
42
|
+
const arrayPush = (array, ...values) => array.push(...values);
|
|
32
43
|
|
|
33
44
|
const object = Object;
|
|
34
45
|
const getPrototypeOf = (obj) => object.getPrototypeOf(obj);
|
|
@@ -64,6 +75,44 @@ const objForEach = (obj, cb) =>
|
|
|
64
75
|
const objSize = (obj) => size(objIds(obj));
|
|
65
76
|
const objIsEmpty = (obj) => isObject(obj) && objSize(obj) == 0;
|
|
66
77
|
|
|
78
|
+
const getCellOrValueType = (cellOrValue) => {
|
|
79
|
+
if (isNull(cellOrValue)) {
|
|
80
|
+
return NULL;
|
|
81
|
+
}
|
|
82
|
+
if (isArray(cellOrValue)) {
|
|
83
|
+
return ARRAY;
|
|
84
|
+
}
|
|
85
|
+
if (isObject(cellOrValue)) {
|
|
86
|
+
return OBJECT;
|
|
87
|
+
}
|
|
88
|
+
const type = getTypeOf(cellOrValue);
|
|
89
|
+
return isTypeStringOrBoolean(type) ||
|
|
90
|
+
(type == NUMBER && isFiniteNumber(cellOrValue))
|
|
91
|
+
? type
|
|
92
|
+
: void 0;
|
|
93
|
+
};
|
|
94
|
+
const isJsonType = (type) => type == OBJECT || type == ARRAY;
|
|
95
|
+
const isCellOrValueSchemaType = (type) =>
|
|
96
|
+
isTypeStringOrBoolean(type) || type == NUMBER || isJsonType(type);
|
|
97
|
+
const isCellOrValueSchemaTypes = (types) =>
|
|
98
|
+
isArray(types)
|
|
99
|
+
? !isUndefined(types[0]) && arrayEvery(types, isCellOrValueSchemaType)
|
|
100
|
+
: isCellOrValueSchemaType(types);
|
|
101
|
+
const isEncodedJson = (value) => isString(value) && value[0] == JSON_PREFIX;
|
|
102
|
+
const isReservedString = (value, encoded = 0) =>
|
|
103
|
+
value === UNDEFINED || (!encoded && isEncodedJson(value));
|
|
104
|
+
|
|
105
|
+
const getTypeOrTypeUnion = (types) => {
|
|
106
|
+
if (!arrayEvery(types, isCellOrValueSchemaType)) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const uniqueTypes = arrayFilter(
|
|
110
|
+
types,
|
|
111
|
+
(type, index) => arrayIndexOf(types, type) == index,
|
|
112
|
+
);
|
|
113
|
+
return uniqueTypes[1] ? uniqueTypes : uniqueTypes[0];
|
|
114
|
+
};
|
|
115
|
+
|
|
67
116
|
const createCustomSchematizer = (
|
|
68
117
|
unwrapSchema,
|
|
69
118
|
getProperties,
|
|
@@ -73,16 +122,27 @@ const createCustomSchematizer = (
|
|
|
73
122
|
const [unwrapped, defaultValue, allowNull, unwrappedRequired = required] =
|
|
74
123
|
unwrapSchema(schema, void 0, void 0, required);
|
|
75
124
|
const type = unwrapped?.type;
|
|
125
|
+
const enumValues = unwrapped?.[ENUM];
|
|
126
|
+
const isEnum =
|
|
127
|
+
isUndefined(type) &&
|
|
128
|
+
isArray(enumValues) &&
|
|
129
|
+
!isUndefined(enumValues[0]) &&
|
|
130
|
+
arrayEvery(enumValues, (enumValue) => {
|
|
131
|
+
const enumType = getCellOrValueType(enumValue);
|
|
132
|
+
return (
|
|
133
|
+
!isNull(enumValue) &&
|
|
134
|
+
!isUndefined(enumType) &&
|
|
135
|
+
!isJsonType(enumType) &&
|
|
136
|
+
!isReservedString(enumValue)
|
|
137
|
+
);
|
|
138
|
+
});
|
|
76
139
|
if (
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
type !== BOOLEAN &&
|
|
80
|
-
type !== OBJECT &&
|
|
81
|
-
type !== ARRAY
|
|
140
|
+
!isEnum &&
|
|
141
|
+
(!isUndefined(enumValues) || !isCellOrValueSchemaTypes(type))
|
|
82
142
|
) {
|
|
83
143
|
return void 0;
|
|
84
144
|
}
|
|
85
|
-
const cellOrValueSchema = {[TYPE]: type};
|
|
145
|
+
const cellOrValueSchema = isEnum ? {[ENUM]: enumValues} : {[TYPE]: type};
|
|
86
146
|
ifNotUndefined(defaultValue, (defaultValue2) => {
|
|
87
147
|
cellOrValueSchema[DEFAULT] = defaultValue2;
|
|
88
148
|
});
|
|
@@ -134,6 +194,7 @@ const createCustomSchematizer = (
|
|
|
134
194
|
|
|
135
195
|
const LITERAL = 'literal';
|
|
136
196
|
const PICKLIST = 'picklist';
|
|
197
|
+
const VALIBOT_UNION = 'union';
|
|
137
198
|
const unwrapSchema = (schema, defaultValue, allowNull, required = true) => {
|
|
138
199
|
const type = schema?.type;
|
|
139
200
|
return type === OPTIONAL
|
|
@@ -152,26 +213,69 @@ const unwrapSchema = (schema, defaultValue, allowNull, required = true) => {
|
|
|
152
213
|
allowNull ?? false,
|
|
153
214
|
required,
|
|
154
215
|
]
|
|
155
|
-
: type === PICKLIST
|
|
216
|
+
: type === PICKLIST
|
|
156
217
|
? [
|
|
157
|
-
{[
|
|
218
|
+
{[ENUM]: schema.options},
|
|
158
219
|
defaultValue ?? schema?.[FALLBACK],
|
|
159
220
|
allowNull ?? false,
|
|
160
221
|
required,
|
|
161
222
|
]
|
|
162
|
-
: type ===
|
|
163
|
-
?
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
223
|
+
: type === VALIBOT_UNION
|
|
224
|
+
? unwrapUnion(schema, defaultValue, allowNull, required)
|
|
225
|
+
: type === LITERAL
|
|
226
|
+
? [
|
|
227
|
+
{[ENUM]: [schema.literal]},
|
|
228
|
+
defaultValue ?? schema?.[FALLBACK],
|
|
229
|
+
allowNull ?? false,
|
|
230
|
+
required,
|
|
231
|
+
]
|
|
232
|
+
: [
|
|
233
|
+
schema,
|
|
234
|
+
defaultValue ?? schema?.[FALLBACK],
|
|
235
|
+
allowNull ?? false,
|
|
236
|
+
required,
|
|
237
|
+
];
|
|
238
|
+
};
|
|
239
|
+
const unwrapUnion = (schema, defaultValue, allowNull, required = true) => {
|
|
240
|
+
const nonNullOptions = arrayFilter(
|
|
241
|
+
schema.options,
|
|
242
|
+
(option) => option.type !== NULL,
|
|
243
|
+
);
|
|
244
|
+
const hasNull = nonNullOptions.length !== schema.options.length;
|
|
245
|
+
const enumValues = [];
|
|
246
|
+
const allEnums = arrayEvery(nonNullOptions, (option) => {
|
|
247
|
+
const values =
|
|
248
|
+
option.type === LITERAL
|
|
249
|
+
? [option.literal]
|
|
250
|
+
: option.type === PICKLIST
|
|
251
|
+
? option.options
|
|
252
|
+
: void 0;
|
|
253
|
+
if (isUndefined(values)) {
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
arrayForEach(values, (value) => arrayPush(enumValues, value));
|
|
257
|
+
return true;
|
|
258
|
+
});
|
|
259
|
+
const types = [];
|
|
260
|
+
if (!allEnums) {
|
|
261
|
+
arrayForEach(nonNullOptions, (option) => {
|
|
262
|
+
if (option.type === LITERAL) {
|
|
263
|
+
arrayPush(types, getCellOrValueType(option.literal));
|
|
264
|
+
} else if (option.type === PICKLIST) {
|
|
265
|
+
arrayForEach(option.options, (value) =>
|
|
266
|
+
arrayPush(types, getCellOrValueType(value)),
|
|
267
|
+
);
|
|
268
|
+
} else {
|
|
269
|
+
arrayPush(types, option.type === RECORD ? OBJECT : option.type);
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
return [
|
|
274
|
+
allEnums ? {[ENUM]: enumValues} : {type: getTypeOrTypeUnion(types)},
|
|
275
|
+
defaultValue ?? schema?.[FALLBACK],
|
|
276
|
+
allowNull || hasNull,
|
|
277
|
+
required,
|
|
278
|
+
];
|
|
175
279
|
};
|
|
176
280
|
const getProperties = (schema) => schema?.entries;
|
|
177
281
|
const createValibotSchematizer = () =>
|