@based/schema 5.0.0-alpha.9 → 5.0.1

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 (106) 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 +61 -4
  34. package/dist/parse/props.d.ts +1 -0
  35. package/dist/parse/props.js +171 -54
  36. package/dist/parse/semver/_constants.d.ts +12 -0
  37. package/dist/parse/semver/_constants.js +21 -0
  38. package/dist/parse/semver/_shared.d.ts +44 -0
  39. package/dist/parse/semver/_shared.js +165 -0
  40. package/dist/parse/semver/_test_comparator_set.d.ts +3 -0
  41. package/dist/parse/semver/_test_comparator_set.js +60 -0
  42. package/dist/parse/semver/can_parse.d.ts +17 -0
  43. package/dist/parse/semver/can_parse.js +28 -0
  44. package/dist/parse/semver/compare.d.ts +28 -0
  45. package/dist/parse/semver/compare.js +36 -0
  46. package/dist/parse/semver/difference.d.ts +27 -0
  47. package/dist/parse/semver/difference.js +41 -0
  48. package/dist/parse/semver/equals.d.ts +24 -0
  49. package/dist/parse/semver/equals.js +28 -0
  50. package/dist/parse/semver/format.d.ts +22 -0
  51. package/dist/parse/semver/format.js +33 -0
  52. package/dist/parse/semver/format_range.d.ts +18 -0
  53. package/dist/parse/semver/format_range.js +29 -0
  54. package/dist/parse/semver/greater_or_equal.d.ts +25 -0
  55. package/dist/parse/semver/greater_or_equal.js +27 -0
  56. package/dist/parse/semver/greater_than.d.ts +25 -0
  57. package/dist/parse/semver/greater_than.js +29 -0
  58. package/dist/parse/semver/greater_than_range.d.ts +23 -0
  59. package/dist/parse/semver/greater_than_range.js +59 -0
  60. package/dist/parse/semver/increment.d.ts +51 -0
  61. package/dist/parse/semver/increment.js +175 -0
  62. package/dist/parse/semver/is_range.d.ts +23 -0
  63. package/dist/parse/semver/is_range.js +39 -0
  64. package/dist/parse/semver/is_semver.d.ts +34 -0
  65. package/dist/parse/semver/is_semver.js +54 -0
  66. package/dist/parse/semver/less_or_equal.d.ts +25 -0
  67. package/dist/parse/semver/less_or_equal.js +27 -0
  68. package/dist/parse/semver/less_than.d.ts +25 -0
  69. package/dist/parse/semver/less_than.js +27 -0
  70. package/dist/parse/semver/less_than_range.d.ts +23 -0
  71. package/dist/parse/semver/less_than_range.js +59 -0
  72. package/dist/parse/semver/max_satisfying.d.ts +22 -0
  73. package/dist/parse/semver/max_satisfying.js +31 -0
  74. package/dist/parse/semver/min_satisfying.d.ts +22 -0
  75. package/dist/parse/semver/min_satisfying.js +31 -0
  76. package/dist/parse/semver/mod.d.ts +298 -0
  77. package/dist/parse/semver/mod.js +301 -0
  78. package/dist/parse/semver/not_equals.d.ts +24 -0
  79. package/dist/parse/semver/not_equals.js +26 -0
  80. package/dist/parse/semver/parse.d.ts +25 -0
  81. package/dist/parse/semver/parse.js +43 -0
  82. package/dist/parse/semver/parse_range.d.ts +27 -0
  83. package/dist/parse/semver/parse_range.js +361 -0
  84. package/dist/parse/semver/range_intersects.d.ts +25 -0
  85. package/dist/parse/semver/range_intersects.js +83 -0
  86. package/dist/parse/semver/satisfies.d.ts +24 -0
  87. package/dist/parse/semver/satisfies.js +28 -0
  88. package/dist/parse/semver/try_parse.d.ts +21 -0
  89. package/dist/parse/semver/try_parse.js +28 -0
  90. package/dist/parse/semver/try_parse_range.d.ts +23 -0
  91. package/dist/parse/semver/try_parse_range.js +34 -0
  92. package/dist/parse/semver/types.d.ts +58 -0
  93. package/dist/parse/semver/types.js +4 -0
  94. package/dist/serialize.d.ts +14 -0
  95. package/dist/serialize.js +543 -0
  96. package/dist/types.d.ts +74 -20
  97. package/dist/types.js +3 -1
  98. package/package.json +5 -4
  99. package/dist/def/getPropLen.d.ts +0 -3
  100. package/dist/def/getPropLen.js +0 -23
  101. package/dist/def/makePacked.d.ts +0 -3
  102. package/dist/def/makePacked.js +0 -50
  103. package/dist/def/readFromPacked.d.ts +0 -3
  104. package/dist/def/readFromPacked.js +0 -140
  105. package/dist/mermaid.d.ts +0 -3
  106. 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
@@ -2,4 +2,6 @@ import { TypeIndex, PropDef, PropDefEdge } from './types.js';
2
2
  export type Validation = (payload: any, prop: PropDef | PropDefEdge) => boolean;
3
3
  export declare const VALIDATION_MAP: Record<TypeIndex, Validation>;
4
4
  export declare const defaultValidation: () => boolean;
5
+ export declare const isValidId: (id: number) => boolean;
6
+ export declare const isValidString: (v: any) => boolean;
5
7
  //# sourceMappingURL=validation.d.ts.map
@@ -1,44 +1,63 @@
1
- import { TYPE_INDEX_MAP } from './types.js';
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
2
5
  export const VALIDATION_MAP = {
3
- [TYPE_INDEX_MAP.alias]: (value) => {
6
+ [NULL]: () => true,
7
+ [OBJECT]: () => true,
8
+ [COLVEC]: () => true,
9
+ [WEAK_REFERENCE]: () => true,
10
+ [WEAK_REFERENCES]: () => true,
11
+ [ALIAS]: (value) => {
4
12
  if (typeof value !== 'string') {
5
13
  return false;
6
14
  }
7
15
  return true;
8
16
  },
9
- [TYPE_INDEX_MAP.binary]: (value) => {
17
+ [BINARY]: (value) => {
10
18
  if (value instanceof Uint8Array) {
11
19
  return true;
12
20
  }
13
21
  return false;
14
22
  },
15
- [TYPE_INDEX_MAP.boolean]: (value) => {
23
+ [BOOLEAN]: (value) => {
16
24
  if (typeof value !== 'boolean') {
17
25
  return false;
18
26
  }
19
27
  return true;
20
28
  },
21
- [TYPE_INDEX_MAP.cardinality]: (val) => {
29
+ [CARDINALITY]: (val) => {
22
30
  return (typeof val === 'string' ||
23
31
  (val instanceof Uint8Array && val.byteLength === 8));
24
32
  },
25
- [TYPE_INDEX_MAP.timestamp]: (value, t) => {
26
- if (typeof value === 'string') {
27
- return true;
28
- }
29
- if (typeof value !== 'number' || value % 1 !== 0) {
33
+ [TIMESTAMP]: (value, t) => {
34
+ if (typeof value !== 'number' || value % t.step !== 0) {
30
35
  return false;
31
36
  }
32
- if (t.min !== undefined && value < t.min) {
33
- return false;
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
+ }
34
46
  }
35
- if (t.max !== undefined && value > t.max) {
36
- return false;
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
+ }
37
56
  }
38
57
  return true;
39
58
  },
40
- [TYPE_INDEX_MAP.int16]: (value, t) => {
41
- if (typeof value !== 'number' || value % 1 !== 0) {
59
+ [INT16]: (value, t) => {
60
+ if (typeof value !== 'number' || value % t.step !== 0) {
42
61
  return false;
43
62
  }
44
63
  if (value > 32767 || value < -32768) {
@@ -52,8 +71,8 @@ export const VALIDATION_MAP = {
52
71
  }
53
72
  return true;
54
73
  },
55
- [TYPE_INDEX_MAP.int32]: (value, t) => {
56
- if (typeof value !== 'number' || value % 1 !== 0) {
74
+ [INT32]: (value, t) => {
75
+ if (typeof value !== 'number' || value % t.step !== 0) {
57
76
  return false;
58
77
  }
59
78
  if (value > 2147483647 || value < -2147483648) {
@@ -67,9 +86,9 @@ export const VALIDATION_MAP = {
67
86
  }
68
87
  return true;
69
88
  },
70
- [TYPE_INDEX_MAP.int8]: (value, t) => {
89
+ [INT8]: (value, t) => {
71
90
  // use % for steps size
72
- if (typeof value !== 'number' || value % 1 !== 0) {
91
+ if (typeof value !== 'number' || value % t.step !== 0) {
73
92
  return false;
74
93
  }
75
94
  if (value > 127 || value < -128) {
@@ -83,8 +102,8 @@ export const VALIDATION_MAP = {
83
102
  }
84
103
  return true;
85
104
  },
86
- [TYPE_INDEX_MAP.uint8]: (value, t) => {
87
- if (typeof value !== 'number' || value % 1 !== 0) {
105
+ [UINT8]: (value, t) => {
106
+ if (typeof value !== 'number' || value % t.step !== 0) {
88
107
  return false;
89
108
  }
90
109
  if (value > 255 || value < 0) {
@@ -98,8 +117,8 @@ export const VALIDATION_MAP = {
98
117
  }
99
118
  return true;
100
119
  },
101
- [TYPE_INDEX_MAP.uint16]: (value, t) => {
102
- if (typeof value !== 'number' || value % 1 !== 0) {
120
+ [UINT16]: (value, t) => {
121
+ if (typeof value !== 'number' || value % t.step !== 0) {
103
122
  return false;
104
123
  }
105
124
  if (value > 65535 || value < 0) {
@@ -113,8 +132,8 @@ export const VALIDATION_MAP = {
113
132
  }
114
133
  return true;
115
134
  },
116
- [TYPE_INDEX_MAP.uint32]: (value, t) => {
117
- if (typeof value !== 'number' || value % 1 !== 0) {
135
+ [UINT32]: (value, t) => {
136
+ if (typeof value !== 'number' || value % t.step !== 0) {
118
137
  return false;
119
138
  }
120
139
  if (value > 4294967295 || value < 0) {
@@ -128,7 +147,13 @@ export const VALIDATION_MAP = {
128
147
  }
129
148
  return true;
130
149
  },
131
- [TYPE_INDEX_MAP.number]: (value, t) => {
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
+ }
132
157
  if (typeof value !== 'number') {
133
158
  return false;
134
159
  }
@@ -140,7 +165,7 @@ export const VALIDATION_MAP = {
140
165
  }
141
166
  return true;
142
167
  },
143
- [TYPE_INDEX_MAP.enum]: (value, prop) => {
168
+ [ENUM]: (value, prop) => {
144
169
  if (value === null) {
145
170
  return true;
146
171
  }
@@ -152,44 +177,55 @@ export const VALIDATION_MAP = {
152
177
  }
153
178
  return false;
154
179
  },
155
- [TYPE_INDEX_MAP.id]: (value) => {
180
+ [ID]: (value) => {
156
181
  if (typeof value !== 'number' || value % 1 !== 0) {
157
182
  return false;
158
183
  }
159
184
  return true;
160
185
  },
161
- [TYPE_INDEX_MAP.json]: (value) => {
186
+ [JSON]: (value) => {
187
+ // mep
162
188
  return true;
163
189
  },
164
- [TYPE_INDEX_MAP.microbuffer]: (value) => {
190
+ [MICRO_BUFFER]: (value) => {
165
191
  if (!(value instanceof Uint8Array)) {
166
192
  return false;
167
193
  }
168
194
  return true;
169
195
  },
170
- [TYPE_INDEX_MAP.reference]: (value) => {
171
- // if (typeof value !== 'number' && value != null) {
172
- // return false
173
- // }
196
+ [REFERENCE]: (v) => {
197
+ if (typeof v !== 'number') {
198
+ return false;
199
+ }
200
+ if (v === 0 || v > MAX_ID) {
201
+ return false;
202
+ }
174
203
  return true;
175
204
  },
176
- [TYPE_INDEX_MAP.references]: (v) => {
205
+ [REFERENCES]: (v) => {
177
206
  if (typeof v !== 'number') {
178
207
  return false;
179
208
  }
180
- if (v === 0) {
209
+ if (v === 0 || v > MAX_ID) {
181
210
  return false;
182
211
  }
183
212
  return true;
184
213
  },
185
- [TYPE_INDEX_MAP.string]: (value, t) => {
186
- // add max etc all here - make a ref to the original SCHEMA
214
+ [STRING]: (value, t) => {
215
+ // add max etc all here - make a ref to the original SCHEMA on DEF
187
216
  if (typeof value !== 'string' && !(value instanceof Uint8Array)) {
188
217
  return false;
189
218
  }
190
219
  return true;
191
220
  },
192
- [TYPE_INDEX_MAP.aliases]: (value) => {
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) => {
193
229
  if (!Array.isArray(value)) {
194
230
  return false;
195
231
  }
@@ -201,7 +237,7 @@ export const VALIDATION_MAP = {
201
237
  }
202
238
  return true;
203
239
  },
204
- [TYPE_INDEX_MAP.vector]: (value) => {
240
+ [VECTOR]: (value) => {
205
241
  // Array should be supported
206
242
  if (!(value instanceof Float32Array)) {
207
243
  return false;
@@ -210,4 +246,16 @@ export const VALIDATION_MAP = {
210
246
  },
211
247
  };
212
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
+ };
213
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 | 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>;
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 { SemVer } from './semver/types.js';
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';
2
1
  import { EXPECTED_ARR, EXPECTED_BOOL, EXPECTED_FN, EXPECTED_NUM, EXPECTED_OBJ, EXPECTED_STR, } from './errors.js';
2
+ import { parse } from './semver/parse.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,16 @@ 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 { parse as parseVersion } from './semver/parse.js';
8
+ import { parseRange } from './semver/parse_range.js';
9
+ import { satisfies } from './semver/satisfies.js';
10
+ import { rangeIntersects } from './semver/range_intersects.js';
7
11
  export { getPropType };
8
12
  export class SchemaParser {
9
13
  constructor(schema) {
14
+ // uint8Array is not working
10
15
  this.schema = deepCopy(schema);
11
16
  }
12
17
  isItems;
@@ -64,6 +69,9 @@ export class SchemaParser {
64
69
  expectObject(locales);
65
70
  for (const locale in locales) {
66
71
  const opts = locales[locale];
72
+ if (opts === true) {
73
+ continue;
74
+ }
67
75
  expectObject(opts);
68
76
  for (const key in opts) {
69
77
  const val = opts[key];
@@ -71,7 +79,7 @@ export class SchemaParser {
71
79
  expectBoolean(val);
72
80
  }
73
81
  else if (key === 'fallback') {
74
- if (!Array.isArray(val) || !val.every((v) => typeof v === 'string')) {
82
+ if (Array.isArray(val) || typeof val !== 'string') {
75
83
  throw Error(INVALID_VALUE);
76
84
  }
77
85
  }
@@ -81,6 +89,43 @@ export class SchemaParser {
81
89
  }
82
90
  }
83
91
  }
92
+ parseDefaultTimezone() {
93
+ expectTimezoneName(this.schema.defaultTimezone);
94
+ }
95
+ parseMigrations() {
96
+ const { migrations, version } = this.schema;
97
+ const ranges = new Map();
98
+ expectArray(migrations);
99
+ for (const item of migrations) {
100
+ expectObject(item);
101
+ expectObject(item.migrate);
102
+ const targetRange = parseRange(item.version);
103
+ const currentVersion = parseVersion(version);
104
+ if (satisfies(currentVersion, targetRange)) {
105
+ throw Error('migration version can not match current version');
106
+ }
107
+ if (Object.keys(item).length > 2) {
108
+ throw new Error('migrations can only have "version" and "migrate" properties');
109
+ }
110
+ for (const type in item.migrate) {
111
+ expectFunction(item.migrate[type]);
112
+ if (ranges.has(type)) {
113
+ const otherRanges = ranges.get(type);
114
+ for (const otherRange of otherRanges) {
115
+ if (rangeIntersects(targetRange, otherRange)) {
116
+ throw Error('invalid overlapping version for migration for ' +
117
+ type +
118
+ ` ${JSON.stringify(otherRange)} ${JSON.stringify(targetRange)} ${otherRange === targetRange}`);
119
+ }
120
+ }
121
+ ranges.get(type).push(targetRange);
122
+ }
123
+ else {
124
+ ranges.set(type, [targetRange]);
125
+ }
126
+ }
127
+ }
128
+ }
84
129
  parse() {
85
130
  expectObject(this.schema);
86
131
  // always do types first because it removes props shorthand
@@ -88,12 +133,21 @@ export class SchemaParser {
88
133
  this.parseTypes();
89
134
  }
90
135
  for (const key in this.schema) {
91
- if (key === 'props') {
136
+ if (key === 'version') {
137
+ expectVersion(this.schema.version);
138
+ }
139
+ else if (key === 'props') {
92
140
  this.parseProps(this.schema.props);
93
141
  }
94
142
  else if (key === 'locales') {
95
143
  this.parseLocales();
96
144
  }
145
+ else if (key === 'defaultTimezone') {
146
+ this.parseDefaultTimezone();
147
+ }
148
+ else if (key === 'migrations') {
149
+ this.parseMigrations();
150
+ }
97
151
  else if (key !== 'types') {
98
152
  throw Error(UNKNOWN_PROP);
99
153
  }
@@ -105,6 +159,9 @@ export const print = (schema, path) => {
105
159
  let obj = schema;
106
160
  const depth = path.length - 1;
107
161
  const lines = path.map((key, lvl) => {
162
+ if (typeof obj !== 'object') {
163
+ return '';
164
+ }
108
165
  const v = obj[key];
109
166
  const padding = ' '.repeat(lvl);
110
167
  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