@based/schema 5.0.0-alpha.8 → 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 (49) hide show
  1. package/dist/def/addEdges.js +30 -3
  2. package/dist/def/createEmptyDef.d.ts +13 -9
  3. package/dist/def/createEmptyDef.js +7 -3
  4. package/dist/def/defaultMap.d.ts +3 -0
  5. package/dist/def/defaultMap.js +31 -0
  6. package/dist/def/fillEmptyMain.d.ts +2 -2
  7. package/dist/def/fillEmptyMain.js +14 -8
  8. package/dist/def/index.d.ts +2 -1
  9. package/dist/def/index.js +2 -1
  10. package/dist/def/makeSeparateSort.js +6 -6
  11. package/dist/def/makeSeparateTextSort.js +12 -12
  12. package/dist/def/refSet.d.ts +7 -0
  13. package/dist/def/refSet.js +25 -0
  14. package/dist/def/selvaBuffer.js +77 -28
  15. package/dist/def/typeDef.d.ts +9 -3
  16. package/dist/def/typeDef.js +143 -56
  17. package/dist/def/typeIndexes.d.ts +40 -0
  18. package/dist/def/typeIndexes.js +50 -0
  19. package/dist/def/types.d.ts +38 -60
  20. package/dist/def/types.js +24 -61
  21. package/dist/def/utils.d.ts +5 -3
  22. package/dist/def/utils.js +44 -2
  23. package/dist/def/validation.d.ts +7 -0
  24. package/dist/def/validation.js +261 -0
  25. package/dist/index.d.ts +3 -1
  26. package/dist/index.js +3 -1
  27. package/dist/infer.d.ts +82 -0
  28. package/dist/infer.js +5 -0
  29. package/dist/lang.d.ts +3 -1
  30. package/dist/lang.js +6 -0
  31. package/dist/parse/assert.d.ts +4 -0
  32. package/dist/parse/assert.js +19 -2
  33. package/dist/parse/index.d.ts +2 -0
  34. package/dist/parse/index.js +58 -4
  35. package/dist/parse/props.d.ts +1 -0
  36. package/dist/parse/props.js +171 -54
  37. package/dist/serialize.d.ts +14 -0
  38. package/dist/serialize.js +543 -0
  39. package/dist/types.d.ts +75 -19
  40. package/dist/types.js +3 -1
  41. package/package.json +7 -5
  42. package/dist/def/getPropLen.d.ts +0 -3
  43. package/dist/def/getPropLen.js +0 -23
  44. package/dist/def/makePacked.d.ts +0 -3
  45. package/dist/def/makePacked.js +0 -50
  46. package/dist/def/readFromPacked.d.ts +0 -3
  47. package/dist/def/readFromPacked.js +0 -137
  48. package/dist/mermaid.d.ts +0 -3
  49. package/dist/mermaid.js +0 -24
package/dist/def/utils.js CHANGED
@@ -1,8 +1,11 @@
1
- import { INT16, INT32, INT8, UINT16, UINT32, UINT8, NUMBER, TIMESTAMP, SIZE_MAP, } from './types.js';
1
+ import { INT16, INT32, INT8, UINT16, UINT32, UINT8, NUMBER, TIMESTAMP, SIZE_MAP, VECTOR_BASE_TYPE_SIZE_MAP, VectorBaseType, } from './types.js';
2
2
  import { isPropType } from '../types.js';
3
3
  import { getPropType } from '../parse/utils.js';
4
+ import { convertToTimestamp } from '@based/utils';
4
5
  export function isSeparate(schemaProp, len) {
5
- return len === 0 || isPropType('vector', schemaProp);
6
+ return (len === 0 ||
7
+ isPropType('vector', schemaProp) ||
8
+ isPropType('colvec', schemaProp));
6
9
  }
7
10
  export const propIsSigned = (prop) => {
8
11
  const t = prop.typeIndex;
@@ -25,6 +28,28 @@ export const propIsNumerical = (prop) => {
25
28
  }
26
29
  return false;
27
30
  };
31
+ export const schemaVectorBaseTypeToEnum = (vector) => {
32
+ switch (vector) {
33
+ case 'int8':
34
+ return VectorBaseType.Int8;
35
+ case 'uint8':
36
+ return VectorBaseType.Uint8;
37
+ case 'int16':
38
+ return VectorBaseType.Int16;
39
+ case 'uint16':
40
+ return VectorBaseType.Uint16;
41
+ case 'int32':
42
+ return VectorBaseType.Int32;
43
+ case 'uint32':
44
+ return VectorBaseType.Uint32;
45
+ case 'float32':
46
+ return VectorBaseType.Float32;
47
+ case 'float64':
48
+ return VectorBaseType.Float64;
49
+ case 'number':
50
+ return VectorBaseType.Float64;
51
+ }
52
+ };
28
53
  export function getPropLen(schemaProp) {
29
54
  let len = SIZE_MAP[getPropType(schemaProp)];
30
55
  if (isPropType('string', schemaProp) ||
@@ -42,6 +67,23 @@ export function getPropLen(schemaProp) {
42
67
  else if (isPropType('vector', schemaProp)) {
43
68
  len = 4 * schemaProp.size;
44
69
  }
70
+ else if (isPropType('colvec', schemaProp)) {
71
+ len =
72
+ schemaProp.size *
73
+ VECTOR_BASE_TYPE_SIZE_MAP[schemaVectorBaseTypeToEnum(schemaProp.baseType) ??
74
+ VectorBaseType.Float64];
75
+ }
45
76
  return len;
46
77
  }
78
+ export const parseMinMaxStep = (val) => {
79
+ if (typeof val === 'number') {
80
+ return val;
81
+ }
82
+ if (typeof val === 'string') {
83
+ if (!val.includes('now')) {
84
+ return convertToTimestamp(val);
85
+ }
86
+ return val;
87
+ }
88
+ };
47
89
  //# sourceMappingURL=utils.js.map
@@ -0,0 +1,7 @@
1
+ import { TypeIndex, PropDef, PropDefEdge } from './types.js';
2
+ export type Validation = (payload: any, prop: PropDef | PropDefEdge) => boolean;
3
+ export declare const VALIDATION_MAP: Record<TypeIndex, Validation>;
4
+ export declare const defaultValidation: () => boolean;
5
+ export declare const isValidId: (id: number) => boolean;
6
+ export declare const isValidString: (v: any) => boolean;
7
+ //# sourceMappingURL=validation.d.ts.map
@@ -0,0 +1,261 @@
1
+ import { convertToTimestamp } from '@based/utils';
2
+ import { ALIAS, BINARY, JSON, BOOLEAN, CARDINALITY, TIMESTAMP, INT16, INT32, INT8, UINT8, UINT16, UINT32, NUMBER, ENUM, ID, MICRO_BUFFER, REFERENCE, REFERENCES, STRING, TEXT, ALIASES, VECTOR, COLVEC, WEAK_REFERENCE, WEAK_REFERENCES, NULL, OBJECT, } 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
+ [NULL]: () => true,
7
+ [OBJECT]: () => true,
8
+ [COLVEC]: () => true,
9
+ [WEAK_REFERENCE]: () => true,
10
+ [WEAK_REFERENCES]: () => true,
11
+ [ALIAS]: (value) => {
12
+ if (typeof value !== 'string') {
13
+ return false;
14
+ }
15
+ return true;
16
+ },
17
+ [BINARY]: (value) => {
18
+ if (value instanceof Uint8Array) {
19
+ return true;
20
+ }
21
+ return false;
22
+ },
23
+ [BOOLEAN]: (value) => {
24
+ if (typeof value !== 'boolean') {
25
+ return false;
26
+ }
27
+ return true;
28
+ },
29
+ [CARDINALITY]: (val) => {
30
+ return (typeof val === 'string' ||
31
+ (val instanceof Uint8Array && val.byteLength === 8));
32
+ },
33
+ [TIMESTAMP]: (value, t) => {
34
+ if (typeof value !== 'number' || value % t.step !== 0) {
35
+ return false;
36
+ }
37
+ if (t.min !== undefined) {
38
+ if (typeof t.min === 'number') {
39
+ if (value < t.min) {
40
+ return false;
41
+ }
42
+ }
43
+ else if (value < convertToTimestamp(t.min)) {
44
+ return false;
45
+ }
46
+ }
47
+ if (t.max !== undefined) {
48
+ if (typeof t.max === 'number') {
49
+ if (value > t.max) {
50
+ return false;
51
+ }
52
+ }
53
+ else if (value > convertToTimestamp(t.max)) {
54
+ return false;
55
+ }
56
+ }
57
+ return true;
58
+ },
59
+ [INT16]: (value, t) => {
60
+ if (typeof value !== 'number' || value % t.step !== 0) {
61
+ return false;
62
+ }
63
+ if (value > 32767 || value < -32768) {
64
+ return false;
65
+ }
66
+ if (t.min !== undefined && value < t.min) {
67
+ return false;
68
+ }
69
+ if (t.max !== undefined && value > t.max) {
70
+ return false;
71
+ }
72
+ return true;
73
+ },
74
+ [INT32]: (value, t) => {
75
+ if (typeof value !== 'number' || value % t.step !== 0) {
76
+ return false;
77
+ }
78
+ if (value > 2147483647 || value < -2147483648) {
79
+ return false;
80
+ }
81
+ if (t.min !== undefined && value < t.min) {
82
+ return false;
83
+ }
84
+ if (t.max !== undefined && value > t.max) {
85
+ return false;
86
+ }
87
+ return true;
88
+ },
89
+ [INT8]: (value, t) => {
90
+ // use % for steps size
91
+ if (typeof value !== 'number' || value % t.step !== 0) {
92
+ return false;
93
+ }
94
+ if (value > 127 || value < -128) {
95
+ return false;
96
+ }
97
+ if (t.min !== undefined && value < t.min) {
98
+ return false;
99
+ }
100
+ if (t.max !== undefined && value > t.max) {
101
+ return false;
102
+ }
103
+ return true;
104
+ },
105
+ [UINT8]: (value, t) => {
106
+ if (typeof value !== 'number' || value % t.step !== 0) {
107
+ return false;
108
+ }
109
+ if (value > 255 || value < 0) {
110
+ return false;
111
+ }
112
+ if (t.min !== undefined && value < t.min) {
113
+ return false;
114
+ }
115
+ if (t.max !== undefined && value > t.max) {
116
+ return false;
117
+ }
118
+ return true;
119
+ },
120
+ [UINT16]: (value, t) => {
121
+ if (typeof value !== 'number' || value % t.step !== 0) {
122
+ return false;
123
+ }
124
+ if (value > 65535 || value < 0) {
125
+ return false;
126
+ }
127
+ if (t.min !== undefined && value < t.min) {
128
+ return false;
129
+ }
130
+ if (t.max !== undefined && value > t.max) {
131
+ return false;
132
+ }
133
+ return true;
134
+ },
135
+ [UINT32]: (value, t) => {
136
+ if (typeof value !== 'number' || value % t.step !== 0) {
137
+ return false;
138
+ }
139
+ if (value > 4294967295 || value < 0) {
140
+ return false;
141
+ }
142
+ if (t.min !== undefined && value < t.min) {
143
+ return false;
144
+ }
145
+ if (t.max !== undefined && value > t.max) {
146
+ return false;
147
+ }
148
+ return true;
149
+ },
150
+ [NUMBER]: (value, t) => {
151
+ if (t.step) {
152
+ const div = value / t.step;
153
+ if (Math.abs(div - Math.round(div)) > EPSILON) {
154
+ return false;
155
+ }
156
+ }
157
+ if (typeof value !== 'number') {
158
+ return false;
159
+ }
160
+ if (t.min !== undefined && value < t.min) {
161
+ return false;
162
+ }
163
+ if (t.max !== undefined && value > t.max) {
164
+ return false;
165
+ }
166
+ return true;
167
+ },
168
+ [ENUM]: (value, prop) => {
169
+ if (value === null) {
170
+ return true;
171
+ }
172
+ const arr = prop.enum;
173
+ for (let i = 0; i < arr.length; i++) {
174
+ if (value === arr[i]) {
175
+ return true;
176
+ }
177
+ }
178
+ return false;
179
+ },
180
+ [ID]: (value) => {
181
+ if (typeof value !== 'number' || value % 1 !== 0) {
182
+ return false;
183
+ }
184
+ return true;
185
+ },
186
+ [JSON]: (value) => {
187
+ // mep
188
+ return true;
189
+ },
190
+ [MICRO_BUFFER]: (value) => {
191
+ if (!(value instanceof Uint8Array)) {
192
+ return false;
193
+ }
194
+ return true;
195
+ },
196
+ [REFERENCE]: (v) => {
197
+ if (typeof v !== 'number') {
198
+ return false;
199
+ }
200
+ if (v === 0 || v > MAX_ID) {
201
+ return false;
202
+ }
203
+ return true;
204
+ },
205
+ [REFERENCES]: (v) => {
206
+ if (typeof v !== 'number') {
207
+ return false;
208
+ }
209
+ if (v === 0 || v > MAX_ID) {
210
+ return false;
211
+ }
212
+ return true;
213
+ },
214
+ [STRING]: (value, t) => {
215
+ // add max etc all here - make a ref to the original SCHEMA on DEF
216
+ if (typeof value !== 'string' && !(value instanceof Uint8Array)) {
217
+ return false;
218
+ }
219
+ return true;
220
+ },
221
+ [TEXT]: (value, t) => {
222
+ // add max etc all here - make a ref to the original SCHEMA on DEF
223
+ if (typeof value !== 'string' && !(value instanceof Uint8Array)) {
224
+ return false;
225
+ }
226
+ return true;
227
+ },
228
+ [ALIASES]: (value) => {
229
+ if (!Array.isArray(value)) {
230
+ return false;
231
+ }
232
+ const len = value.length;
233
+ for (let i = 0; i < len; i++) {
234
+ if (typeof value[i] !== 'string') {
235
+ return false;
236
+ }
237
+ }
238
+ return true;
239
+ },
240
+ [VECTOR]: (value) => {
241
+ // Array should be supported
242
+ if (!(value instanceof Float32Array)) {
243
+ return false;
244
+ }
245
+ return true;
246
+ },
247
+ };
248
+ export const defaultValidation = () => true;
249
+ export const isValidId = (id) => {
250
+ if (typeof id != 'number' || id < MIN_ID || id > MAX_ID) {
251
+ return false;
252
+ }
253
+ return true;
254
+ };
255
+ export const isValidString = (v) => {
256
+ const isVal = typeof v === 'string' ||
257
+ v instanceof Uint8Array ||
258
+ ArrayBuffer.isView(v);
259
+ return isVal;
260
+ };
261
+ //# sourceMappingURL=validation.js.map
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  export * from './types.js';
2
2
  export * from './parse/index.js';
3
3
  export * from './lang.js';
4
- export * from './mermaid.js';
4
+ export * from './def/validation.js';
5
+ export * from './serialize.js';
6
+ export * from './infer.js';
5
7
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  export * from './types.js';
2
2
  export * from './parse/index.js';
3
3
  export * from './lang.js';
4
- export * from './mermaid.js';
4
+ export * from './def/validation.js';
5
+ export * from './serialize.js';
6
+ export * from './infer.js';
5
7
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,82 @@
1
+ import { Schema } from './types.js';
2
+ type InferString = string;
3
+ type InferNumber = number;
4
+ type InferBoolean = boolean;
5
+ type InferText = string;
6
+ type InferJson = any;
7
+ type InferTimestamp = number;
8
+ type InferBinary = Uint8Array;
9
+ type InferCardinality = number;
10
+ type InferVector = Float32Array;
11
+ type InferColvec = Float32Array;
12
+ type InferEnum<T extends readonly (string | number | boolean)[]> = T[number];
13
+ type InferReference<RefName extends string, Types> = RefName extends keyof Types ? {
14
+ id: number;
15
+ } & InferSchemaType<Types[RefName], Types> : {
16
+ id: number;
17
+ } | undefined;
18
+ type InferReferences<RefName extends string, Types> = RefName extends keyof Types ? Array<{
19
+ id: number;
20
+ } & InferSchemaType<Types[RefName], Types>> : Array<{
21
+ id: number;
22
+ }>;
23
+ type InferObject<T extends Record<string, any>, Types> = {
24
+ [K in keyof T]: InferProp<T[K], Types>;
25
+ };
26
+ type InferSet<T, Types> = InferProp<T, Types>[];
27
+ type InferProp<T, Types> = T extends {
28
+ type: 'string';
29
+ } ? InferString : T extends {
30
+ type: 'number' | 'int8' | 'uint8' | 'int16' | 'uint16' | 'int32' | 'uint32';
31
+ } ? InferNumber : T extends {
32
+ type: 'boolean';
33
+ } ? InferBoolean : T extends {
34
+ type: 'text';
35
+ } ? InferText : T extends {
36
+ type: 'json';
37
+ } ? InferJson : T extends {
38
+ type: 'timestamp';
39
+ } ? InferTimestamp : T extends {
40
+ type: 'binary';
41
+ } ? InferBinary : T extends {
42
+ type: 'cardinality';
43
+ } ? InferCardinality : T extends {
44
+ type: 'vector';
45
+ } ? InferVector : T extends {
46
+ type: 'colvec';
47
+ } ? InferColvec : T extends {
48
+ enum: infer E;
49
+ } ? E extends readonly (string | number | boolean)[] ? InferEnum<E> : never : T extends {
50
+ ref: infer R extends string;
51
+ } ? InferReference<R, Types> : T extends {
52
+ items: {
53
+ ref: infer R extends string;
54
+ };
55
+ } ? InferReferences<R, Types> : T extends {
56
+ items: infer I;
57
+ } ? InferSet<I, Types> : T extends {
58
+ props: infer P;
59
+ } ? InferObject<P, Types> : T extends string ? T extends 'string' ? InferString : T extends 'number' | 'int8' | 'uint8' | 'int16' | 'uint16' | 'int32' | 'uint32' ? InferNumber : T extends 'boolean' ? InferBoolean : T extends 'text' ? InferText : T extends 'json' ? InferJson : T extends 'timestamp' ? InferTimestamp : T extends 'binary' ? InferBinary : T extends 'cardinality' ? InferCardinality : T extends 'vector' ? InferVector : T extends 'colvec' ? InferColvec : never : never;
60
+ type InferSchemaType<T, Types> = T extends {
61
+ props: infer Props;
62
+ } ? {
63
+ [K in keyof Props]: InferProp<Props[K], Types>;
64
+ } & {
65
+ id: number;
66
+ } : {
67
+ [K in keyof T]: InferProp<T[K], Types>;
68
+ } & {
69
+ id: number;
70
+ };
71
+ type InferSchemaTypes<T> = {
72
+ [K in keyof T]: InferSchemaType<T[K], T>;
73
+ };
74
+ type InferSchema<T extends Schema> = T extends {
75
+ types: infer Types;
76
+ } ? Types extends Record<string, any> ? InferSchemaTypes<Types> : never : never;
77
+ export type Infer<T> = InferSchema<T>;
78
+ export declare const infer: <T extends {
79
+ types: Record<string, any>;
80
+ }>(schema: T) => InferSchema<T>;
81
+ export {};
82
+ //# sourceMappingURL=infer.d.ts.map
package/dist/infer.js ADDED
@@ -0,0 +1,5 @@
1
+ // Main inference function, returns the inferred schema
2
+ export const infer = (schema) => {
3
+ return schema.types;
4
+ };
5
+ //# sourceMappingURL=infer.js.map
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 | 2 | 1 | 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>;
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
@@ -144,10 +144,16 @@ const langCodes = {
144
144
  yi: 142,
145
145
  yo: 143,
146
146
  zu: 144,
147
+ ka: 145,
148
+ cnr: 146,
147
149
  };
148
150
  export const langCodesMap = new Map(Object.entries(langCodes));
149
151
  export const inverseLangMap = new Map();
150
152
  langCodesMap.forEach((v, k) => {
151
153
  inverseLangMap.set(v, k);
152
154
  });
155
+ // generate this
156
+ // __aaabafaksqamarangyasaevaeyazeubebnbisbsbrbgmycakmcezhcvkwcohrxsdadvnldzenetfofifrffgdgldegswelklguhtahahehihuisiiganiaiuikgaitjaknkskkrwkokukylolalvlblilnltmkmgmsmlmtgvmironmnesenoenbnocoroospsfaplptqurmrusmsascrsdsisklslsostnresswssvtlttattethbotitotstntrktkugukuruzveviwacyfywoxhyiyozukacnr
157
+ // for the reader index = code * 2 [0] [1]
158
+ // add a file to the build step
153
159
  //# sourceMappingURL=lang.js.map
@@ -1,8 +1,12 @@
1
+ import { type SemVer } from '@std/semver';
2
+ export declare const expectVersion: (obj: any) => SemVer;
1
3
  export declare const expectObject: (obj: any, msg?: string) => void;
4
+ export declare const expectArray: (obj: any) => void;
2
5
  export declare const expectFloat32Array: (arr: any) => void;
3
6
  export declare const expectString: (obj: any) => void;
4
7
  export declare const expectBoolean: (v: any) => void;
5
8
  export declare const expectFunction: (v: any) => void;
6
9
  export declare const expectNumber: (v: any) => void;
7
10
  export declare const expectPositiveNumber: (v: any) => void;
11
+ export declare const expectTimezoneName: (v: any) => void;
8
12
  //# sourceMappingURL=assert.d.ts.map
@@ -1,12 +1,25 @@
1
- import { isFloat32Array } from 'util/types';
1
+ import { parse } from '@std/semver';
2
2
  import { EXPECTED_ARR, EXPECTED_BOOL, EXPECTED_FN, EXPECTED_NUM, EXPECTED_OBJ, EXPECTED_STR, } from './errors.js';
3
+ export const expectVersion = (obj) => {
4
+ try {
5
+ return parse(obj);
6
+ }
7
+ catch (e) {
8
+ throw Error('invalid semver version');
9
+ }
10
+ };
3
11
  export const expectObject = (obj, msg) => {
4
12
  if (typeof obj !== 'object' || obj === null) {
5
13
  throw Error(msg || EXPECTED_OBJ);
6
14
  }
7
15
  };
16
+ export const expectArray = (obj) => {
17
+ if (!Array.isArray(obj)) {
18
+ throw Error(EXPECTED_ARR);
19
+ }
20
+ };
8
21
  export const expectFloat32Array = (arr) => {
9
- if (!isFloat32Array(arr)) {
22
+ if (!(arr instanceof Float32Array)) {
10
23
  throw Error(EXPECTED_ARR);
11
24
  }
12
25
  };
@@ -36,4 +49,8 @@ export const expectPositiveNumber = (v) => {
36
49
  throw Error('Expected positive number');
37
50
  }
38
51
  };
52
+ export const expectTimezoneName = (v) => {
53
+ expectString(v);
54
+ Intl.DateTimeFormat(undefined, { timeZone: v });
55
+ };
39
56
  //# sourceMappingURL=assert.js.map
@@ -12,6 +12,8 @@ export declare class SchemaParser {
12
12
  parseTypes(): void;
13
13
  parseProps(props: any, schemaType?: SchemaType): void;
14
14
  parseLocales(): void;
15
+ parseDefaultTimezone(): void;
16
+ parseMigrations(): void;
15
17
  parse(): StrictSchema;
16
18
  }
17
19
  export declare const print: (schema: Schema, path: string[]) => string;
@@ -2,11 +2,13 @@ import { INVALID_KEY, INVALID_VALUE, UNKNOWN_PROP } from './errors.js';
2
2
  import { getPropType } from './utils.js';
3
3
  import propParsers from './props.js';
4
4
  import pc from 'picocolors';
5
- import { expectBoolean, expectObject } from './assert.js';
6
- import { deepCopy } from '@saulx/utils';
5
+ import { expectArray, expectBoolean, expectFunction, expectObject, expectTimezoneName, expectVersion, } from './assert.js';
6
+ import { deepCopy } from '@based/utils';
7
+ import { satisfies, parseRange, parse as parseVersion, rangeIntersects, } from '@std/semver';
7
8
  export { getPropType };
8
9
  export class SchemaParser {
9
10
  constructor(schema) {
11
+ // uint8Array is not working
10
12
  this.schema = deepCopy(schema);
11
13
  }
12
14
  isItems;
@@ -64,6 +66,9 @@ export class SchemaParser {
64
66
  expectObject(locales);
65
67
  for (const locale in locales) {
66
68
  const opts = locales[locale];
69
+ if (opts === true) {
70
+ continue;
71
+ }
67
72
  expectObject(opts);
68
73
  for (const key in opts) {
69
74
  const val = opts[key];
@@ -71,7 +76,7 @@ export class SchemaParser {
71
76
  expectBoolean(val);
72
77
  }
73
78
  else if (key === 'fallback') {
74
- if (!Array.isArray(val) || !val.every((v) => typeof v === 'string')) {
79
+ if (Array.isArray(val) || typeof val !== 'string') {
75
80
  throw Error(INVALID_VALUE);
76
81
  }
77
82
  }
@@ -81,6 +86,43 @@ export class SchemaParser {
81
86
  }
82
87
  }
83
88
  }
89
+ parseDefaultTimezone() {
90
+ expectTimezoneName(this.schema.defaultTimezone);
91
+ }
92
+ parseMigrations() {
93
+ const { migrations, version } = this.schema;
94
+ const ranges = new Map();
95
+ expectArray(migrations);
96
+ for (const item of migrations) {
97
+ expectObject(item);
98
+ expectObject(item.migrate);
99
+ const targetRange = parseRange(item.version);
100
+ const currentVersion = parseVersion(version);
101
+ if (satisfies(currentVersion, targetRange)) {
102
+ throw Error('migration version can not match current version');
103
+ }
104
+ if (Object.keys(item).length > 2) {
105
+ throw new Error('migrations can only have "version" and "migrate" properties');
106
+ }
107
+ for (const type in item.migrate) {
108
+ expectFunction(item.migrate[type]);
109
+ if (ranges.has(type)) {
110
+ const otherRanges = ranges.get(type);
111
+ for (const otherRange of otherRanges) {
112
+ if (rangeIntersects(targetRange, otherRange)) {
113
+ throw Error('invalid overlapping version for migration for ' +
114
+ type +
115
+ ` ${JSON.stringify(otherRange)} ${JSON.stringify(targetRange)} ${otherRange === targetRange}`);
116
+ }
117
+ }
118
+ ranges.get(type).push(targetRange);
119
+ }
120
+ else {
121
+ ranges.set(type, [targetRange]);
122
+ }
123
+ }
124
+ }
125
+ }
84
126
  parse() {
85
127
  expectObject(this.schema);
86
128
  // always do types first because it removes props shorthand
@@ -88,12 +130,21 @@ export class SchemaParser {
88
130
  this.parseTypes();
89
131
  }
90
132
  for (const key in this.schema) {
91
- if (key === 'props') {
133
+ if (key === 'version') {
134
+ expectVersion(this.schema.version);
135
+ }
136
+ else if (key === 'props') {
92
137
  this.parseProps(this.schema.props);
93
138
  }
94
139
  else if (key === 'locales') {
95
140
  this.parseLocales();
96
141
  }
142
+ else if (key === 'defaultTimezone') {
143
+ this.parseDefaultTimezone();
144
+ }
145
+ else if (key === 'migrations') {
146
+ this.parseMigrations();
147
+ }
97
148
  else if (key !== 'types') {
98
149
  throw Error(UNKNOWN_PROP);
99
150
  }
@@ -105,6 +156,9 @@ export const print = (schema, path) => {
105
156
  let obj = schema;
106
157
  const depth = path.length - 1;
107
158
  const lines = path.map((key, lvl) => {
159
+ if (typeof obj !== 'object') {
160
+ return '';
161
+ }
108
162
  const v = obj[key];
109
163
  const padding = ' '.repeat(lvl);
110
164
  const prefix = key === Object.keys(obj)[0] ? '' : `${padding}...\n`;
@@ -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