@based/schema 5.0.0-alpha.2 → 5.0.0-alpha.21

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 (50) hide show
  1. package/dist/def/DEFAULT_MAP.d.ts +3 -0
  2. package/dist/def/DEFAULT_MAP.js +29 -0
  3. package/dist/def/addEdges.d.ts +4 -0
  4. package/dist/def/addEdges.js +76 -0
  5. package/dist/def/createEmptyDef.d.ts +42 -0
  6. package/dist/def/createEmptyDef.js +45 -0
  7. package/dist/def/defaultMap.d.ts +3 -0
  8. package/dist/def/defaultMap.js +28 -0
  9. package/dist/def/fillEmptyMain.d.ts +5 -0
  10. package/dist/def/fillEmptyMain.js +61 -0
  11. package/dist/def/getPropLen.d.ts +3 -0
  12. package/dist/def/getPropLen.js +23 -0
  13. package/dist/def/index.d.ts +8 -0
  14. package/dist/def/index.js +9 -0
  15. package/dist/def/makePacked.d.ts +3 -0
  16. package/dist/def/makePacked.js +50 -0
  17. package/dist/def/makeSeparateSort.d.ts +3 -0
  18. package/dist/def/makeSeparateSort.js +27 -0
  19. package/dist/def/makeSeparateTextSort.d.ts +3 -0
  20. package/dist/def/makeSeparateTextSort.js +38 -0
  21. package/dist/def/readFromPacked.d.ts +3 -0
  22. package/dist/def/readFromPacked.js +140 -0
  23. package/dist/def/selvaBuffer.d.ts +5 -0
  24. package/dist/def/selvaBuffer.js +145 -0
  25. package/dist/def/timestamp.d.ts +2 -0
  26. package/dist/def/timestamp.js +67 -0
  27. package/dist/def/typeDef.d.ts +13 -0
  28. package/dist/def/typeDef.js +231 -0
  29. package/dist/def/types.d.ts +186 -0
  30. package/dist/def/types.js +138 -0
  31. package/dist/def/utils.d.ts +8 -0
  32. package/dist/def/utils.js +62 -0
  33. package/dist/def/validation.d.ts +7 -0
  34. package/dist/def/validation.js +259 -0
  35. package/dist/index.d.ts +2 -1
  36. package/dist/index.js +2 -1
  37. package/dist/lang.d.ts +3 -1
  38. package/dist/lang.js +2 -0
  39. package/dist/parse/assert.js +1 -2
  40. package/dist/parse/index.d.ts +1 -1
  41. package/dist/parse/index.js +8 -5
  42. package/dist/parse/props.d.ts +1 -0
  43. package/dist/parse/props.js +118 -54
  44. package/dist/serialize.d.ts +14 -0
  45. package/dist/serialize.js +473 -0
  46. package/dist/types.d.ts +29 -9
  47. package/dist/types.js +2 -0
  48. package/dist/validation/validation.d.ts +2 -0
  49. package/dist/validation/validation.js +6 -0
  50. package/package.json +8 -4
@@ -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, 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,10 +43,21 @@ 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) => {
@@ -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
- expectBoolean(val);
147
+ default(val, prop, ctx) {
148
+ return isDefault(val, prop, ctx);
97
149
  },
98
150
  }, 0);
99
151
  p.vector = propParser({
@@ -101,8 +153,17 @@ p.vector = propParser({
101
153
  expectNumber(val);
102
154
  },
103
155
  }, {
104
- default(val) {
105
- expectFloat32Array(val);
156
+ default(val, prop, ctx) {
157
+ return isDefault(val, prop, ctx);
158
+ },
159
+ }, 0);
160
+ p.colvec = propParser({
161
+ size(val) {
162
+ expectNumber(val);
163
+ },
164
+ }, {
165
+ default(val, prop, ctx) {
166
+ return isDefault(val, prop, ctx);
106
167
  },
107
168
  }, 0);
108
169
  p.enum = propParser({
@@ -120,10 +181,8 @@ p.enum = propParser({
120
181
  }
121
182
  },
122
183
  }, {
123
- default(val, prop) {
124
- if (!prop.enum.includes(val)) {
125
- throw Error(EXPECTED_VALUE_IN_ENUM);
126
- }
184
+ default(val, prop, ctx) {
185
+ return isDefault(val, prop, ctx);
127
186
  },
128
187
  }, 1);
129
188
  const numberOpts = {
@@ -146,18 +205,8 @@ const numberOpts = {
146
205
  throw Error(INVALID_VALUE);
147
206
  }
148
207
  },
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
- }
208
+ default(val, prop, ctx) {
209
+ return isDefault(val, prop, ctx);
161
210
  },
162
211
  };
163
212
  p.number = propParser(STUB, numberOpts, 0);
@@ -168,14 +217,10 @@ p.uint16 = propParser(STUB, numberOpts, 0);
168
217
  p.int32 = propParser(STUB, numberOpts, 0);
169
218
  p.uint32 = propParser(STUB, numberOpts, 0);
170
219
  p.object = propParser({
171
- props(val, prop, ctx) {
220
+ props(val, _prop, ctx) {
172
221
  ctx.parseProps(val, ctx.type);
173
222
  },
174
- }, {
175
- default(val) {
176
- console.warn('TODO object default value');
177
- },
178
- });
223
+ }, {});
179
224
  p.set = propParser({
180
225
  items(items, prop, ctx) {
181
226
  expectObject(items);
@@ -218,16 +263,13 @@ p.references = propParser({
218
263
  }
219
264
  },
220
265
  }, {
221
- default(val, prop) {
222
- console.warn('TODO SET DEFAULT VALUE');
223
- // if (typeof val === 'object') {
224
- // throwErr(ERRORS.EXPECTED_PRIMITIVE, prop, 'default')
225
- // }
266
+ default(val, prop, ctx) {
267
+ return isDefault(val, prop, ctx);
226
268
  },
227
269
  });
228
270
  const binaryOpts = {
229
- default(val) {
230
- expectString(val);
271
+ default(val, prop, ctx) {
272
+ return isDefault(val, prop, ctx);
231
273
  },
232
274
  format(val) {
233
275
  expectString(val);
@@ -270,8 +312,14 @@ p.text = propParser({
270
312
  throw Error(TEXT_REQUIRES_LOCALES);
271
313
  },
272
314
  }, {
273
- default(val, prop) {
274
- console.warn('MAKE DEFAULT VALUE FOR TEXT');
315
+ compression(val) {
316
+ // return the actualy string!
317
+ return val;
318
+ },
319
+ format: binaryOpts.format,
320
+ default(_val, _prop) {
321
+ // console.warn('MAKE DEFAULT VALUE FOR TEXT')
322
+ return true;
275
323
  },
276
324
  }, 0);
277
325
  p.timestamp = propParser(STUB, {
@@ -280,11 +328,27 @@ p.timestamp = propParser(STUB, {
280
328
  dateDisplaysSet ??= new Set(dateDisplays);
281
329
  dateDisplaysSet.has(val);
282
330
  },
283
- default(val) {
284
- if (typeof val !== 'number' && !(val instanceof Date)) {
285
- throw Error(EXPECTED_DATE);
331
+ min(val) {
332
+ if (typeof val !== 'string' && typeof val !== 'number') {
333
+ throw Error(INVALID_VALUE);
286
334
  }
287
335
  },
336
+ max(val) {
337
+ if (typeof val !== 'string' && typeof val !== 'number') {
338
+ throw Error(INVALID_VALUE);
339
+ }
340
+ },
341
+ step(val) {
342
+ if (typeof val !== 'string' && typeof val !== 'number') {
343
+ throw Error(INVALID_VALUE);
344
+ }
345
+ if (typeof val === 'string' && val.includes('now')) {
346
+ throw Error(INVALID_VALUE);
347
+ }
348
+ },
349
+ default(val, prop, ctx) {
350
+ return isDefault(val, prop, ctx);
351
+ },
288
352
  on(val) {
289
353
  if (val !== 'create' && val !== 'update') {
290
354
  throw Error(INVALID_VALUE);
@@ -297,7 +361,7 @@ p.reference = propParser({
297
361
  throw Error(MISSING_TYPE);
298
362
  }
299
363
  },
300
- prop(propKey, prop, { schema, type, inQuery, path }) {
364
+ prop(propKey, prop, { schema, type, inQuery, path, lvl }) {
301
365
  const propAllowed = type && !inQuery;
302
366
  if (propAllowed) {
303
367
  expectString(propKey);
@@ -315,7 +379,7 @@ p.reference = propParser({
315
379
  if (create) {
316
380
  const ref = path[1];
317
381
  let prop = '';
318
- for (let i = 3; i < path.length - 1; i += 2) {
382
+ for (let i = 3; i < lvl; i += 2) {
319
383
  prop += prop ? `.${path[i]}` : path[i];
320
384
  }
321
385
  targetProp.readOnly = true;
@@ -348,8 +412,8 @@ p.reference = propParser({
348
412
  },
349
413
  }, {
350
414
  mime: binaryOpts.mime,
351
- default(val) {
352
- expectString(val);
415
+ default(val, prop, ctx) {
416
+ return isDefault(val, prop, ctx);
353
417
  },
354
418
  edge(val, prop, ctx, key) {
355
419
  const edgeAllowed = ctx.type && !ctx.inQuery;
@@ -377,19 +441,19 @@ p.reference = propParser({
377
441
  },
378
442
  });
379
443
  p.alias = propParser(STUB, {
380
- default(val) {
381
- expectString(val);
444
+ default(val, prop, ctx) {
445
+ return isDefault(val, prop, ctx);
382
446
  },
383
447
  format: binaryOpts.format,
384
448
  }, 0);
385
449
  p.cardinality = propParser(STUB, {
386
- default(val) {
387
- expectNumber(val);
450
+ default(val, prop, ctx) {
451
+ return isDefault(val, prop, ctx);
388
452
  },
389
453
  }, 0);
390
454
  p.json = propParser(STUB, {
391
- default(val) {
392
- expectObject(val);
455
+ default(val, prop, ctx) {
456
+ return isDefault(val, prop, ctx);
393
457
  },
394
458
  }, 0);
395
459
  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