@based/schema 5.0.0-alpha.0 → 5.0.0-alpha.10
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/dist/def/addEdges.d.ts +4 -0
- package/dist/def/addEdges.js +76 -0
- package/dist/def/createEmptyDef.d.ts +38 -0
- package/dist/def/createEmptyDef.js +41 -0
- package/dist/def/defaultMap.d.ts +3 -0
- package/dist/def/defaultMap.js +27 -0
- package/dist/def/fillEmptyMain.d.ts +5 -0
- package/dist/def/fillEmptyMain.js +61 -0
- package/dist/def/getPropLen.d.ts +3 -0
- package/dist/def/getPropLen.js +23 -0
- package/dist/def/index.d.ts +9 -0
- package/dist/def/index.js +10 -0
- package/dist/def/makePacked.d.ts +3 -0
- package/dist/def/makePacked.js +50 -0
- package/dist/def/makeSeparateSort.d.ts +3 -0
- package/dist/def/makeSeparateSort.js +27 -0
- package/dist/def/makeSeparateTextSort.d.ts +3 -0
- package/dist/def/makeSeparateTextSort.js +38 -0
- package/dist/def/readFromPacked.d.ts +3 -0
- package/dist/def/readFromPacked.js +140 -0
- package/dist/def/selvaBuffer.d.ts +5 -0
- package/dist/def/selvaBuffer.js +115 -0
- package/dist/def/typeDef.d.ts +7 -0
- package/dist/def/typeDef.js +215 -0
- package/dist/def/types.d.ts +166 -0
- package/dist/def/types.js +116 -0
- package/dist/def/utils.d.ts +8 -0
- package/dist/def/utils.js +59 -0
- package/dist/def/validation.d.ts +7 -0
- package/dist/def/validation.js +258 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/lang.d.ts +1 -0
- package/dist/lang.js +4 -0
- package/dist/parse/assert.d.ts +1 -0
- package/dist/parse/assert.js +7 -1
- package/dist/parse/index.d.ts +1 -1
- package/dist/parse/index.js +5 -5
- package/dist/parse/props.d.ts +1 -0
- package/dist/parse/props.js +112 -47
- package/dist/types.d.ts +42 -13
- package/dist/types.js +2 -0
- package/package.json +8 -4
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { convertToTimestamp } from '@saulx/utils';
|
|
2
|
+
import { TYPE_INDEX_MAP } from './types.js';
|
|
3
|
+
import { MAX_ID, MIN_ID } from '../types.js';
|
|
4
|
+
const EPSILON = 1e-9; // Small tolerance for floating point comparisons
|
|
5
|
+
export const VALIDATION_MAP = {
|
|
6
|
+
[TYPE_INDEX_MAP.alias]: (value) => {
|
|
7
|
+
if (typeof value !== 'string') {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
return true;
|
|
11
|
+
},
|
|
12
|
+
[TYPE_INDEX_MAP.binary]: (value) => {
|
|
13
|
+
if (value instanceof Uint8Array) {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
return false;
|
|
17
|
+
},
|
|
18
|
+
[TYPE_INDEX_MAP.boolean]: (value) => {
|
|
19
|
+
if (typeof value !== 'boolean') {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
return true;
|
|
23
|
+
},
|
|
24
|
+
[TYPE_INDEX_MAP.cardinality]: (val) => {
|
|
25
|
+
return (typeof val === 'string' ||
|
|
26
|
+
(val instanceof Uint8Array && val.byteLength === 8));
|
|
27
|
+
},
|
|
28
|
+
[TYPE_INDEX_MAP.timestamp]: (value, t) => {
|
|
29
|
+
if (typeof value !== 'number' || value % t.step !== 0) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
if (t.min !== undefined) {
|
|
33
|
+
if (typeof t.min === 'number') {
|
|
34
|
+
if (value < t.min) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else if (value < convertToTimestamp(t.min)) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
return value > -1;
|
|
44
|
+
}
|
|
45
|
+
if (t.max !== undefined) {
|
|
46
|
+
if (typeof t.max === 'number') {
|
|
47
|
+
if (value > t.max) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
else if (value > convertToTimestamp(t.max)) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
},
|
|
57
|
+
[TYPE_INDEX_MAP.int16]: (value, t) => {
|
|
58
|
+
if (typeof value !== 'number' || value % t.step !== 0) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
if (value > 32767 || value < -32768) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
if (t.min !== undefined && value < t.min) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
if (t.max !== undefined && value > t.max) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
},
|
|
72
|
+
[TYPE_INDEX_MAP.int32]: (value, t) => {
|
|
73
|
+
if (typeof value !== 'number' || value % t.step !== 0) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
if (value > 2147483647 || value < -2147483648) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
if (t.min !== undefined && value < t.min) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
if (t.max !== undefined && value > t.max) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
return true;
|
|
86
|
+
},
|
|
87
|
+
[TYPE_INDEX_MAP.int8]: (value, t) => {
|
|
88
|
+
// use % for steps size
|
|
89
|
+
if (typeof value !== 'number' || value % t.step !== 0) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
if (value > 127 || value < -128) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
if (t.min !== undefined && value < t.min) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
if (t.max !== undefined && value > t.max) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
return true;
|
|
102
|
+
},
|
|
103
|
+
[TYPE_INDEX_MAP.uint8]: (value, t) => {
|
|
104
|
+
if (typeof value !== 'number' || value % t.step !== 0) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
if (value > 255 || value < 0) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
if (t.min !== undefined && value < t.min) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
if (t.max !== undefined && value > t.max) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
return true;
|
|
117
|
+
},
|
|
118
|
+
[TYPE_INDEX_MAP.uint16]: (value, t) => {
|
|
119
|
+
if (typeof value !== 'number' || value % t.step !== 0) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
if (value > 65535 || value < 0) {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
if (t.min !== undefined && value < t.min) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
if (t.max !== undefined && value > t.max) {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
return true;
|
|
132
|
+
},
|
|
133
|
+
[TYPE_INDEX_MAP.uint32]: (value, t) => {
|
|
134
|
+
if (typeof value !== 'number' || value % t.step !== 0) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
if (value > 4294967295 || value < 0) {
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
if (t.min !== undefined && value < t.min) {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
if (t.max !== undefined && value > t.max) {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
return true;
|
|
147
|
+
},
|
|
148
|
+
[TYPE_INDEX_MAP.number]: (value, t) => {
|
|
149
|
+
if (t.step) {
|
|
150
|
+
const div = value / t.step;
|
|
151
|
+
if (Math.abs(div - Math.round(div)) > EPSILON) {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (typeof value !== 'number') {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
if (t.min !== undefined && value < t.min) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
if (t.max !== undefined && value > t.max) {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
return true;
|
|
165
|
+
},
|
|
166
|
+
[TYPE_INDEX_MAP.enum]: (value, prop) => {
|
|
167
|
+
if (value === null) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
const arr = prop.enum;
|
|
171
|
+
for (let i = 0; i < arr.length; i++) {
|
|
172
|
+
if (value === arr[i]) {
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return false;
|
|
177
|
+
},
|
|
178
|
+
[TYPE_INDEX_MAP.id]: (value) => {
|
|
179
|
+
if (typeof value !== 'number' || value % 1 !== 0) {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
return true;
|
|
183
|
+
},
|
|
184
|
+
[TYPE_INDEX_MAP.json]: (value) => {
|
|
185
|
+
return true;
|
|
186
|
+
},
|
|
187
|
+
[TYPE_INDEX_MAP.microbuffer]: (value) => {
|
|
188
|
+
if (!(value instanceof Uint8Array)) {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
return true;
|
|
192
|
+
},
|
|
193
|
+
[TYPE_INDEX_MAP.reference]: (v) => {
|
|
194
|
+
if (typeof v !== 'number') {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
if (v === 0 || v > MAX_ID) {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
return true;
|
|
201
|
+
},
|
|
202
|
+
[TYPE_INDEX_MAP.references]: (v) => {
|
|
203
|
+
if (typeof v !== 'number') {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
if (v === 0 || v > MAX_ID) {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
return true;
|
|
210
|
+
},
|
|
211
|
+
[TYPE_INDEX_MAP.string]: (value, t) => {
|
|
212
|
+
// add max etc all here - make a ref to the original SCHEMA on DEF
|
|
213
|
+
if (typeof value !== 'string' && !(value instanceof Uint8Array)) {
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
return true;
|
|
217
|
+
},
|
|
218
|
+
[TYPE_INDEX_MAP.text]: (value, t) => {
|
|
219
|
+
// add max etc all here - make a ref to the original SCHEMA on DEF
|
|
220
|
+
if (typeof value !== 'string' && !(value instanceof Uint8Array)) {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
return true;
|
|
224
|
+
},
|
|
225
|
+
[TYPE_INDEX_MAP.aliases]: (value) => {
|
|
226
|
+
if (!Array.isArray(value)) {
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
const len = value.length;
|
|
230
|
+
for (let i = 0; i < len; i++) {
|
|
231
|
+
if (typeof value[i] !== 'string') {
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return true;
|
|
236
|
+
},
|
|
237
|
+
[TYPE_INDEX_MAP.vector]: (value) => {
|
|
238
|
+
// Array should be supported
|
|
239
|
+
if (!(value instanceof Float32Array)) {
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
return true;
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
export const defaultValidation = () => true;
|
|
246
|
+
export const isValidId = (id) => {
|
|
247
|
+
if (typeof id != 'number' || id < MIN_ID || id > MAX_ID) {
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
return true;
|
|
251
|
+
};
|
|
252
|
+
export const isValidString = (v) => {
|
|
253
|
+
const isVal = typeof v === 'string' ||
|
|
254
|
+
v instanceof Uint8Array ||
|
|
255
|
+
ArrayBuffer.isView(v);
|
|
256
|
+
return isVal;
|
|
257
|
+
};
|
|
258
|
+
//# sourceMappingURL=validation.js.map
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/lang.d.ts
CHANGED
|
@@ -146,6 +146,7 @@ declare const langCodes: {
|
|
|
146
146
|
readonly zu: 144;
|
|
147
147
|
};
|
|
148
148
|
export declare const langCodesMap: Map<string, 0 | 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 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144>;
|
|
149
|
+
export declare const inverseLangMap: Map<any, any>;
|
|
149
150
|
export type LangName = keyof typeof langCodes;
|
|
150
151
|
export type LangCode = (typeof langCodes)[LangName];
|
|
151
152
|
export {};
|
package/dist/lang.js
CHANGED
|
@@ -146,4 +146,8 @@ const langCodes = {
|
|
|
146
146
|
zu: 144,
|
|
147
147
|
};
|
|
148
148
|
export const langCodesMap = new Map(Object.entries(langCodes));
|
|
149
|
+
export const inverseLangMap = new Map();
|
|
150
|
+
langCodesMap.forEach((v, k) => {
|
|
151
|
+
inverseLangMap.set(v, k);
|
|
152
|
+
});
|
|
149
153
|
//# sourceMappingURL=lang.js.map
|
package/dist/parse/assert.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export declare const expectObject: (obj: any, msg?: string) => void;
|
|
2
|
+
export declare const expectFloat32Array: (arr: any) => void;
|
|
2
3
|
export declare const expectString: (obj: any) => void;
|
|
3
4
|
export declare const expectBoolean: (v: any) => void;
|
|
4
5
|
export declare const expectFunction: (v: any) => void;
|
package/dist/parse/assert.js
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isFloat32Array } from 'util/types';
|
|
2
|
+
import { EXPECTED_ARR, EXPECTED_BOOL, EXPECTED_FN, EXPECTED_NUM, EXPECTED_OBJ, EXPECTED_STR, } from './errors.js';
|
|
2
3
|
export const expectObject = (obj, msg) => {
|
|
3
4
|
if (typeof obj !== 'object' || obj === null) {
|
|
4
5
|
throw Error(msg || EXPECTED_OBJ);
|
|
5
6
|
}
|
|
6
7
|
};
|
|
8
|
+
export const expectFloat32Array = (arr) => {
|
|
9
|
+
if (!isFloat32Array(arr)) {
|
|
10
|
+
throw Error(EXPECTED_ARR);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
7
13
|
export const expectString = (obj) => {
|
|
8
14
|
if (typeof obj !== 'string') {
|
|
9
15
|
throw Error(EXPECTED_STR);
|
package/dist/parse/index.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export declare class SchemaParser {
|
|
|
12
12
|
parseTypes(): void;
|
|
13
13
|
parseProps(props: any, schemaType?: SchemaType): void;
|
|
14
14
|
parseLocales(): void;
|
|
15
|
-
parse():
|
|
15
|
+
parse(): StrictSchema;
|
|
16
16
|
}
|
|
17
17
|
export declare const print: (schema: Schema, path: string[]) => string;
|
|
18
18
|
export declare const parse: (schema: Schema) => {
|
package/dist/parse/index.js
CHANGED
|
@@ -3,10 +3,11 @@ import { getPropType } from './utils.js';
|
|
|
3
3
|
import propParsers from './props.js';
|
|
4
4
|
import pc from 'picocolors';
|
|
5
5
|
import { expectBoolean, expectObject } from './assert.js';
|
|
6
|
+
import { deepCopy } from '@saulx/utils';
|
|
6
7
|
export { getPropType };
|
|
7
8
|
export class SchemaParser {
|
|
8
9
|
constructor(schema) {
|
|
9
|
-
this.schema = schema;
|
|
10
|
+
this.schema = deepCopy(schema);
|
|
10
11
|
}
|
|
11
12
|
isItems;
|
|
12
13
|
inQuery;
|
|
@@ -20,7 +21,7 @@ export class SchemaParser {
|
|
|
20
21
|
expectObject(types);
|
|
21
22
|
for (const type in types) {
|
|
22
23
|
this.lvl++;
|
|
23
|
-
if (type
|
|
24
|
+
if (type === '_root') {
|
|
24
25
|
throw new Error(INVALID_KEY);
|
|
25
26
|
}
|
|
26
27
|
this.path[this.lvl] = type;
|
|
@@ -97,6 +98,7 @@ export class SchemaParser {
|
|
|
97
98
|
throw Error(UNKNOWN_PROP);
|
|
98
99
|
}
|
|
99
100
|
}
|
|
101
|
+
return this.schema;
|
|
100
102
|
}
|
|
101
103
|
}
|
|
102
104
|
export const print = (schema, path) => {
|
|
@@ -120,9 +122,7 @@ export const print = (schema, path) => {
|
|
|
120
122
|
export const parse = (schema) => {
|
|
121
123
|
const parser = new SchemaParser(schema);
|
|
122
124
|
try {
|
|
123
|
-
parser.parse();
|
|
124
|
-
// @ts-ignore
|
|
125
|
-
return { schema };
|
|
125
|
+
return { schema: parser.parse() };
|
|
126
126
|
}
|
|
127
127
|
catch (e) {
|
|
128
128
|
const cause = parser.path.slice(0, Math.min(4, parser.lvl) + 1);
|
package/dist/parse/props.d.ts
CHANGED
|
@@ -3,5 +3,6 @@ import type { SchemaParser } from './index.js';
|
|
|
3
3
|
type PropsFns<PropType> = Record<string, (val: any, prop: PropType, ctx: SchemaParser, key?: string) => void>;
|
|
4
4
|
declare function propParser<PropType extends SchemaAnyProp>(required: PropsFns<PropType>, optional: PropsFns<PropType>, allowShorthand?: number): (prop: any, ctx: SchemaParser) => void;
|
|
5
5
|
declare const p: Record<string, ReturnType<typeof propParser>>;
|
|
6
|
+
export declare const isDefault: (val: any, prop: any, ctx: any) => any;
|
|
6
7
|
export default p;
|
|
7
8
|
//# sourceMappingURL=props.d.ts.map
|
package/dist/parse/props.js
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
+
import { convertToTimestamp } from '@saulx/utils';
|
|
2
|
+
import { NUMBER, TYPE_INDEX_MAP, } from '../def/types.js';
|
|
3
|
+
import { VALIDATION_MAP } from '../def/validation.js';
|
|
1
4
|
import { stringFormats, dateDisplays, numberDisplays, } from '../types.js';
|
|
2
|
-
import { expectBoolean, expectFunction, expectObject, expectString,
|
|
3
|
-
import { EXPECTED_ARR,
|
|
5
|
+
import { expectBoolean, expectFunction, expectNumber, expectObject, expectString, } from './assert.js';
|
|
6
|
+
import { EXPECTED_ARR, EXPECTED_OBJ, EXPECTED_PRIMITIVE, INVALID_VALUE, MIN_MAX, MISSING_TYPE, TEXT_REQUIRES_LOCALES, TYPE_MISMATCH, UNKNOWN_PROP, NOT_ALLOWED_IN_ITEMS, } from './errors.js';
|
|
4
7
|
import { getPropType } from './utils.js';
|
|
8
|
+
import { DEFAULT_MAP } from '../def/defaultMap.js';
|
|
9
|
+
import { parseMinMaxStep } from '../def/utils.js';
|
|
5
10
|
let stringFormatsSet;
|
|
6
11
|
let numberDisplaysSet;
|
|
7
12
|
let dateDisplaysSet;
|
|
@@ -38,10 +43,21 @@ const shared = {
|
|
|
38
43
|
throw Error(TYPE_MISMATCH);
|
|
39
44
|
}
|
|
40
45
|
},
|
|
41
|
-
title(val) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
46
|
+
title(val) {
|
|
47
|
+
expectString(val);
|
|
48
|
+
},
|
|
49
|
+
description(val) {
|
|
50
|
+
expectString(val);
|
|
51
|
+
},
|
|
52
|
+
readOnly(val) {
|
|
53
|
+
expectBoolean(val);
|
|
54
|
+
},
|
|
55
|
+
examples(val) {
|
|
56
|
+
expectString(val);
|
|
57
|
+
},
|
|
58
|
+
validation(val) {
|
|
59
|
+
expectFunction(val);
|
|
60
|
+
},
|
|
45
61
|
};
|
|
46
62
|
function propParser(required, optional, allowShorthand) {
|
|
47
63
|
return (prop, ctx) => {
|
|
@@ -91,9 +107,54 @@ function propParser(required, optional, allowShorthand) {
|
|
|
91
107
|
};
|
|
92
108
|
}
|
|
93
109
|
const p = {};
|
|
110
|
+
export const isDefault = (val, prop, ctx) => {
|
|
111
|
+
let typeIndex;
|
|
112
|
+
typeIndex = TYPE_INDEX_MAP[prop.type];
|
|
113
|
+
if ('enum' in prop) {
|
|
114
|
+
typeIndex = TYPE_INDEX_MAP['enum'];
|
|
115
|
+
}
|
|
116
|
+
if (prop.type === 'timestamp') {
|
|
117
|
+
val = convertToTimestamp(val);
|
|
118
|
+
}
|
|
119
|
+
const validation = prop.validation || VALIDATION_MAP[typeIndex];
|
|
120
|
+
const propDef = {
|
|
121
|
+
typeIndex,
|
|
122
|
+
__isPropDef: true,
|
|
123
|
+
start: 0,
|
|
124
|
+
path: [],
|
|
125
|
+
prop: 0,
|
|
126
|
+
len: 0,
|
|
127
|
+
separate: false,
|
|
128
|
+
enum: prop.enum,
|
|
129
|
+
validation,
|
|
130
|
+
default: DEFAULT_MAP[typeIndex],
|
|
131
|
+
step: parseMinMaxStep(prop.step ?? typeIndex === NUMBER ? 0 : 1),
|
|
132
|
+
max: parseMinMaxStep(prop.max),
|
|
133
|
+
min: parseMinMaxStep(prop.min),
|
|
134
|
+
};
|
|
135
|
+
if (!validation(val, propDef)) {
|
|
136
|
+
throw new Error(`Incorrect default for type "${prop.type ?? 'enum'}"`);
|
|
137
|
+
}
|
|
138
|
+
if ('enum' in prop) {
|
|
139
|
+
if (val === undefined) {
|
|
140
|
+
return 0;
|
|
141
|
+
}
|
|
142
|
+
return prop.enum.findIndex((v) => v === val) + 1;
|
|
143
|
+
}
|
|
144
|
+
return val;
|
|
145
|
+
};
|
|
94
146
|
p.boolean = propParser(STUB, {
|
|
95
|
-
default(val) {
|
|
96
|
-
|
|
147
|
+
default(val, prop, ctx) {
|
|
148
|
+
return isDefault(val, prop, ctx);
|
|
149
|
+
},
|
|
150
|
+
}, 0);
|
|
151
|
+
p.vector = propParser({
|
|
152
|
+
size(val) {
|
|
153
|
+
expectNumber(val);
|
|
154
|
+
},
|
|
155
|
+
}, {
|
|
156
|
+
default(val, prop, ctx) {
|
|
157
|
+
return isDefault(val, prop, ctx);
|
|
97
158
|
},
|
|
98
159
|
}, 0);
|
|
99
160
|
p.enum = propParser({
|
|
@@ -111,10 +172,8 @@ p.enum = propParser({
|
|
|
111
172
|
}
|
|
112
173
|
},
|
|
113
174
|
}, {
|
|
114
|
-
default(val, prop) {
|
|
115
|
-
|
|
116
|
-
throw Error(EXPECTED_VALUE_IN_ENUM);
|
|
117
|
-
}
|
|
175
|
+
default(val, prop, ctx) {
|
|
176
|
+
return isDefault(val, prop, ctx);
|
|
118
177
|
},
|
|
119
178
|
}, 1);
|
|
120
179
|
const numberOpts = {
|
|
@@ -137,18 +196,8 @@ const numberOpts = {
|
|
|
137
196
|
throw Error(INVALID_VALUE);
|
|
138
197
|
}
|
|
139
198
|
},
|
|
140
|
-
default(val, prop) {
|
|
141
|
-
|
|
142
|
-
if (val > prop.max || val < prop.min) {
|
|
143
|
-
throw Error(OUT_OF_RANGE);
|
|
144
|
-
}
|
|
145
|
-
if (prop.step !== 'any') {
|
|
146
|
-
const min = typeof prop.min !== 'number' || prop.min === Infinity ? 0 : prop.min;
|
|
147
|
-
const v = val - min;
|
|
148
|
-
if (~~(v / prop.step) * prop.step !== v) {
|
|
149
|
-
throw Error(INVALID_VALUE);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
199
|
+
default(val, prop, ctx) {
|
|
200
|
+
return isDefault(val, prop, ctx);
|
|
152
201
|
},
|
|
153
202
|
};
|
|
154
203
|
p.number = propParser(STUB, numberOpts, 0);
|
|
@@ -162,11 +211,7 @@ p.object = propParser({
|
|
|
162
211
|
props(val, prop, ctx) {
|
|
163
212
|
ctx.parseProps(val, ctx.type);
|
|
164
213
|
},
|
|
165
|
-
}, {
|
|
166
|
-
default(val) {
|
|
167
|
-
console.warn('TODO object default value');
|
|
168
|
-
},
|
|
169
|
-
});
|
|
214
|
+
}, {});
|
|
170
215
|
p.set = propParser({
|
|
171
216
|
items(items, prop, ctx) {
|
|
172
217
|
expectObject(items);
|
|
@@ -209,16 +254,13 @@ p.references = propParser({
|
|
|
209
254
|
}
|
|
210
255
|
},
|
|
211
256
|
}, {
|
|
212
|
-
default(val, prop) {
|
|
213
|
-
|
|
214
|
-
// if (typeof val === 'object') {
|
|
215
|
-
// throwErr(ERRORS.EXPECTED_PRIMITIVE, prop, 'default')
|
|
216
|
-
// }
|
|
257
|
+
default(val, prop, ctx) {
|
|
258
|
+
return isDefault(val, prop, ctx);
|
|
217
259
|
},
|
|
218
260
|
});
|
|
219
261
|
const binaryOpts = {
|
|
220
|
-
default(val) {
|
|
221
|
-
|
|
262
|
+
default(val, prop, ctx) {
|
|
263
|
+
return isDefault(val, prop, ctx);
|
|
222
264
|
},
|
|
223
265
|
format(val) {
|
|
224
266
|
expectString(val);
|
|
@@ -261,8 +303,10 @@ p.text = propParser({
|
|
|
261
303
|
throw Error(TEXT_REQUIRES_LOCALES);
|
|
262
304
|
},
|
|
263
305
|
}, {
|
|
306
|
+
format: binaryOpts.format,
|
|
264
307
|
default(val, prop) {
|
|
265
|
-
console.warn('MAKE DEFAULT VALUE FOR TEXT')
|
|
308
|
+
// console.warn('MAKE DEFAULT VALUE FOR TEXT')
|
|
309
|
+
return true;
|
|
266
310
|
},
|
|
267
311
|
}, 0);
|
|
268
312
|
p.timestamp = propParser(STUB, {
|
|
@@ -271,11 +315,27 @@ p.timestamp = propParser(STUB, {
|
|
|
271
315
|
dateDisplaysSet ??= new Set(dateDisplays);
|
|
272
316
|
dateDisplaysSet.has(val);
|
|
273
317
|
},
|
|
274
|
-
|
|
275
|
-
if (typeof val !== '
|
|
276
|
-
throw Error(
|
|
318
|
+
min(val) {
|
|
319
|
+
if (typeof val !== 'string' && typeof val !== 'number') {
|
|
320
|
+
throw Error(INVALID_VALUE);
|
|
277
321
|
}
|
|
278
322
|
},
|
|
323
|
+
max(val) {
|
|
324
|
+
if (typeof val !== 'string' && typeof val !== 'number') {
|
|
325
|
+
throw Error(INVALID_VALUE);
|
|
326
|
+
}
|
|
327
|
+
},
|
|
328
|
+
step(val) {
|
|
329
|
+
if (typeof val !== 'string' && typeof val !== 'number') {
|
|
330
|
+
throw Error(INVALID_VALUE);
|
|
331
|
+
}
|
|
332
|
+
if (typeof val === 'string' && val.includes('now')) {
|
|
333
|
+
throw Error(INVALID_VALUE);
|
|
334
|
+
}
|
|
335
|
+
},
|
|
336
|
+
default(val, prop, ctx) {
|
|
337
|
+
return isDefault(val, prop, ctx);
|
|
338
|
+
},
|
|
279
339
|
on(val) {
|
|
280
340
|
if (val !== 'create' && val !== 'update') {
|
|
281
341
|
throw Error(INVALID_VALUE);
|
|
@@ -339,8 +399,8 @@ p.reference = propParser({
|
|
|
339
399
|
},
|
|
340
400
|
}, {
|
|
341
401
|
mime: binaryOpts.mime,
|
|
342
|
-
default(val) {
|
|
343
|
-
|
|
402
|
+
default(val, prop, ctx) {
|
|
403
|
+
return isDefault(val, prop, ctx);
|
|
344
404
|
},
|
|
345
405
|
edge(val, prop, ctx, key) {
|
|
346
406
|
const edgeAllowed = ctx.type && !ctx.inQuery;
|
|
@@ -368,14 +428,19 @@ p.reference = propParser({
|
|
|
368
428
|
},
|
|
369
429
|
});
|
|
370
430
|
p.alias = propParser(STUB, {
|
|
371
|
-
default(val) {
|
|
372
|
-
|
|
431
|
+
default(val, prop, ctx) {
|
|
432
|
+
return isDefault(val, prop, ctx);
|
|
373
433
|
},
|
|
374
434
|
format: binaryOpts.format,
|
|
375
435
|
}, 0);
|
|
376
|
-
p.
|
|
377
|
-
default(val) {
|
|
378
|
-
|
|
436
|
+
p.cardinality = propParser(STUB, {
|
|
437
|
+
default(val, prop, ctx) {
|
|
438
|
+
return isDefault(val, prop, ctx);
|
|
439
|
+
},
|
|
440
|
+
}, 0);
|
|
441
|
+
p.json = propParser(STUB, {
|
|
442
|
+
default(val, prop, ctx) {
|
|
443
|
+
return isDefault(val, prop, ctx);
|
|
379
444
|
},
|
|
380
445
|
}, 0);
|
|
381
446
|
export default p;
|