@based/schema 5.0.0-alpha.9 → 5.0.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.
Files changed (48) hide show
  1. package/dist/def/addEdges.js +26 -3
  2. package/dist/def/createEmptyDef.d.ts +13 -9
  3. package/dist/def/createEmptyDef.js +7 -3
  4. package/dist/def/defaultMap.js +28 -24
  5. package/dist/def/fillEmptyMain.d.ts +2 -2
  6. package/dist/def/fillEmptyMain.js +13 -4
  7. package/dist/def/index.d.ts +0 -1
  8. package/dist/def/index.js +0 -1
  9. package/dist/def/makeSeparateSort.js +6 -6
  10. package/dist/def/makeSeparateTextSort.js +12 -12
  11. package/dist/def/refSet.d.ts +7 -0
  12. package/dist/def/refSet.js +25 -0
  13. package/dist/def/selvaBuffer.js +77 -28
  14. package/dist/def/typeDef.d.ts +9 -3
  15. package/dist/def/typeDef.js +136 -59
  16. package/dist/def/typeIndexes.d.ts +40 -0
  17. package/dist/def/typeIndexes.js +50 -0
  18. package/dist/def/types.d.ts +36 -57
  19. package/dist/def/types.js +22 -28
  20. package/dist/def/utils.d.ts +5 -3
  21. package/dist/def/utils.js +44 -2
  22. package/dist/def/validation.d.ts +2 -0
  23. package/dist/def/validation.js +89 -41
  24. package/dist/index.d.ts +3 -1
  25. package/dist/index.js +3 -1
  26. package/dist/infer.d.ts +82 -0
  27. package/dist/infer.js +5 -0
  28. package/dist/lang.d.ts +3 -1
  29. package/dist/lang.js +6 -0
  30. package/dist/parse/assert.d.ts +4 -0
  31. package/dist/parse/assert.js +19 -2
  32. package/dist/parse/index.d.ts +2 -0
  33. package/dist/parse/index.js +58 -4
  34. package/dist/parse/props.d.ts +1 -0
  35. package/dist/parse/props.js +171 -54
  36. package/dist/serialize.d.ts +14 -0
  37. package/dist/serialize.js +543 -0
  38. package/dist/types.d.ts +74 -20
  39. package/dist/types.js +3 -1
  40. package/package.json +6 -4
  41. package/dist/def/getPropLen.d.ts +0 -3
  42. package/dist/def/getPropLen.js +0 -23
  43. package/dist/def/makePacked.d.ts +0 -3
  44. package/dist/def/makePacked.js +0 -50
  45. package/dist/def/readFromPacked.d.ts +0 -3
  46. package/dist/def/readFromPacked.js +0 -140
  47. package/dist/mermaid.d.ts +0 -3
  48. package/dist/mermaid.js +0 -24
@@ -1,7 +1,12 @@
1
+ import { convertToTimestamp } from '@based/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, expectFloat32Array, expectFunction, expectNumber, expectObject, expectString, } from './assert.js';
3
- import { EXPECTED_ARR, EXPECTED_DATE, EXPECTED_OBJ, EXPECTED_PRIMITIVE, EXPECTED_VALUE_IN_ENUM, INVALID_VALUE, MIN_MAX, MISSING_TYPE, OUT_OF_RANGE, TEXT_REQUIRES_LOCALES, TYPE_MISMATCH, UNKNOWN_PROP, NOT_ALLOWED_IN_ITEMS, } from './errors.js';
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,16 +43,29 @@ const shared = {
38
43
  throw Error(TYPE_MISMATCH);
39
44
  }
40
45
  },
41
- title(val) { },
42
- description(val) { },
43
- readOnly(val) { },
44
- examples(val) { },
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) => {
48
64
  if (typeof prop === 'string') {
49
65
  // allow string
50
66
  if (allowShorthand === 0) {
67
+ // @ts-ignore
68
+ required?.type?.(prop, { type: prop }, ctx);
51
69
  return;
52
70
  }
53
71
  throw Error(EXPECTED_OBJ);
@@ -91,9 +109,67 @@ function propParser(required, optional, allowShorthand) {
91
109
  };
92
110
  }
93
111
  const p = {};
112
+ export const isDefault = (val, prop, ctx) => {
113
+ let typeIndex;
114
+ typeIndex = TYPE_INDEX_MAP[prop.type];
115
+ if ('enum' in prop) {
116
+ typeIndex = TYPE_INDEX_MAP['enum'];
117
+ }
118
+ if (prop.type === 'timestamp') {
119
+ val = convertToTimestamp(val);
120
+ }
121
+ const validation = prop.validation || VALIDATION_MAP[typeIndex];
122
+ const propDef = {
123
+ typeIndex,
124
+ __isPropDef: true,
125
+ start: 0,
126
+ path: [],
127
+ prop: 0,
128
+ len: 0,
129
+ separate: false,
130
+ enum: prop.enum,
131
+ validation,
132
+ default: DEFAULT_MAP[typeIndex],
133
+ step: parseMinMaxStep((prop.step ?? typeIndex === NUMBER) ? 0 : 1),
134
+ max: parseMinMaxStep(prop.max),
135
+ min: parseMinMaxStep(prop.min),
136
+ };
137
+ if (prop.type === 'text') {
138
+ if (typeof val === 'object') {
139
+ for (const key in val) {
140
+ if (!ctx.schema.locales || !(key in ctx.schema.locales)) {
141
+ throw new Error(`Incorrect default for type "text" lang "${key}"`);
142
+ }
143
+ if (!validation(val[key], propDef)) {
144
+ throw new Error(`Incorrect default for type "text" lang "${key}"`);
145
+ }
146
+ }
147
+ }
148
+ else {
149
+ if (!validation(val, propDef)) {
150
+ throw new Error(`Incorrect default for type "text"`);
151
+ }
152
+ val = {};
153
+ for (const key in ctx.schema.locales) {
154
+ val[key] = ctx.schema.locales[key];
155
+ }
156
+ }
157
+ return val;
158
+ }
159
+ if (!validation(val, propDef)) {
160
+ throw new Error(`Incorrect default for type "${prop.type ?? 'enum'}"`);
161
+ }
162
+ if ('enum' in prop) {
163
+ if (val === undefined) {
164
+ return 0;
165
+ }
166
+ return prop.enum.findIndex((v) => v === val) + 1;
167
+ }
168
+ return val;
169
+ };
94
170
  p.boolean = propParser(STUB, {
95
- default(val) {
96
- expectBoolean(val);
171
+ default(val, prop, ctx) {
172
+ return isDefault(val, prop, ctx);
97
173
  },
98
174
  }, 0);
99
175
  p.vector = propParser({
@@ -101,8 +177,49 @@ p.vector = propParser({
101
177
  expectNumber(val);
102
178
  },
103
179
  }, {
104
- default(val) {
105
- expectFloat32Array(val);
180
+ default(val, prop, ctx) {
181
+ return isDefault(val, prop, ctx);
182
+ },
183
+ baseType(val, prop, ctx) {
184
+ if (![
185
+ 'number',
186
+ 'int8',
187
+ ,
188
+ 'uint8',
189
+ 'int16',
190
+ 'uint16',
191
+ 'int32',
192
+ 'uint32',
193
+ 'float32',
194
+ 'float64',
195
+ ].includes(val)) {
196
+ throw Error(INVALID_VALUE);
197
+ }
198
+ },
199
+ }, 0);
200
+ p.colvec = propParser({
201
+ size(val) {
202
+ expectNumber(val);
203
+ },
204
+ }, {
205
+ default(val, prop, ctx) {
206
+ return isDefault(val, prop, ctx);
207
+ },
208
+ baseType(val, prop, ctx) {
209
+ if (![
210
+ 'number',
211
+ 'int8',
212
+ ,
213
+ 'uint8',
214
+ 'int16',
215
+ 'uint16',
216
+ 'int32',
217
+ 'uint32',
218
+ 'float32',
219
+ 'float64',
220
+ ].includes(val)) {
221
+ throw Error(INVALID_VALUE);
222
+ }
106
223
  },
107
224
  }, 0);
108
225
  p.enum = propParser({
@@ -120,10 +237,8 @@ p.enum = propParser({
120
237
  }
121
238
  },
122
239
  }, {
123
- default(val, prop) {
124
- if (!prop.enum.includes(val)) {
125
- throw Error(EXPECTED_VALUE_IN_ENUM);
126
- }
240
+ default(val, prop, ctx) {
241
+ return isDefault(val, prop, ctx);
127
242
  },
128
243
  }, 1);
129
244
  const numberOpts = {
@@ -146,18 +261,8 @@ const numberOpts = {
146
261
  throw Error(INVALID_VALUE);
147
262
  }
148
263
  },
149
- default(val, prop) {
150
- expectNumber(val);
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
- }
264
+ default(val, prop, ctx) {
265
+ return isDefault(val, prop, ctx);
161
266
  },
162
267
  };
163
268
  p.number = propParser(STUB, numberOpts, 0);
@@ -168,14 +273,10 @@ p.uint16 = propParser(STUB, numberOpts, 0);
168
273
  p.int32 = propParser(STUB, numberOpts, 0);
169
274
  p.uint32 = propParser(STUB, numberOpts, 0);
170
275
  p.object = propParser({
171
- props(val, prop, ctx) {
276
+ props(val, _prop, ctx) {
172
277
  ctx.parseProps(val, ctx.type);
173
278
  },
174
- }, {
175
- default(val) {
176
- console.warn('TODO object default value');
177
- },
178
- });
279
+ }, {});
179
280
  p.set = propParser({
180
281
  items(items, prop, ctx) {
181
282
  expectObject(items);
@@ -218,16 +319,13 @@ p.references = propParser({
218
319
  }
219
320
  },
220
321
  }, {
221
- default(val, prop) {
222
- console.warn('TODO SET DEFAULT VALUE');
223
- // if (typeof val === 'object') {
224
- // throwErr(ERRORS.EXPECTED_PRIMITIVE, prop, 'default')
225
- // }
322
+ default(val, prop, ctx) {
323
+ return isDefault(val, prop, ctx);
226
324
  },
227
325
  });
228
326
  const binaryOpts = {
229
- default(val) {
230
- expectString(val);
327
+ default(val, prop, ctx) {
328
+ return isDefault(val, prop, ctx);
231
329
  },
232
330
  format(val) {
233
331
  expectString(val);
@@ -270,9 +368,12 @@ p.text = propParser({
270
368
  throw Error(TEXT_REQUIRES_LOCALES);
271
369
  },
272
370
  }, {
371
+ compression(val) {
372
+ return val;
373
+ },
273
374
  format: binaryOpts.format,
274
- default(val, prop) {
275
- console.warn('MAKE DEFAULT VALUE FOR TEXT');
375
+ default(val, prop, ctx) {
376
+ return isDefault(val, prop, ctx);
276
377
  },
277
378
  }, 0);
278
379
  p.timestamp = propParser(STUB, {
@@ -281,11 +382,27 @@ p.timestamp = propParser(STUB, {
281
382
  dateDisplaysSet ??= new Set(dateDisplays);
282
383
  dateDisplaysSet.has(val);
283
384
  },
284
- default(val) {
285
- if (typeof val !== 'number' && !(val instanceof Date)) {
286
- throw Error(EXPECTED_DATE);
385
+ min(val) {
386
+ if (typeof val !== 'string' && typeof val !== 'number') {
387
+ throw Error(INVALID_VALUE);
287
388
  }
288
389
  },
390
+ max(val) {
391
+ if (typeof val !== 'string' && typeof val !== 'number') {
392
+ throw Error(INVALID_VALUE);
393
+ }
394
+ },
395
+ step(val) {
396
+ if (typeof val !== 'string' && typeof val !== 'number') {
397
+ throw Error(INVALID_VALUE);
398
+ }
399
+ if (typeof val === 'string' && val.includes('now')) {
400
+ throw Error(INVALID_VALUE);
401
+ }
402
+ },
403
+ default(val, prop, ctx) {
404
+ return isDefault(val, prop, ctx);
405
+ },
289
406
  on(val) {
290
407
  if (val !== 'create' && val !== 'update') {
291
408
  throw Error(INVALID_VALUE);
@@ -298,7 +415,7 @@ p.reference = propParser({
298
415
  throw Error(MISSING_TYPE);
299
416
  }
300
417
  },
301
- prop(propKey, prop, { schema, type, inQuery, path }) {
418
+ prop(propKey, prop, { schema, type, inQuery, path, lvl }) {
302
419
  const propAllowed = type && !inQuery;
303
420
  if (propAllowed) {
304
421
  expectString(propKey);
@@ -316,7 +433,7 @@ p.reference = propParser({
316
433
  if (create) {
317
434
  const ref = path[1];
318
435
  let prop = '';
319
- for (let i = 3; i < path.length - 1; i += 2) {
436
+ for (let i = 3; i < lvl; i += 2) {
320
437
  prop += prop ? `.${path[i]}` : path[i];
321
438
  }
322
439
  targetProp.readOnly = true;
@@ -349,8 +466,8 @@ p.reference = propParser({
349
466
  },
350
467
  }, {
351
468
  mime: binaryOpts.mime,
352
- default(val) {
353
- expectString(val);
469
+ default(val, prop, ctx) {
470
+ return isDefault(val, prop, ctx);
354
471
  },
355
472
  edge(val, prop, ctx, key) {
356
473
  const edgeAllowed = ctx.type && !ctx.inQuery;
@@ -378,19 +495,19 @@ p.reference = propParser({
378
495
  },
379
496
  });
380
497
  p.alias = propParser(STUB, {
381
- default(val) {
382
- expectString(val);
498
+ default(val, prop, ctx) {
499
+ return isDefault(val, prop, ctx);
383
500
  },
384
501
  format: binaryOpts.format,
385
502
  }, 0);
386
503
  p.cardinality = propParser(STUB, {
387
- default(val) {
388
- expectNumber(val);
504
+ default(val, prop, ctx) {
505
+ return isDefault(val, prop, ctx);
389
506
  },
390
507
  }, 0);
391
508
  p.json = propParser(STUB, {
392
- default(val) {
393
- expectObject(val);
509
+ default(val, prop, ctx) {
510
+ return isDefault(val, prop, ctx);
394
511
  },
395
512
  }, 0);
396
513
  export default p;
@@ -0,0 +1,14 @@
1
+ import { StrictSchema } from './types.js';
2
+ type Opts = {
3
+ readOnly?: boolean;
4
+ stripMetaInformation?: boolean;
5
+ };
6
+ export declare const serialize: (schema: any, opts?: Opts) => Uint8Array;
7
+ export declare const deSerializeKey: (buf: Uint8Array, keySize: number, i: number) => {
8
+ size: number;
9
+ value: string;
10
+ };
11
+ export declare const deSerializeInner: (buf: Uint8Array, obj: any, start: number, fromArray: boolean) => number;
12
+ export declare const deSerialize: (buf: Uint8Array) => StrictSchema;
13
+ export {};
14
+ //# sourceMappingURL=serialize.d.ts.map