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,27 +6,37 @@ 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 NULLABLE = 'nullable';
|
|
12
14
|
const OPTIONAL = 'optional';
|
|
13
15
|
const REQUIRED = 'required';
|
|
16
|
+
const UNDEFINED = '\uFFFC';
|
|
17
|
+
const JSON_PREFIX = '\uFFFD';
|
|
14
18
|
|
|
15
19
|
const getIfNotFunction = (predicate) => (value, then, otherwise) =>
|
|
16
20
|
predicate(value)
|
|
17
21
|
? /* istanbul ignore next */
|
|
18
22
|
otherwise?.()
|
|
19
23
|
: then(value);
|
|
24
|
+
const isFiniteNumber = isFinite;
|
|
20
25
|
const isNullish = (thing) => thing == null;
|
|
21
26
|
const isUndefined = (thing) => thing === void 0;
|
|
27
|
+
const isNull = (thing) => thing === null;
|
|
22
28
|
const ifNotNullish = getIfNotFunction(isNullish);
|
|
23
29
|
const ifNotUndefined = getIfNotFunction(isUndefined);
|
|
30
|
+
const isTypeStringOrBoolean = (type) => type == STRING || type == BOOLEAN;
|
|
24
31
|
const isString = (thing) => getTypeOf(thing) == STRING;
|
|
32
|
+
const isArray = (thing) => Array.isArray(thing);
|
|
25
33
|
const size = (thing) => thing.length;
|
|
26
34
|
const isEmpty = (thing) => size(thing) == 0;
|
|
27
35
|
|
|
28
36
|
const arrayEvery = (array, cb) => array.every(cb);
|
|
29
37
|
const arrayForEach = (array, cb) => array.forEach(cb);
|
|
38
|
+
const arrayFilter = (array, cb) => array.filter(cb);
|
|
39
|
+
const arrayFind = (array, cb) => array.find(cb);
|
|
30
40
|
|
|
31
41
|
const collValues = (coll) => [...(coll?.values() ?? [])];
|
|
32
42
|
|
|
@@ -64,6 +74,33 @@ const objForEach = (obj, cb) =>
|
|
|
64
74
|
const objSize = (obj) => size(objIds(obj));
|
|
65
75
|
const objIsEmpty = (obj) => isObject(obj) && objSize(obj) == 0;
|
|
66
76
|
|
|
77
|
+
const getCellOrValueType = (cellOrValue) => {
|
|
78
|
+
if (isNull(cellOrValue)) {
|
|
79
|
+
return NULL;
|
|
80
|
+
}
|
|
81
|
+
if (isArray(cellOrValue)) {
|
|
82
|
+
return ARRAY;
|
|
83
|
+
}
|
|
84
|
+
if (isObject(cellOrValue)) {
|
|
85
|
+
return OBJECT;
|
|
86
|
+
}
|
|
87
|
+
const type = getTypeOf(cellOrValue);
|
|
88
|
+
return isTypeStringOrBoolean(type) ||
|
|
89
|
+
(type == NUMBER && isFiniteNumber(cellOrValue))
|
|
90
|
+
? type
|
|
91
|
+
: void 0;
|
|
92
|
+
};
|
|
93
|
+
const isJsonType = (type) => type == OBJECT || type == ARRAY;
|
|
94
|
+
const isCellOrValueSchemaType = (type) =>
|
|
95
|
+
isTypeStringOrBoolean(type) || type == NUMBER || isJsonType(type);
|
|
96
|
+
const isCellOrValueSchemaTypes = (types) =>
|
|
97
|
+
isArray(types)
|
|
98
|
+
? !isUndefined(types[0]) && arrayEvery(types, isCellOrValueSchemaType)
|
|
99
|
+
: isCellOrValueSchemaType(types);
|
|
100
|
+
const isEncodedJson = (value) => isString(value) && value[0] == JSON_PREFIX;
|
|
101
|
+
const isReservedString = (value, encoded = 0) =>
|
|
102
|
+
value === UNDEFINED || (!encoded && isEncodedJson(value));
|
|
103
|
+
|
|
67
104
|
const createCustomSchematizer = (
|
|
68
105
|
unwrapSchema,
|
|
69
106
|
getProperties,
|
|
@@ -73,16 +110,27 @@ const createCustomSchematizer = (
|
|
|
73
110
|
const [unwrapped, defaultValue, allowNull, unwrappedRequired = required] =
|
|
74
111
|
unwrapSchema(schema, void 0, void 0, required);
|
|
75
112
|
const type = unwrapped?.type;
|
|
113
|
+
const enumValues = unwrapped?.[ENUM];
|
|
114
|
+
const isEnum =
|
|
115
|
+
isUndefined(type) &&
|
|
116
|
+
isArray(enumValues) &&
|
|
117
|
+
!isUndefined(enumValues[0]) &&
|
|
118
|
+
arrayEvery(enumValues, (enumValue) => {
|
|
119
|
+
const enumType = getCellOrValueType(enumValue);
|
|
120
|
+
return (
|
|
121
|
+
!isNull(enumValue) &&
|
|
122
|
+
!isUndefined(enumType) &&
|
|
123
|
+
!isJsonType(enumType) &&
|
|
124
|
+
!isReservedString(enumValue)
|
|
125
|
+
);
|
|
126
|
+
});
|
|
76
127
|
if (
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
type !== BOOLEAN &&
|
|
80
|
-
type !== OBJECT &&
|
|
81
|
-
type !== ARRAY
|
|
128
|
+
!isEnum &&
|
|
129
|
+
(!isUndefined(enumValues) || !isCellOrValueSchemaTypes(type))
|
|
82
130
|
) {
|
|
83
131
|
return void 0;
|
|
84
132
|
}
|
|
85
|
-
const cellOrValueSchema = {[TYPE]: type};
|
|
133
|
+
const cellOrValueSchema = isEnum ? {[ENUM]: enumValues} : {[TYPE]: type};
|
|
86
134
|
ifNotUndefined(defaultValue, (defaultValue2) => {
|
|
87
135
|
cellOrValueSchema[DEFAULT] = defaultValue2;
|
|
88
136
|
});
|
|
@@ -132,7 +180,6 @@ const createCustomSchematizer = (
|
|
|
132
180
|
});
|
|
133
181
|
};
|
|
134
182
|
|
|
135
|
-
const MIXED = 'mixed';
|
|
136
183
|
const unwrapSchema = (
|
|
137
184
|
schema,
|
|
138
185
|
defaultValue,
|
|
@@ -140,15 +187,12 @@ const unwrapSchema = (
|
|
|
140
187
|
required = schema?.spec?.[OPTIONAL] === false,
|
|
141
188
|
) => {
|
|
142
189
|
const oneOf = collValues(schema?._whitelist);
|
|
190
|
+
const enumValues = arrayFilter(oneOf, (value) => !isNull(value));
|
|
191
|
+
const hasNull = !isUndefined(arrayFind(oneOf, isNull));
|
|
143
192
|
return [
|
|
144
|
-
{
|
|
145
|
-
[TYPE]:
|
|
146
|
-
schema?.type === MIXED && !isEmpty(oneOf) && arrayEvery(oneOf, isString)
|
|
147
|
-
? STRING
|
|
148
|
-
: schema?.type,
|
|
149
|
-
},
|
|
193
|
+
!isEmpty(enumValues) ? {[ENUM]: enumValues} : {[TYPE]: schema?.type},
|
|
150
194
|
defaultValue ?? schema?.spec?.[DEFAULT],
|
|
151
|
-
allowNull || schema?.spec?.[NULLABLE] || false,
|
|
195
|
+
allowNull || schema?.spec?.[NULLABLE] || hasNull || false,
|
|
152
196
|
required,
|
|
153
197
|
];
|
|
154
198
|
};
|
|
@@ -6,27 +6,37 @@ 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 NULLABLE = 'nullable';
|
|
12
14
|
const OPTIONAL = 'optional';
|
|
13
15
|
const REQUIRED = 'required';
|
|
16
|
+
const UNDEFINED = '\uFFFC';
|
|
17
|
+
const JSON_PREFIX = '\uFFFD';
|
|
14
18
|
|
|
15
19
|
const getIfNotFunction = (predicate) => (value, then, otherwise) =>
|
|
16
20
|
predicate(value)
|
|
17
21
|
? /* istanbul ignore next */
|
|
18
22
|
otherwise?.()
|
|
19
23
|
: then(value);
|
|
24
|
+
const isFiniteNumber = isFinite;
|
|
20
25
|
const isNullish = (thing) => thing == null;
|
|
21
26
|
const isUndefined = (thing) => thing === void 0;
|
|
27
|
+
const isNull = (thing) => thing === null;
|
|
22
28
|
const ifNotNullish = getIfNotFunction(isNullish);
|
|
23
29
|
const ifNotUndefined = getIfNotFunction(isUndefined);
|
|
30
|
+
const isTypeStringOrBoolean = (type) => type == STRING || type == BOOLEAN;
|
|
24
31
|
const isString = (thing) => getTypeOf(thing) == STRING;
|
|
32
|
+
const isArray = (thing) => Array.isArray(thing);
|
|
25
33
|
const size = (thing) => thing.length;
|
|
26
34
|
const isEmpty = (thing) => size(thing) == 0;
|
|
27
35
|
|
|
28
36
|
const arrayEvery = (array, cb) => array.every(cb);
|
|
29
37
|
const arrayForEach = (array, cb) => array.forEach(cb);
|
|
38
|
+
const arrayFilter = (array, cb) => array.filter(cb);
|
|
39
|
+
const arrayFind = (array, cb) => array.find(cb);
|
|
30
40
|
|
|
31
41
|
const collValues = (coll) => [...(coll?.values() ?? [])];
|
|
32
42
|
|
|
@@ -64,6 +74,33 @@ const objForEach = (obj, cb) =>
|
|
|
64
74
|
const objSize = (obj) => size(objIds(obj));
|
|
65
75
|
const objIsEmpty = (obj) => isObject(obj) && objSize(obj) == 0;
|
|
66
76
|
|
|
77
|
+
const getCellOrValueType = (cellOrValue) => {
|
|
78
|
+
if (isNull(cellOrValue)) {
|
|
79
|
+
return NULL;
|
|
80
|
+
}
|
|
81
|
+
if (isArray(cellOrValue)) {
|
|
82
|
+
return ARRAY;
|
|
83
|
+
}
|
|
84
|
+
if (isObject(cellOrValue)) {
|
|
85
|
+
return OBJECT;
|
|
86
|
+
}
|
|
87
|
+
const type = getTypeOf(cellOrValue);
|
|
88
|
+
return isTypeStringOrBoolean(type) ||
|
|
89
|
+
(type == NUMBER && isFiniteNumber(cellOrValue))
|
|
90
|
+
? type
|
|
91
|
+
: void 0;
|
|
92
|
+
};
|
|
93
|
+
const isJsonType = (type) => type == OBJECT || type == ARRAY;
|
|
94
|
+
const isCellOrValueSchemaType = (type) =>
|
|
95
|
+
isTypeStringOrBoolean(type) || type == NUMBER || isJsonType(type);
|
|
96
|
+
const isCellOrValueSchemaTypes = (types) =>
|
|
97
|
+
isArray(types)
|
|
98
|
+
? !isUndefined(types[0]) && arrayEvery(types, isCellOrValueSchemaType)
|
|
99
|
+
: isCellOrValueSchemaType(types);
|
|
100
|
+
const isEncodedJson = (value) => isString(value) && value[0] == JSON_PREFIX;
|
|
101
|
+
const isReservedString = (value, encoded = 0) =>
|
|
102
|
+
value === UNDEFINED || (!encoded && isEncodedJson(value));
|
|
103
|
+
|
|
67
104
|
const createCustomSchematizer = (
|
|
68
105
|
unwrapSchema,
|
|
69
106
|
getProperties,
|
|
@@ -73,16 +110,27 @@ const createCustomSchematizer = (
|
|
|
73
110
|
const [unwrapped, defaultValue, allowNull, unwrappedRequired = required] =
|
|
74
111
|
unwrapSchema(schema, void 0, void 0, required);
|
|
75
112
|
const type = unwrapped?.type;
|
|
113
|
+
const enumValues = unwrapped?.[ENUM];
|
|
114
|
+
const isEnum =
|
|
115
|
+
isUndefined(type) &&
|
|
116
|
+
isArray(enumValues) &&
|
|
117
|
+
!isUndefined(enumValues[0]) &&
|
|
118
|
+
arrayEvery(enumValues, (enumValue) => {
|
|
119
|
+
const enumType = getCellOrValueType(enumValue);
|
|
120
|
+
return (
|
|
121
|
+
!isNull(enumValue) &&
|
|
122
|
+
!isUndefined(enumType) &&
|
|
123
|
+
!isJsonType(enumType) &&
|
|
124
|
+
!isReservedString(enumValue)
|
|
125
|
+
);
|
|
126
|
+
});
|
|
76
127
|
if (
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
type !== BOOLEAN &&
|
|
80
|
-
type !== OBJECT &&
|
|
81
|
-
type !== ARRAY
|
|
128
|
+
!isEnum &&
|
|
129
|
+
(!isUndefined(enumValues) || !isCellOrValueSchemaTypes(type))
|
|
82
130
|
) {
|
|
83
131
|
return void 0;
|
|
84
132
|
}
|
|
85
|
-
const cellOrValueSchema = {[TYPE]: type};
|
|
133
|
+
const cellOrValueSchema = isEnum ? {[ENUM]: enumValues} : {[TYPE]: type};
|
|
86
134
|
ifNotUndefined(defaultValue, (defaultValue2) => {
|
|
87
135
|
cellOrValueSchema[DEFAULT] = defaultValue2;
|
|
88
136
|
});
|
|
@@ -132,7 +180,6 @@ const createCustomSchematizer = (
|
|
|
132
180
|
});
|
|
133
181
|
};
|
|
134
182
|
|
|
135
|
-
const MIXED = 'mixed';
|
|
136
183
|
const unwrapSchema = (
|
|
137
184
|
schema,
|
|
138
185
|
defaultValue,
|
|
@@ -140,15 +187,12 @@ const unwrapSchema = (
|
|
|
140
187
|
required = schema?.spec?.[OPTIONAL] === false,
|
|
141
188
|
) => {
|
|
142
189
|
const oneOf = collValues(schema?._whitelist);
|
|
190
|
+
const enumValues = arrayFilter(oneOf, (value) => !isNull(value));
|
|
191
|
+
const hasNull = !isUndefined(arrayFind(oneOf, isNull));
|
|
143
192
|
return [
|
|
144
|
-
{
|
|
145
|
-
[TYPE]:
|
|
146
|
-
schema?.type === MIXED && !isEmpty(oneOf) && arrayEvery(oneOf, isString)
|
|
147
|
-
? STRING
|
|
148
|
-
: schema?.type,
|
|
149
|
-
},
|
|
193
|
+
!isEmpty(enumValues) ? {[ENUM]: enumValues} : {[TYPE]: schema?.type},
|
|
150
194
|
defaultValue ?? schema?.spec?.[DEFAULT],
|
|
151
|
-
allowNull || schema?.spec?.[NULLABLE] || false,
|
|
195
|
+
allowNull || schema?.spec?.[NULLABLE] || hasNull || false,
|
|
152
196
|
required,
|
|
153
197
|
];
|
|
154
198
|
};
|
|
@@ -6,27 +6,38 @@ 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 NULLABLE = 'nullable';
|
|
12
14
|
const OPTIONAL = 'optional';
|
|
13
15
|
const REQUIRED = 'required';
|
|
14
16
|
const RECORD = 'record';
|
|
17
|
+
const UNDEFINED = '\uFFFC';
|
|
18
|
+
const JSON_PREFIX = '\uFFFD';
|
|
15
19
|
|
|
16
20
|
const getIfNotFunction = (predicate) => (value, then, otherwise) =>
|
|
17
21
|
predicate(value)
|
|
18
22
|
? /* istanbul ignore next */
|
|
19
23
|
otherwise?.()
|
|
20
24
|
: then(value);
|
|
25
|
+
const isFiniteNumber = isFinite;
|
|
21
26
|
const isNullish = (thing) => thing == null;
|
|
22
27
|
const isUndefined = (thing) => thing === void 0;
|
|
28
|
+
const isNull = (thing) => thing === null;
|
|
23
29
|
const ifNotNullish = getIfNotFunction(isNullish);
|
|
24
30
|
const ifNotUndefined = getIfNotFunction(isUndefined);
|
|
31
|
+
const isTypeStringOrBoolean = (type) => type == STRING || type == BOOLEAN;
|
|
25
32
|
const isString = (thing) => getTypeOf(thing) == STRING;
|
|
33
|
+
const isArray = (thing) => Array.isArray(thing);
|
|
26
34
|
const size = (thing) => thing.length;
|
|
27
35
|
|
|
36
|
+
const arrayIndexOf = (array, value) => array.indexOf(value);
|
|
28
37
|
const arrayEvery = (array, cb) => array.every(cb);
|
|
29
38
|
const arrayForEach = (array, cb) => array.forEach(cb);
|
|
39
|
+
const arrayFilter = (array, cb) => array.filter(cb);
|
|
40
|
+
const arrayPush = (array, ...values) => array.push(...values);
|
|
30
41
|
|
|
31
42
|
const object = Object;
|
|
32
43
|
const getPrototypeOf = (obj) => object.getPrototypeOf(obj);
|
|
@@ -59,9 +70,48 @@ const objNew = (entries = []) => {
|
|
|
59
70
|
};
|
|
60
71
|
const objForEach = (obj, cb) =>
|
|
61
72
|
arrayForEach(objIds(obj), (id) => cb(obj[id], id));
|
|
73
|
+
const objValues = (obj) => object.values(obj);
|
|
62
74
|
const objSize = (obj) => size(objIds(obj));
|
|
63
75
|
const objIsEmpty = (obj) => isObject(obj) && objSize(obj) == 0;
|
|
64
76
|
|
|
77
|
+
const getCellOrValueType = (cellOrValue) => {
|
|
78
|
+
if (isNull(cellOrValue)) {
|
|
79
|
+
return NULL;
|
|
80
|
+
}
|
|
81
|
+
if (isArray(cellOrValue)) {
|
|
82
|
+
return ARRAY;
|
|
83
|
+
}
|
|
84
|
+
if (isObject(cellOrValue)) {
|
|
85
|
+
return OBJECT;
|
|
86
|
+
}
|
|
87
|
+
const type = getTypeOf(cellOrValue);
|
|
88
|
+
return isTypeStringOrBoolean(type) ||
|
|
89
|
+
(type == NUMBER && isFiniteNumber(cellOrValue))
|
|
90
|
+
? type
|
|
91
|
+
: void 0;
|
|
92
|
+
};
|
|
93
|
+
const isJsonType = (type) => type == OBJECT || type == ARRAY;
|
|
94
|
+
const isCellOrValueSchemaType = (type) =>
|
|
95
|
+
isTypeStringOrBoolean(type) || type == NUMBER || isJsonType(type);
|
|
96
|
+
const isCellOrValueSchemaTypes = (types) =>
|
|
97
|
+
isArray(types)
|
|
98
|
+
? !isUndefined(types[0]) && arrayEvery(types, isCellOrValueSchemaType)
|
|
99
|
+
: isCellOrValueSchemaType(types);
|
|
100
|
+
const isEncodedJson = (value) => isString(value) && value[0] == JSON_PREFIX;
|
|
101
|
+
const isReservedString = (value, encoded = 0) =>
|
|
102
|
+
value === UNDEFINED || (!encoded && isEncodedJson(value));
|
|
103
|
+
|
|
104
|
+
const getTypeOrTypeUnion = (types) => {
|
|
105
|
+
if (!arrayEvery(types, isCellOrValueSchemaType)) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const uniqueTypes = arrayFilter(
|
|
109
|
+
types,
|
|
110
|
+
(type, index) => arrayIndexOf(types, type) == index,
|
|
111
|
+
);
|
|
112
|
+
return uniqueTypes[1] ? uniqueTypes : uniqueTypes[0];
|
|
113
|
+
};
|
|
114
|
+
|
|
65
115
|
const createCustomSchematizer = (
|
|
66
116
|
unwrapSchema,
|
|
67
117
|
getProperties,
|
|
@@ -71,16 +121,27 @@ const createCustomSchematizer = (
|
|
|
71
121
|
const [unwrapped, defaultValue, allowNull, unwrappedRequired = required] =
|
|
72
122
|
unwrapSchema(schema, void 0, void 0, required);
|
|
73
123
|
const type = unwrapped?.type;
|
|
124
|
+
const enumValues = unwrapped?.[ENUM];
|
|
125
|
+
const isEnum =
|
|
126
|
+
isUndefined(type) &&
|
|
127
|
+
isArray(enumValues) &&
|
|
128
|
+
!isUndefined(enumValues[0]) &&
|
|
129
|
+
arrayEvery(enumValues, (enumValue) => {
|
|
130
|
+
const enumType = getCellOrValueType(enumValue);
|
|
131
|
+
return (
|
|
132
|
+
!isNull(enumValue) &&
|
|
133
|
+
!isUndefined(enumType) &&
|
|
134
|
+
!isJsonType(enumType) &&
|
|
135
|
+
!isReservedString(enumValue)
|
|
136
|
+
);
|
|
137
|
+
});
|
|
74
138
|
if (
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
type !== BOOLEAN &&
|
|
78
|
-
type !== OBJECT &&
|
|
79
|
-
type !== ARRAY
|
|
139
|
+
!isEnum &&
|
|
140
|
+
(!isUndefined(enumValues) || !isCellOrValueSchemaTypes(type))
|
|
80
141
|
) {
|
|
81
142
|
return void 0;
|
|
82
143
|
}
|
|
83
|
-
const cellOrValueSchema = {[TYPE]: type};
|
|
144
|
+
const cellOrValueSchema = isEnum ? {[ENUM]: enumValues} : {[TYPE]: type};
|
|
84
145
|
ifNotUndefined(defaultValue, (defaultValue2) => {
|
|
85
146
|
cellOrValueSchema[DEFAULT] = defaultValue2;
|
|
86
147
|
});
|
|
@@ -130,8 +191,8 @@ const createCustomSchematizer = (
|
|
|
130
191
|
});
|
|
131
192
|
};
|
|
132
193
|
|
|
133
|
-
const ENUM = 'enum';
|
|
134
194
|
const LITERAL = 'literal';
|
|
195
|
+
const ZOD_UNION = 'union';
|
|
135
196
|
const getDef = (schema) => schema?.def ?? schema?._zod?.def;
|
|
136
197
|
const unwrapSchema = (schema, defaultValue, allowNull, required = true) => {
|
|
137
198
|
const def = getDef(schema);
|
|
@@ -144,11 +205,69 @@ const unwrapSchema = (schema, defaultValue, allowNull, required = true) => {
|
|
|
144
205
|
? unwrapSchema(def.innerType, def.defaultValue, allowNull, false)
|
|
145
206
|
: type === RECORD
|
|
146
207
|
? [{type: OBJECT}, defaultValue, allowNull ?? false, required]
|
|
147
|
-
: type ===
|
|
148
|
-
?
|
|
149
|
-
: type ===
|
|
150
|
-
? [
|
|
151
|
-
|
|
208
|
+
: type === ZOD_UNION
|
|
209
|
+
? unwrapUnion(def, defaultValue, allowNull, required)
|
|
210
|
+
: type === ENUM
|
|
211
|
+
? [
|
|
212
|
+
{[ENUM]: objValues(def.entries)},
|
|
213
|
+
defaultValue,
|
|
214
|
+
allowNull ?? false,
|
|
215
|
+
required,
|
|
216
|
+
]
|
|
217
|
+
: type === LITERAL
|
|
218
|
+
? [
|
|
219
|
+
{[ENUM]: def.values},
|
|
220
|
+
defaultValue,
|
|
221
|
+
allowNull ?? false,
|
|
222
|
+
required,
|
|
223
|
+
]
|
|
224
|
+
: [schema, defaultValue, allowNull ?? false, required];
|
|
225
|
+
};
|
|
226
|
+
const unwrapUnion = (def, defaultValue, allowNull, required = true) => {
|
|
227
|
+
const nonNullOptions = arrayFilter(
|
|
228
|
+
def.options,
|
|
229
|
+
(option) => getDef(option)?.type !== NULL,
|
|
230
|
+
);
|
|
231
|
+
const hasNull = nonNullOptions.length !== def.options.length;
|
|
232
|
+
const enumValues = [];
|
|
233
|
+
const allEnums = arrayEvery(nonNullOptions, (option) => {
|
|
234
|
+
const optionDef = getDef(option);
|
|
235
|
+
const values =
|
|
236
|
+
optionDef?.type === LITERAL
|
|
237
|
+
? optionDef.values
|
|
238
|
+
: optionDef?.type === ENUM
|
|
239
|
+
? objValues(optionDef.entries)
|
|
240
|
+
: void 0;
|
|
241
|
+
if (isUndefined(values)) {
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
arrayForEach(values, (value) => arrayPush(enumValues, value));
|
|
245
|
+
return true;
|
|
246
|
+
});
|
|
247
|
+
const types = [];
|
|
248
|
+
if (!allEnums) {
|
|
249
|
+
arrayForEach(nonNullOptions, (option) => {
|
|
250
|
+
const optionDef = getDef(option);
|
|
251
|
+
const optionType = optionDef?.type;
|
|
252
|
+
if (optionType === LITERAL) {
|
|
253
|
+
arrayForEach(optionDef.values, (value) =>
|
|
254
|
+
arrayPush(types, getCellOrValueType(value)),
|
|
255
|
+
);
|
|
256
|
+
} else if (optionType === ENUM) {
|
|
257
|
+
arrayForEach(objValues(optionDef.entries), (value) =>
|
|
258
|
+
arrayPush(types, getCellOrValueType(value)),
|
|
259
|
+
);
|
|
260
|
+
} else {
|
|
261
|
+
arrayPush(types, optionType === RECORD ? OBJECT : optionType);
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
return [
|
|
266
|
+
allEnums ? {[ENUM]: enumValues} : {type: getTypeOrTypeUnion(types)},
|
|
267
|
+
defaultValue,
|
|
268
|
+
allowNull || hasNull,
|
|
269
|
+
required,
|
|
270
|
+
];
|
|
152
271
|
};
|
|
153
272
|
const getProperties = (schema) => getDef(schema)?.shape;
|
|
154
273
|
const createZodSchematizer = () =>
|
|
@@ -6,27 +6,38 @@ 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 NULLABLE = 'nullable';
|
|
12
14
|
const OPTIONAL = 'optional';
|
|
13
15
|
const REQUIRED = 'required';
|
|
14
16
|
const RECORD = 'record';
|
|
17
|
+
const UNDEFINED = '\uFFFC';
|
|
18
|
+
const JSON_PREFIX = '\uFFFD';
|
|
15
19
|
|
|
16
20
|
const getIfNotFunction = (predicate) => (value, then, otherwise) =>
|
|
17
21
|
predicate(value)
|
|
18
22
|
? /* istanbul ignore next */
|
|
19
23
|
otherwise?.()
|
|
20
24
|
: then(value);
|
|
25
|
+
const isFiniteNumber = isFinite;
|
|
21
26
|
const isNullish = (thing) => thing == null;
|
|
22
27
|
const isUndefined = (thing) => thing === void 0;
|
|
28
|
+
const isNull = (thing) => thing === null;
|
|
23
29
|
const ifNotNullish = getIfNotFunction(isNullish);
|
|
24
30
|
const ifNotUndefined = getIfNotFunction(isUndefined);
|
|
31
|
+
const isTypeStringOrBoolean = (type) => type == STRING || type == BOOLEAN;
|
|
25
32
|
const isString = (thing) => getTypeOf(thing) == STRING;
|
|
33
|
+
const isArray = (thing) => Array.isArray(thing);
|
|
26
34
|
const size = (thing) => thing.length;
|
|
27
35
|
|
|
36
|
+
const arrayIndexOf = (array, value) => array.indexOf(value);
|
|
28
37
|
const arrayEvery = (array, cb) => array.every(cb);
|
|
29
38
|
const arrayForEach = (array, cb) => array.forEach(cb);
|
|
39
|
+
const arrayFilter = (array, cb) => array.filter(cb);
|
|
40
|
+
const arrayPush = (array, ...values) => array.push(...values);
|
|
30
41
|
|
|
31
42
|
const object = Object;
|
|
32
43
|
const getPrototypeOf = (obj) => object.getPrototypeOf(obj);
|
|
@@ -59,9 +70,48 @@ const objNew = (entries = []) => {
|
|
|
59
70
|
};
|
|
60
71
|
const objForEach = (obj, cb) =>
|
|
61
72
|
arrayForEach(objIds(obj), (id) => cb(obj[id], id));
|
|
73
|
+
const objValues = (obj) => object.values(obj);
|
|
62
74
|
const objSize = (obj) => size(objIds(obj));
|
|
63
75
|
const objIsEmpty = (obj) => isObject(obj) && objSize(obj) == 0;
|
|
64
76
|
|
|
77
|
+
const getCellOrValueType = (cellOrValue) => {
|
|
78
|
+
if (isNull(cellOrValue)) {
|
|
79
|
+
return NULL;
|
|
80
|
+
}
|
|
81
|
+
if (isArray(cellOrValue)) {
|
|
82
|
+
return ARRAY;
|
|
83
|
+
}
|
|
84
|
+
if (isObject(cellOrValue)) {
|
|
85
|
+
return OBJECT;
|
|
86
|
+
}
|
|
87
|
+
const type = getTypeOf(cellOrValue);
|
|
88
|
+
return isTypeStringOrBoolean(type) ||
|
|
89
|
+
(type == NUMBER && isFiniteNumber(cellOrValue))
|
|
90
|
+
? type
|
|
91
|
+
: void 0;
|
|
92
|
+
};
|
|
93
|
+
const isJsonType = (type) => type == OBJECT || type == ARRAY;
|
|
94
|
+
const isCellOrValueSchemaType = (type) =>
|
|
95
|
+
isTypeStringOrBoolean(type) || type == NUMBER || isJsonType(type);
|
|
96
|
+
const isCellOrValueSchemaTypes = (types) =>
|
|
97
|
+
isArray(types)
|
|
98
|
+
? !isUndefined(types[0]) && arrayEvery(types, isCellOrValueSchemaType)
|
|
99
|
+
: isCellOrValueSchemaType(types);
|
|
100
|
+
const isEncodedJson = (value) => isString(value) && value[0] == JSON_PREFIX;
|
|
101
|
+
const isReservedString = (value, encoded = 0) =>
|
|
102
|
+
value === UNDEFINED || (!encoded && isEncodedJson(value));
|
|
103
|
+
|
|
104
|
+
const getTypeOrTypeUnion = (types) => {
|
|
105
|
+
if (!arrayEvery(types, isCellOrValueSchemaType)) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const uniqueTypes = arrayFilter(
|
|
109
|
+
types,
|
|
110
|
+
(type, index) => arrayIndexOf(types, type) == index,
|
|
111
|
+
);
|
|
112
|
+
return uniqueTypes[1] ? uniqueTypes : uniqueTypes[0];
|
|
113
|
+
};
|
|
114
|
+
|
|
65
115
|
const createCustomSchematizer = (
|
|
66
116
|
unwrapSchema,
|
|
67
117
|
getProperties,
|
|
@@ -71,16 +121,27 @@ const createCustomSchematizer = (
|
|
|
71
121
|
const [unwrapped, defaultValue, allowNull, unwrappedRequired = required] =
|
|
72
122
|
unwrapSchema(schema, void 0, void 0, required);
|
|
73
123
|
const type = unwrapped?.type;
|
|
124
|
+
const enumValues = unwrapped?.[ENUM];
|
|
125
|
+
const isEnum =
|
|
126
|
+
isUndefined(type) &&
|
|
127
|
+
isArray(enumValues) &&
|
|
128
|
+
!isUndefined(enumValues[0]) &&
|
|
129
|
+
arrayEvery(enumValues, (enumValue) => {
|
|
130
|
+
const enumType = getCellOrValueType(enumValue);
|
|
131
|
+
return (
|
|
132
|
+
!isNull(enumValue) &&
|
|
133
|
+
!isUndefined(enumType) &&
|
|
134
|
+
!isJsonType(enumType) &&
|
|
135
|
+
!isReservedString(enumValue)
|
|
136
|
+
);
|
|
137
|
+
});
|
|
74
138
|
if (
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
type !== BOOLEAN &&
|
|
78
|
-
type !== OBJECT &&
|
|
79
|
-
type !== ARRAY
|
|
139
|
+
!isEnum &&
|
|
140
|
+
(!isUndefined(enumValues) || !isCellOrValueSchemaTypes(type))
|
|
80
141
|
) {
|
|
81
142
|
return void 0;
|
|
82
143
|
}
|
|
83
|
-
const cellOrValueSchema = {[TYPE]: type};
|
|
144
|
+
const cellOrValueSchema = isEnum ? {[ENUM]: enumValues} : {[TYPE]: type};
|
|
84
145
|
ifNotUndefined(defaultValue, (defaultValue2) => {
|
|
85
146
|
cellOrValueSchema[DEFAULT] = defaultValue2;
|
|
86
147
|
});
|
|
@@ -130,8 +191,8 @@ const createCustomSchematizer = (
|
|
|
130
191
|
});
|
|
131
192
|
};
|
|
132
193
|
|
|
133
|
-
const ENUM = 'enum';
|
|
134
194
|
const LITERAL = 'literal';
|
|
195
|
+
const ZOD_UNION = 'union';
|
|
135
196
|
const getDef = (schema) => schema?.def ?? schema?._zod?.def;
|
|
136
197
|
const unwrapSchema = (schema, defaultValue, allowNull, required = true) => {
|
|
137
198
|
const def = getDef(schema);
|
|
@@ -144,11 +205,69 @@ const unwrapSchema = (schema, defaultValue, allowNull, required = true) => {
|
|
|
144
205
|
? unwrapSchema(def.innerType, def.defaultValue, allowNull, false)
|
|
145
206
|
: type === RECORD
|
|
146
207
|
? [{type: OBJECT}, defaultValue, allowNull ?? false, required]
|
|
147
|
-
: type ===
|
|
148
|
-
?
|
|
149
|
-
: type ===
|
|
150
|
-
? [
|
|
151
|
-
|
|
208
|
+
: type === ZOD_UNION
|
|
209
|
+
? unwrapUnion(def, defaultValue, allowNull, required)
|
|
210
|
+
: type === ENUM
|
|
211
|
+
? [
|
|
212
|
+
{[ENUM]: objValues(def.entries)},
|
|
213
|
+
defaultValue,
|
|
214
|
+
allowNull ?? false,
|
|
215
|
+
required,
|
|
216
|
+
]
|
|
217
|
+
: type === LITERAL
|
|
218
|
+
? [
|
|
219
|
+
{[ENUM]: def.values},
|
|
220
|
+
defaultValue,
|
|
221
|
+
allowNull ?? false,
|
|
222
|
+
required,
|
|
223
|
+
]
|
|
224
|
+
: [schema, defaultValue, allowNull ?? false, required];
|
|
225
|
+
};
|
|
226
|
+
const unwrapUnion = (def, defaultValue, allowNull, required = true) => {
|
|
227
|
+
const nonNullOptions = arrayFilter(
|
|
228
|
+
def.options,
|
|
229
|
+
(option) => getDef(option)?.type !== NULL,
|
|
230
|
+
);
|
|
231
|
+
const hasNull = nonNullOptions.length !== def.options.length;
|
|
232
|
+
const enumValues = [];
|
|
233
|
+
const allEnums = arrayEvery(nonNullOptions, (option) => {
|
|
234
|
+
const optionDef = getDef(option);
|
|
235
|
+
const values =
|
|
236
|
+
optionDef?.type === LITERAL
|
|
237
|
+
? optionDef.values
|
|
238
|
+
: optionDef?.type === ENUM
|
|
239
|
+
? objValues(optionDef.entries)
|
|
240
|
+
: void 0;
|
|
241
|
+
if (isUndefined(values)) {
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
arrayForEach(values, (value) => arrayPush(enumValues, value));
|
|
245
|
+
return true;
|
|
246
|
+
});
|
|
247
|
+
const types = [];
|
|
248
|
+
if (!allEnums) {
|
|
249
|
+
arrayForEach(nonNullOptions, (option) => {
|
|
250
|
+
const optionDef = getDef(option);
|
|
251
|
+
const optionType = optionDef?.type;
|
|
252
|
+
if (optionType === LITERAL) {
|
|
253
|
+
arrayForEach(optionDef.values, (value) =>
|
|
254
|
+
arrayPush(types, getCellOrValueType(value)),
|
|
255
|
+
);
|
|
256
|
+
} else if (optionType === ENUM) {
|
|
257
|
+
arrayForEach(objValues(optionDef.entries), (value) =>
|
|
258
|
+
arrayPush(types, getCellOrValueType(value)),
|
|
259
|
+
);
|
|
260
|
+
} else {
|
|
261
|
+
arrayPush(types, optionType === RECORD ? OBJECT : optionType);
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
return [
|
|
266
|
+
allEnums ? {[ENUM]: enumValues} : {type: getTypeOrTypeUnion(types)},
|
|
267
|
+
defaultValue,
|
|
268
|
+
allowNull || hasNull,
|
|
269
|
+
required,
|
|
270
|
+
];
|
|
152
271
|
};
|
|
153
272
|
const getProperties = (schema) => getDef(schema)?.shape;
|
|
154
273
|
const createZodSchematizer = () =>
|