@based/schema 5.0.0-alpha.2 → 5.0.0-alpha.20
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 +40 -0
- package/dist/def/createEmptyDef.js +43 -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/index.d.ts +8 -0
- package/dist/def/index.js +9 -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/selvaBuffer.d.ts +5 -0
- package/dist/def/selvaBuffer.js +136 -0
- package/dist/def/typeDef.d.ts +14 -0
- package/dist/def/typeDef.js +203 -0
- package/dist/def/types.d.ts +179 -0
- package/dist/def/types.js +132 -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 +2 -1
- package/dist/index.js +2 -1
- package/dist/lang.d.ts +3 -1
- package/dist/lang.js +2 -0
- package/dist/parse/assert.js +1 -2
- package/dist/parse/index.d.ts +1 -1
- package/dist/parse/index.js +9 -5
- package/dist/parse/props.d.ts +1 -0
- package/dist/parse/props.js +109 -54
- package/dist/serialize.d.ts +5 -0
- package/dist/serialize.js +142 -0
- package/dist/types.d.ts +19 -8
- package/dist/types.js +2 -0
- package/package.json +9 -4
- package/dist/mermaid.d.ts +0 -3
- package/dist/mermaid.js +0 -24
|
@@ -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
|
@@ -144,8 +144,10 @@ declare const langCodes: {
|
|
|
144
144
|
readonly yi: 142;
|
|
145
145
|
readonly yo: 143;
|
|
146
146
|
readonly zu: 144;
|
|
147
|
+
readonly ka: 145;
|
|
148
|
+
readonly cnr: 146;
|
|
147
149
|
};
|
|
148
|
-
export declare const langCodesMap: Map<string, 0 |
|
|
150
|
+
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 | 145 | 146>;
|
|
149
151
|
export declare const inverseLangMap: Map<any, any>;
|
|
150
152
|
export type LangName = keyof typeof langCodes;
|
|
151
153
|
export type LangCode = (typeof langCodes)[LangName];
|
package/dist/lang.js
CHANGED
package/dist/parse/assert.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { isFloat32Array } from 'util/types';
|
|
2
1
|
import { EXPECTED_ARR, EXPECTED_BOOL, EXPECTED_FN, EXPECTED_NUM, EXPECTED_OBJ, EXPECTED_STR, } from './errors.js';
|
|
3
2
|
export const expectObject = (obj, msg) => {
|
|
4
3
|
if (typeof obj !== 'object' || obj === null) {
|
|
@@ -6,7 +5,7 @@ export const expectObject = (obj, msg) => {
|
|
|
6
5
|
}
|
|
7
6
|
};
|
|
8
7
|
export const expectFloat32Array = (arr) => {
|
|
9
|
-
if (!
|
|
8
|
+
if (!(arr instanceof Float32Array)) {
|
|
10
9
|
throw Error(EXPECTED_ARR);
|
|
11
10
|
}
|
|
12
11
|
};
|
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;
|
|
@@ -63,6 +64,10 @@ export class SchemaParser {
|
|
|
63
64
|
expectObject(locales);
|
|
64
65
|
for (const locale in locales) {
|
|
65
66
|
const opts = locales[locale];
|
|
67
|
+
if (opts === true) {
|
|
68
|
+
console.log(locale, opts);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
66
71
|
expectObject(opts);
|
|
67
72
|
for (const key in opts) {
|
|
68
73
|
const val = opts[key];
|
|
@@ -70,7 +75,7 @@ export class SchemaParser {
|
|
|
70
75
|
expectBoolean(val);
|
|
71
76
|
}
|
|
72
77
|
else if (key === 'fallback') {
|
|
73
|
-
if (
|
|
78
|
+
if (Array.isArray(val) || typeof val !== 'string') {
|
|
74
79
|
throw Error(INVALID_VALUE);
|
|
75
80
|
}
|
|
76
81
|
}
|
|
@@ -97,6 +102,7 @@ export class SchemaParser {
|
|
|
97
102
|
throw Error(UNKNOWN_PROP);
|
|
98
103
|
}
|
|
99
104
|
}
|
|
105
|
+
return this.schema;
|
|
100
106
|
}
|
|
101
107
|
}
|
|
102
108
|
export const print = (schema, path) => {
|
|
@@ -120,9 +126,7 @@ export const print = (schema, path) => {
|
|
|
120
126
|
export const parse = (schema) => {
|
|
121
127
|
const parser = new SchemaParser(schema);
|
|
122
128
|
try {
|
|
123
|
-
parser.parse();
|
|
124
|
-
// @ts-ignore
|
|
125
|
-
return { schema };
|
|
129
|
+
return { schema: parser.parse() };
|
|
126
130
|
}
|
|
127
131
|
catch (e) {
|
|
128
132
|
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,
|
|
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,45 @@ 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);
|
|
97
149
|
},
|
|
98
150
|
}, 0);
|
|
99
151
|
p.vector = propParser({
|
|
@@ -101,8 +153,8 @@ p.vector = propParser({
|
|
|
101
153
|
expectNumber(val);
|
|
102
154
|
},
|
|
103
155
|
}, {
|
|
104
|
-
default(val) {
|
|
105
|
-
|
|
156
|
+
default(val, prop, ctx) {
|
|
157
|
+
return isDefault(val, prop, ctx);
|
|
106
158
|
},
|
|
107
159
|
}, 0);
|
|
108
160
|
p.enum = propParser({
|
|
@@ -120,10 +172,8 @@ p.enum = propParser({
|
|
|
120
172
|
}
|
|
121
173
|
},
|
|
122
174
|
}, {
|
|
123
|
-
default(val, prop) {
|
|
124
|
-
|
|
125
|
-
throw Error(EXPECTED_VALUE_IN_ENUM);
|
|
126
|
-
}
|
|
175
|
+
default(val, prop, ctx) {
|
|
176
|
+
return isDefault(val, prop, ctx);
|
|
127
177
|
},
|
|
128
178
|
}, 1);
|
|
129
179
|
const numberOpts = {
|
|
@@ -146,18 +196,8 @@ const numberOpts = {
|
|
|
146
196
|
throw Error(INVALID_VALUE);
|
|
147
197
|
}
|
|
148
198
|
},
|
|
149
|
-
default(val, prop) {
|
|
150
|
-
|
|
151
|
-
if (val > prop.max || val < prop.min) {
|
|
152
|
-
throw Error(OUT_OF_RANGE);
|
|
153
|
-
}
|
|
154
|
-
if (prop.step !== 'any') {
|
|
155
|
-
const min = typeof prop.min !== 'number' || prop.min === Infinity ? 0 : prop.min;
|
|
156
|
-
const v = val - min;
|
|
157
|
-
if (~~(v / prop.step) * prop.step !== v) {
|
|
158
|
-
throw Error(INVALID_VALUE);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
199
|
+
default(val, prop, ctx) {
|
|
200
|
+
return isDefault(val, prop, ctx);
|
|
161
201
|
},
|
|
162
202
|
};
|
|
163
203
|
p.number = propParser(STUB, numberOpts, 0);
|
|
@@ -168,14 +208,10 @@ p.uint16 = propParser(STUB, numberOpts, 0);
|
|
|
168
208
|
p.int32 = propParser(STUB, numberOpts, 0);
|
|
169
209
|
p.uint32 = propParser(STUB, numberOpts, 0);
|
|
170
210
|
p.object = propParser({
|
|
171
|
-
props(val,
|
|
211
|
+
props(val, _prop, ctx) {
|
|
172
212
|
ctx.parseProps(val, ctx.type);
|
|
173
213
|
},
|
|
174
|
-
}, {
|
|
175
|
-
default(val) {
|
|
176
|
-
console.warn('TODO object default value');
|
|
177
|
-
},
|
|
178
|
-
});
|
|
214
|
+
}, {});
|
|
179
215
|
p.set = propParser({
|
|
180
216
|
items(items, prop, ctx) {
|
|
181
217
|
expectObject(items);
|
|
@@ -218,16 +254,13 @@ p.references = propParser({
|
|
|
218
254
|
}
|
|
219
255
|
},
|
|
220
256
|
}, {
|
|
221
|
-
default(val, prop) {
|
|
222
|
-
|
|
223
|
-
// if (typeof val === 'object') {
|
|
224
|
-
// throwErr(ERRORS.EXPECTED_PRIMITIVE, prop, 'default')
|
|
225
|
-
// }
|
|
257
|
+
default(val, prop, ctx) {
|
|
258
|
+
return isDefault(val, prop, ctx);
|
|
226
259
|
},
|
|
227
260
|
});
|
|
228
261
|
const binaryOpts = {
|
|
229
|
-
default(val) {
|
|
230
|
-
|
|
262
|
+
default(val, prop, ctx) {
|
|
263
|
+
return isDefault(val, prop, ctx);
|
|
231
264
|
},
|
|
232
265
|
format(val) {
|
|
233
266
|
expectString(val);
|
|
@@ -270,8 +303,14 @@ p.text = propParser({
|
|
|
270
303
|
throw Error(TEXT_REQUIRES_LOCALES);
|
|
271
304
|
},
|
|
272
305
|
}, {
|
|
273
|
-
|
|
274
|
-
|
|
306
|
+
compression(val) {
|
|
307
|
+
// return the actualy string!
|
|
308
|
+
return val;
|
|
309
|
+
},
|
|
310
|
+
format: binaryOpts.format,
|
|
311
|
+
default(_val, _prop) {
|
|
312
|
+
// console.warn('MAKE DEFAULT VALUE FOR TEXT')
|
|
313
|
+
return true;
|
|
275
314
|
},
|
|
276
315
|
}, 0);
|
|
277
316
|
p.timestamp = propParser(STUB, {
|
|
@@ -280,11 +319,27 @@ p.timestamp = propParser(STUB, {
|
|
|
280
319
|
dateDisplaysSet ??= new Set(dateDisplays);
|
|
281
320
|
dateDisplaysSet.has(val);
|
|
282
321
|
},
|
|
283
|
-
|
|
284
|
-
if (typeof val !== '
|
|
285
|
-
throw Error(
|
|
322
|
+
min(val) {
|
|
323
|
+
if (typeof val !== 'string' && typeof val !== 'number') {
|
|
324
|
+
throw Error(INVALID_VALUE);
|
|
325
|
+
}
|
|
326
|
+
},
|
|
327
|
+
max(val) {
|
|
328
|
+
if (typeof val !== 'string' && typeof val !== 'number') {
|
|
329
|
+
throw Error(INVALID_VALUE);
|
|
286
330
|
}
|
|
287
331
|
},
|
|
332
|
+
step(val) {
|
|
333
|
+
if (typeof val !== 'string' && typeof val !== 'number') {
|
|
334
|
+
throw Error(INVALID_VALUE);
|
|
335
|
+
}
|
|
336
|
+
if (typeof val === 'string' && val.includes('now')) {
|
|
337
|
+
throw Error(INVALID_VALUE);
|
|
338
|
+
}
|
|
339
|
+
},
|
|
340
|
+
default(val, prop, ctx) {
|
|
341
|
+
return isDefault(val, prop, ctx);
|
|
342
|
+
},
|
|
288
343
|
on(val) {
|
|
289
344
|
if (val !== 'create' && val !== 'update') {
|
|
290
345
|
throw Error(INVALID_VALUE);
|
|
@@ -297,7 +352,7 @@ p.reference = propParser({
|
|
|
297
352
|
throw Error(MISSING_TYPE);
|
|
298
353
|
}
|
|
299
354
|
},
|
|
300
|
-
prop(propKey, prop, { schema, type, inQuery, path }) {
|
|
355
|
+
prop(propKey, prop, { schema, type, inQuery, path, lvl }) {
|
|
301
356
|
const propAllowed = type && !inQuery;
|
|
302
357
|
if (propAllowed) {
|
|
303
358
|
expectString(propKey);
|
|
@@ -315,7 +370,7 @@ p.reference = propParser({
|
|
|
315
370
|
if (create) {
|
|
316
371
|
const ref = path[1];
|
|
317
372
|
let prop = '';
|
|
318
|
-
for (let i = 3; i <
|
|
373
|
+
for (let i = 3; i < lvl; i += 2) {
|
|
319
374
|
prop += prop ? `.${path[i]}` : path[i];
|
|
320
375
|
}
|
|
321
376
|
targetProp.readOnly = true;
|
|
@@ -348,8 +403,8 @@ p.reference = propParser({
|
|
|
348
403
|
},
|
|
349
404
|
}, {
|
|
350
405
|
mime: binaryOpts.mime,
|
|
351
|
-
default(val) {
|
|
352
|
-
|
|
406
|
+
default(val, prop, ctx) {
|
|
407
|
+
return isDefault(val, prop, ctx);
|
|
353
408
|
},
|
|
354
409
|
edge(val, prop, ctx, key) {
|
|
355
410
|
const edgeAllowed = ctx.type && !ctx.inQuery;
|
|
@@ -377,19 +432,19 @@ p.reference = propParser({
|
|
|
377
432
|
},
|
|
378
433
|
});
|
|
379
434
|
p.alias = propParser(STUB, {
|
|
380
|
-
default(val) {
|
|
381
|
-
|
|
435
|
+
default(val, prop, ctx) {
|
|
436
|
+
return isDefault(val, prop, ctx);
|
|
382
437
|
},
|
|
383
438
|
format: binaryOpts.format,
|
|
384
439
|
}, 0);
|
|
385
440
|
p.cardinality = propParser(STUB, {
|
|
386
|
-
default(val) {
|
|
387
|
-
|
|
441
|
+
default(val, prop, ctx) {
|
|
442
|
+
return isDefault(val, prop, ctx);
|
|
388
443
|
},
|
|
389
444
|
}, 0);
|
|
390
445
|
p.json = propParser(STUB, {
|
|
391
|
-
default(val) {
|
|
392
|
-
|
|
446
|
+
default(val, prop, ctx) {
|
|
447
|
+
return isDefault(val, prop, ctx);
|
|
393
448
|
},
|
|
394
449
|
}, 0);
|
|
395
450
|
export default p;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { StrictSchema } from './types.js';
|
|
2
|
+
export declare const serialize: (schema: any, noCompression?: boolean) => Uint8Array;
|
|
3
|
+
export declare const deSerializeInner: (buf: Uint8Array, obj: any, start: number) => number;
|
|
4
|
+
export declare const deSerialize: (buf: Uint8Array) => StrictSchema;
|
|
5
|
+
//# sourceMappingURL=serialize.d.ts.map
|