@based/schema 5.0.0-alpha.7 → 5.0.0-alpha.9
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.js +5 -1
- package/dist/def/createEmptyDef.d.ts +1 -0
- package/dist/def/createEmptyDef.js +2 -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 +53 -0
- package/dist/def/index.d.ts +2 -0
- package/dist/def/index.js +2 -0
- package/dist/def/readFromPacked.js +8 -1
- package/dist/def/typeDef.js +17 -1
- package/dist/def/types.d.ts +9 -5
- package/dist/def/types.js +5 -9
- package/dist/def/validation.d.ts +5 -0
- package/dist/def/validation.js +213 -0
- package/dist/parse/index.d.ts +1 -1
- package/dist/parse/index.js +4 -4
- package/dist/parse/props.js +1 -0
- package/dist/types.d.ts +3 -0
- package/package.json +3 -2
package/dist/def/addEdges.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { getPropType } from '../index.js';
|
|
2
2
|
import { TYPE_INDEX_MAP, REFERENCES, REFERENCE, ENUM, } from './types.js';
|
|
3
3
|
import { getPropLen, isSeparate } from './utils.js';
|
|
4
|
+
import { defaultValidation, VALIDATION_MAP } from './validation.js';
|
|
4
5
|
export const addEdges = (prop, refProp) => {
|
|
5
6
|
for (const key in refProp) {
|
|
6
7
|
if (key[0] === '$') {
|
|
@@ -18,12 +19,15 @@ export const addEdges = (prop, refProp) => {
|
|
|
18
19
|
if (separate) {
|
|
19
20
|
prop.edgesSeperateCnt++;
|
|
20
21
|
}
|
|
22
|
+
const typeIndex = TYPE_INDEX_MAP[edgeType];
|
|
23
|
+
// add default
|
|
21
24
|
const edge = {
|
|
22
25
|
__isPropDef: true,
|
|
23
26
|
__isEdge: true,
|
|
24
27
|
prop: separate ? prop.edgesSeperateCnt : 0,
|
|
28
|
+
validation: edgeProp.validate ?? VALIDATION_MAP[typeIndex] ?? defaultValidation,
|
|
25
29
|
name: key,
|
|
26
|
-
typeIndex
|
|
30
|
+
typeIndex,
|
|
27
31
|
len,
|
|
28
32
|
separate,
|
|
29
33
|
path: [...prop.path, key],
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { TYPE_INDEX_MAP } from './types.js';
|
|
2
|
+
// TODO update defaults
|
|
3
|
+
export const DEFAULT_MAP = {
|
|
4
|
+
[TYPE_INDEX_MAP.alias]: '',
|
|
5
|
+
[TYPE_INDEX_MAP.binary]: undefined,
|
|
6
|
+
[TYPE_INDEX_MAP.boolean]: false,
|
|
7
|
+
[TYPE_INDEX_MAP.cardinality]: 0,
|
|
8
|
+
[TYPE_INDEX_MAP.number]: 0,
|
|
9
|
+
[TYPE_INDEX_MAP.timestamp]: 0,
|
|
10
|
+
[TYPE_INDEX_MAP.enum]: 0,
|
|
11
|
+
[TYPE_INDEX_MAP.id]: 0,
|
|
12
|
+
[TYPE_INDEX_MAP.int16]: 0,
|
|
13
|
+
[TYPE_INDEX_MAP.int32]: 0,
|
|
14
|
+
[TYPE_INDEX_MAP.int8]: 0,
|
|
15
|
+
[TYPE_INDEX_MAP.uint8]: 0,
|
|
16
|
+
[TYPE_INDEX_MAP.uint16]: 0,
|
|
17
|
+
[TYPE_INDEX_MAP.uint32]: 0,
|
|
18
|
+
[TYPE_INDEX_MAP.json]: undefined,
|
|
19
|
+
[TYPE_INDEX_MAP.microbuffer]: undefined,
|
|
20
|
+
[TYPE_INDEX_MAP.reference]: undefined,
|
|
21
|
+
[TYPE_INDEX_MAP.references]: [],
|
|
22
|
+
[TYPE_INDEX_MAP.string]: '',
|
|
23
|
+
[TYPE_INDEX_MAP.aliases]: [],
|
|
24
|
+
[TYPE_INDEX_MAP.text]: '',
|
|
25
|
+
[TYPE_INDEX_MAP.vector]: undefined, // maybe not can set a vec with 0
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=defaultMap.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { PropDef } from './types.js';
|
|
2
|
+
export declare const ENCODER: TextEncoder;
|
|
3
|
+
export declare const fillEmptyMain: (vals: PropDef[], mainLen: number) => Uint8Array;
|
|
4
|
+
export declare const isZeroes: (buf: Uint8Array) => boolean;
|
|
5
|
+
//# sourceMappingURL=fillEmptyMain.d.ts.map
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { BINARY, BOOLEAN, ENUM, INT16, INT32, INT8, NUMBER, STRING, TIMESTAMP, UINT16, UINT32, UINT8, } from './types.js';
|
|
2
|
+
// Lets add validation of values in here - need to validate DEFAULT!
|
|
3
|
+
export const ENCODER = new TextEncoder();
|
|
4
|
+
export const fillEmptyMain = (vals, mainLen) => {
|
|
5
|
+
const mainEmpty = new Uint8Array(mainLen);
|
|
6
|
+
for (const f of vals) {
|
|
7
|
+
if (f.separate) {
|
|
8
|
+
continue;
|
|
9
|
+
}
|
|
10
|
+
const t = f.typeIndex;
|
|
11
|
+
const s = f.start;
|
|
12
|
+
let val = f.default;
|
|
13
|
+
if (t === BOOLEAN || t === INT8 || t === UINT8 || t === ENUM) {
|
|
14
|
+
mainEmpty[s] = val === true ? 1 : 0;
|
|
15
|
+
}
|
|
16
|
+
else if (t === UINT32 || t === INT32) {
|
|
17
|
+
mainEmpty[s] = val;
|
|
18
|
+
mainEmpty[s + 1] = val >>>= 8;
|
|
19
|
+
mainEmpty[s + 2] = val >>>= 8;
|
|
20
|
+
mainEmpty[s + 3] = val >>>= 8;
|
|
21
|
+
}
|
|
22
|
+
else if (t === UINT16 || t === INT16) {
|
|
23
|
+
mainEmpty[s] = val;
|
|
24
|
+
mainEmpty[s + 1] = val >>>= 8;
|
|
25
|
+
}
|
|
26
|
+
else if (t === NUMBER || t === TIMESTAMP) {
|
|
27
|
+
const view = new DataView(mainEmpty.buffer, s, 8);
|
|
28
|
+
view.setFloat64(0, val, true);
|
|
29
|
+
}
|
|
30
|
+
else if (t === STRING) {
|
|
31
|
+
val = ENCODER.encode(val);
|
|
32
|
+
mainEmpty[s] = val.byteLength;
|
|
33
|
+
mainEmpty.set(val, s + 1);
|
|
34
|
+
}
|
|
35
|
+
else if (t === BINARY) {
|
|
36
|
+
if (val !== undefined) {
|
|
37
|
+
mainEmpty.set(val, s);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return mainEmpty;
|
|
42
|
+
};
|
|
43
|
+
export const isZeroes = (buf) => {
|
|
44
|
+
let i = 0;
|
|
45
|
+
while (i < buf.byteLength) {
|
|
46
|
+
if (buf[i] !== 0) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
i++;
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
};
|
|
53
|
+
//# sourceMappingURL=fillEmptyMain.js.map
|
package/dist/def/index.d.ts
CHANGED
package/dist/def/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { REVERSE_SIZE_MAP } from './types.js';
|
|
2
|
+
import { DEFAULT_MAP } from './defaultMap.js';
|
|
3
|
+
import { VALIDATION_MAP } from './validation.js';
|
|
2
4
|
export const readFromPacked = (packed) => {
|
|
3
5
|
const size = (packed[0] | (packed[1] << 8)) >>> 0;
|
|
4
6
|
const props = [];
|
|
@@ -49,7 +51,6 @@ export const readFromPacked = (packed) => {
|
|
|
49
51
|
// Text not supported yet
|
|
50
52
|
// Ref not supported yet
|
|
51
53
|
// compression: 1 (0)
|
|
52
|
-
// YET
|
|
53
54
|
const result = {
|
|
54
55
|
cnt: 0,
|
|
55
56
|
checksum: 0,
|
|
@@ -86,6 +87,8 @@ export const readFromPacked = (packed) => {
|
|
|
86
87
|
bufferTmp: new Uint8Array([]),
|
|
87
88
|
props: [],
|
|
88
89
|
},
|
|
90
|
+
mainEmpty: new Uint8Array([]),
|
|
91
|
+
mainEmptyAllZeroes: true,
|
|
89
92
|
// need this...
|
|
90
93
|
locales: {},
|
|
91
94
|
localeSize: 0,
|
|
@@ -97,7 +100,9 @@ export const readFromPacked = (packed) => {
|
|
|
97
100
|
prop: p.prop,
|
|
98
101
|
separate: false,
|
|
99
102
|
__isPropDef: true,
|
|
103
|
+
validation: VALIDATION_MAP[p.typeIndex],
|
|
100
104
|
start: s,
|
|
105
|
+
default: DEFAULT_MAP[p.typeIndex], // tmp
|
|
101
106
|
typeIndex: p.typeIndex,
|
|
102
107
|
path: p.path.split('.'),
|
|
103
108
|
len,
|
|
@@ -111,8 +116,10 @@ export const readFromPacked = (packed) => {
|
|
|
111
116
|
prop: p.prop,
|
|
112
117
|
separate: true,
|
|
113
118
|
__isPropDef: true,
|
|
119
|
+
validation: VALIDATION_MAP[p.typeIndex],
|
|
114
120
|
start: 0,
|
|
115
121
|
typeIndex: p.typeIndex,
|
|
122
|
+
default: DEFAULT_MAP[p.typeIndex], // tmp
|
|
116
123
|
path: p.path.split('.'),
|
|
117
124
|
len: 0,
|
|
118
125
|
compression: 1,
|
package/dist/def/typeDef.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { isPropType, getPropType, } from '../index.js';
|
|
2
2
|
import { setByPath } from '@saulx/utils';
|
|
3
3
|
import { TYPE_INDEX_MAP, REFERENCES, REFERENCE, } from './types.js';
|
|
4
|
+
import { DEFAULT_MAP } from './defaultMap.js';
|
|
4
5
|
import { makePacked } from './makePacked.js';
|
|
5
6
|
import { makeSeparateTextSort } from './makeSeparateTextSort.js';
|
|
6
7
|
import { makeSeparateSort } from './makeSeparateSort.js';
|
|
@@ -9,6 +10,8 @@ import { isSeparate } from './utils.js';
|
|
|
9
10
|
import { addEdges } from './addEdges.js';
|
|
10
11
|
import { createEmptyDef } from './createEmptyDef.js';
|
|
11
12
|
import { hashObjectIgnoreKeyOrder } from '@saulx/hash';
|
|
13
|
+
import { fillEmptyMain, isZeroes } from './fillEmptyMain.js';
|
|
14
|
+
import { defaultValidation, VALIDATION_MAP } from './validation.js';
|
|
12
15
|
export const DEFAULT_BLOCK_CAPACITY = 100_000;
|
|
13
16
|
export const updateTypeDefs = (schema, schemaTypesParsed, schemaTypesParsedById) => {
|
|
14
17
|
for (const field in schemaTypesParsed) {
|
|
@@ -57,6 +60,7 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = cr
|
|
|
57
60
|
let separateSortProps = 0;
|
|
58
61
|
let separateSortText = 0;
|
|
59
62
|
for (const key in target) {
|
|
63
|
+
// Create prop def
|
|
60
64
|
const schemaProp = target[key];
|
|
61
65
|
const propPath = [...path, key];
|
|
62
66
|
const propType = getPropType(schemaProp);
|
|
@@ -82,15 +86,24 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = cr
|
|
|
82
86
|
separateSortText++;
|
|
83
87
|
}
|
|
84
88
|
const isseparate = isSeparate(schemaProp, len);
|
|
89
|
+
const typeIndex = TYPE_INDEX_MAP[propType];
|
|
85
90
|
const prop = {
|
|
86
|
-
typeIndex
|
|
91
|
+
typeIndex,
|
|
87
92
|
__isPropDef: true,
|
|
88
93
|
separate: isseparate,
|
|
89
94
|
path: propPath,
|
|
90
95
|
start: 0,
|
|
96
|
+
validation: schemaProp.validate ?? VALIDATION_MAP[typeIndex] ?? defaultValidation,
|
|
91
97
|
len,
|
|
98
|
+
default: schemaProp.default ?? DEFAULT_MAP[typeIndex],
|
|
92
99
|
prop: isseparate ? ++result.cnt : 0,
|
|
93
100
|
};
|
|
101
|
+
if (schemaProp.max) {
|
|
102
|
+
prop.max = schemaProp.max;
|
|
103
|
+
}
|
|
104
|
+
if (schemaProp.min) {
|
|
105
|
+
prop.min = schemaProp.min;
|
|
106
|
+
}
|
|
94
107
|
if (isPropType('enum', schemaProp)) {
|
|
95
108
|
prop.enum = Array.isArray(schemaProp) ? schemaProp : schemaProp.enum;
|
|
96
109
|
prop.reverseEnum = {};
|
|
@@ -138,6 +151,7 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = cr
|
|
|
138
151
|
}
|
|
139
152
|
}
|
|
140
153
|
if (top) {
|
|
154
|
+
// Put top level together
|
|
141
155
|
const vals = Object.values(result.props);
|
|
142
156
|
vals.sort((a, b) => {
|
|
143
157
|
if (b.separate &&
|
|
@@ -168,6 +182,8 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = cr
|
|
|
168
182
|
setByPath(result.tree, f.path, f);
|
|
169
183
|
}
|
|
170
184
|
}
|
|
185
|
+
result.mainEmpty = fillEmptyMain(vals, result.mainLen);
|
|
186
|
+
result.mainEmptyAllZeroes = isZeroes(result.mainEmpty);
|
|
171
187
|
makePacked(result, typeName, vals, len);
|
|
172
188
|
if (separateSortText > 0) {
|
|
173
189
|
makeSeparateTextSort(result);
|
package/dist/def/types.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import type { LangCode, SchemaLocales } from '../index.js';
|
|
2
|
+
import { Validation } from './validation.js';
|
|
2
3
|
export declare const NULL = 0;
|
|
3
4
|
export declare const TIMESTAMP = 1;
|
|
4
|
-
export declare const CREATED = 2;
|
|
5
|
-
export declare const UPDATED = 3;
|
|
6
5
|
export declare const NUMBER = 4;
|
|
7
6
|
export declare const CARDINALITY = 5;
|
|
8
7
|
export declare const INT8 = 20;
|
|
@@ -34,8 +33,6 @@ export declare const TYPE_INDEX_MAP: {
|
|
|
34
33
|
reference: number;
|
|
35
34
|
timestamp: number;
|
|
36
35
|
boolean: number;
|
|
37
|
-
created: number;
|
|
38
|
-
updated: number;
|
|
39
36
|
number: number;
|
|
40
37
|
string: number;
|
|
41
38
|
text: number;
|
|
@@ -69,6 +66,8 @@ export type PropDef = {
|
|
|
69
66
|
inversePropNumber?: number;
|
|
70
67
|
enum?: any[];
|
|
71
68
|
dependent?: boolean;
|
|
69
|
+
validation: Validation;
|
|
70
|
+
default: any;
|
|
72
71
|
edgeMainLen?: 0;
|
|
73
72
|
reverseEnum?: {
|
|
74
73
|
[key: string]: number;
|
|
@@ -83,7 +82,10 @@ export type PropDef = {
|
|
|
83
82
|
reverseMainEdges?: {
|
|
84
83
|
[start: string]: PropDefEdge;
|
|
85
84
|
};
|
|
85
|
+
edgeMainEmpty?: Uint8Array;
|
|
86
86
|
__isEdge?: boolean;
|
|
87
|
+
max?: number;
|
|
88
|
+
min?: number;
|
|
87
89
|
};
|
|
88
90
|
export type PropDefEdge = Partial<PropDef> & {
|
|
89
91
|
__isPropDef: true;
|
|
@@ -133,6 +135,8 @@ export type SchemaTypeDef = {
|
|
|
133
135
|
main: {
|
|
134
136
|
[start: string]: PropDef;
|
|
135
137
|
};
|
|
138
|
+
mainEmpty: Uint8Array;
|
|
139
|
+
mainEmptyAllZeroes: boolean;
|
|
136
140
|
tree: SchemaPropTree;
|
|
137
141
|
hasSeperateSort: boolean;
|
|
138
142
|
seperateSort: SchemaSortUndefinedHandler;
|
|
@@ -148,7 +152,7 @@ export type SchemaTypeDef = {
|
|
|
148
152
|
localeSize: number;
|
|
149
153
|
};
|
|
150
154
|
export declare const SIZE_MAP: Record<InternalSchemaProp, number>;
|
|
151
|
-
export declare
|
|
155
|
+
export declare const REVERSE_SIZE_MAP: Record<TypeIndex, number>;
|
|
152
156
|
export declare const REVERSE_TYPE_INDEX_MAP: Record<TypeIndex, InternalSchemaProp>;
|
|
153
157
|
export declare const ID_FIELD_DEF: PropDef;
|
|
154
158
|
export declare const EMPTY_MICRO_BUFFER: PropDef;
|
package/dist/def/types.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
// WARN: The following type codes are used in js and zig but selva has its own typing.
|
|
2
2
|
export const NULL = 0;
|
|
3
3
|
export const TIMESTAMP = 1;
|
|
4
|
-
export const CREATED = 2;
|
|
5
|
-
export const UPDATED = 3;
|
|
6
4
|
export const NUMBER = 4;
|
|
7
5
|
export const CARDINALITY = 5;
|
|
8
6
|
export const INT8 = 20;
|
|
@@ -34,8 +32,6 @@ export const TYPE_INDEX_MAP = {
|
|
|
34
32
|
reference: REFERENCE,
|
|
35
33
|
timestamp: TIMESTAMP,
|
|
36
34
|
boolean: BOOLEAN,
|
|
37
|
-
created: CREATED,
|
|
38
|
-
updated: UPDATED,
|
|
39
35
|
number: NUMBER,
|
|
40
36
|
string: STRING,
|
|
41
37
|
text: TEXT,
|
|
@@ -54,8 +50,6 @@ export const TYPE_INDEX_MAP = {
|
|
|
54
50
|
};
|
|
55
51
|
export const SIZE_MAP = {
|
|
56
52
|
timestamp: 8, // 64bit
|
|
57
|
-
created: 8,
|
|
58
|
-
updated: 8,
|
|
59
53
|
// double-precision 64-bit binary format IEEE 754 value
|
|
60
54
|
number: 8, // 64bit
|
|
61
55
|
int8: 1,
|
|
@@ -83,9 +77,7 @@ const reverseMap = {};
|
|
|
83
77
|
for (const k in TYPE_INDEX_MAP) {
|
|
84
78
|
reverseMap[TYPE_INDEX_MAP[k]] = k;
|
|
85
79
|
}
|
|
86
|
-
export
|
|
87
|
-
// @ts-ignore
|
|
88
|
-
REVERSE_SIZE_MAP = {};
|
|
80
|
+
export const REVERSE_SIZE_MAP = {};
|
|
89
81
|
for (const k in SIZE_MAP) {
|
|
90
82
|
REVERSE_SIZE_MAP[TYPE_INDEX_MAP[k]] = SIZE_MAP[k];
|
|
91
83
|
}
|
|
@@ -96,7 +88,9 @@ export const ID_FIELD_DEF = {
|
|
|
96
88
|
path: ['id'],
|
|
97
89
|
start: 0,
|
|
98
90
|
prop: 255,
|
|
91
|
+
default: 0,
|
|
99
92
|
len: 4,
|
|
93
|
+
validation: () => true,
|
|
100
94
|
__isPropDef: true,
|
|
101
95
|
};
|
|
102
96
|
export const EMPTY_MICRO_BUFFER = {
|
|
@@ -104,8 +98,10 @@ export const EMPTY_MICRO_BUFFER = {
|
|
|
104
98
|
separate: true,
|
|
105
99
|
path: [''],
|
|
106
100
|
start: 0,
|
|
101
|
+
default: undefined,
|
|
107
102
|
prop: 0,
|
|
108
103
|
len: 1,
|
|
104
|
+
validation: () => true,
|
|
109
105
|
__isPropDef: true,
|
|
110
106
|
};
|
|
111
107
|
export const getPropTypeName = (propType) => {
|
|
@@ -0,0 +1,5 @@
|
|
|
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
|
+
//# sourceMappingURL=validation.d.ts.map
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { TYPE_INDEX_MAP } from './types.js';
|
|
2
|
+
export const VALIDATION_MAP = {
|
|
3
|
+
[TYPE_INDEX_MAP.alias]: (value) => {
|
|
4
|
+
if (typeof value !== 'string') {
|
|
5
|
+
return false;
|
|
6
|
+
}
|
|
7
|
+
return true;
|
|
8
|
+
},
|
|
9
|
+
[TYPE_INDEX_MAP.binary]: (value) => {
|
|
10
|
+
if (value instanceof Uint8Array) {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
return false;
|
|
14
|
+
},
|
|
15
|
+
[TYPE_INDEX_MAP.boolean]: (value) => {
|
|
16
|
+
if (typeof value !== 'boolean') {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
return true;
|
|
20
|
+
},
|
|
21
|
+
[TYPE_INDEX_MAP.cardinality]: (val) => {
|
|
22
|
+
return (typeof val === 'string' ||
|
|
23
|
+
(val instanceof Uint8Array && val.byteLength === 8));
|
|
24
|
+
},
|
|
25
|
+
[TYPE_INDEX_MAP.timestamp]: (value, t) => {
|
|
26
|
+
if (typeof value === 'string') {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
if (typeof value !== 'number' || value % 1 !== 0) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
if (t.min !== undefined && value < t.min) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
if (t.max !== undefined && value > t.max) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
return true;
|
|
39
|
+
},
|
|
40
|
+
[TYPE_INDEX_MAP.int16]: (value, t) => {
|
|
41
|
+
if (typeof value !== 'number' || value % 1 !== 0) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
if (value > 32767 || value < -32768) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
if (t.min !== undefined && value < t.min) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
if (t.max !== undefined && value > t.max) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
},
|
|
55
|
+
[TYPE_INDEX_MAP.int32]: (value, t) => {
|
|
56
|
+
if (typeof value !== 'number' || value % 1 !== 0) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
if (value > 2147483647 || value < -2147483648) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
if (t.min !== undefined && value < t.min) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
if (t.max !== undefined && value > t.max) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
},
|
|
70
|
+
[TYPE_INDEX_MAP.int8]: (value, t) => {
|
|
71
|
+
// use % for steps size
|
|
72
|
+
if (typeof value !== 'number' || value % 1 !== 0) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
if (value > 127 || value < -128) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
if (t.min !== undefined && value < t.min) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
if (t.max !== undefined && value > t.max) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
return true;
|
|
85
|
+
},
|
|
86
|
+
[TYPE_INDEX_MAP.uint8]: (value, t) => {
|
|
87
|
+
if (typeof value !== 'number' || value % 1 !== 0) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
if (value > 255 || value < 0) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
if (t.min !== undefined && value < t.min) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
if (t.max !== undefined && value > t.max) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
return true;
|
|
100
|
+
},
|
|
101
|
+
[TYPE_INDEX_MAP.uint16]: (value, t) => {
|
|
102
|
+
if (typeof value !== 'number' || value % 1 !== 0) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
if (value > 65535 || value < 0) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
if (t.min !== undefined && value < t.min) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
if (t.max !== undefined && value > t.max) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
return true;
|
|
115
|
+
},
|
|
116
|
+
[TYPE_INDEX_MAP.uint32]: (value, t) => {
|
|
117
|
+
if (typeof value !== 'number' || value % 1 !== 0) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
if (value > 4294967295 || value < 0) {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
if (t.min !== undefined && value < t.min) {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
if (t.max !== undefined && value > t.max) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
return true;
|
|
130
|
+
},
|
|
131
|
+
[TYPE_INDEX_MAP.number]: (value, t) => {
|
|
132
|
+
if (typeof value !== 'number') {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
if (t.min !== undefined && value < t.min) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
if (t.max !== undefined && value > t.max) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
return true;
|
|
142
|
+
},
|
|
143
|
+
[TYPE_INDEX_MAP.enum]: (value, prop) => {
|
|
144
|
+
if (value === null) {
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
const arr = prop.enum;
|
|
148
|
+
for (let i = 0; i < arr.length; i++) {
|
|
149
|
+
if (value === arr[i]) {
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return false;
|
|
154
|
+
},
|
|
155
|
+
[TYPE_INDEX_MAP.id]: (value) => {
|
|
156
|
+
if (typeof value !== 'number' || value % 1 !== 0) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
return true;
|
|
160
|
+
},
|
|
161
|
+
[TYPE_INDEX_MAP.json]: (value) => {
|
|
162
|
+
return true;
|
|
163
|
+
},
|
|
164
|
+
[TYPE_INDEX_MAP.microbuffer]: (value) => {
|
|
165
|
+
if (!(value instanceof Uint8Array)) {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
return true;
|
|
169
|
+
},
|
|
170
|
+
[TYPE_INDEX_MAP.reference]: (value) => {
|
|
171
|
+
// if (typeof value !== 'number' && value != null) {
|
|
172
|
+
// return false
|
|
173
|
+
// }
|
|
174
|
+
return true;
|
|
175
|
+
},
|
|
176
|
+
[TYPE_INDEX_MAP.references]: (v) => {
|
|
177
|
+
if (typeof v !== 'number') {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
if (v === 0) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
return true;
|
|
184
|
+
},
|
|
185
|
+
[TYPE_INDEX_MAP.string]: (value, t) => {
|
|
186
|
+
// add max etc all here - make a ref to the original SCHEMA
|
|
187
|
+
if (typeof value !== 'string' && !(value instanceof Uint8Array)) {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
return true;
|
|
191
|
+
},
|
|
192
|
+
[TYPE_INDEX_MAP.aliases]: (value) => {
|
|
193
|
+
if (!Array.isArray(value)) {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
const len = value.length;
|
|
197
|
+
for (let i = 0; i < len; i++) {
|
|
198
|
+
if (typeof value[i] !== 'string') {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return true;
|
|
203
|
+
},
|
|
204
|
+
[TYPE_INDEX_MAP.vector]: (value) => {
|
|
205
|
+
// Array should be supported
|
|
206
|
+
if (!(value instanceof Float32Array)) {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
return true;
|
|
210
|
+
},
|
|
211
|
+
};
|
|
212
|
+
export const defaultValidation = () => true;
|
|
213
|
+
//# sourceMappingURL=validation.js.map
|
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;
|
|
@@ -97,6 +98,7 @@ export class SchemaParser {
|
|
|
97
98
|
throw Error(UNKNOWN_PROP);
|
|
98
99
|
}
|
|
99
100
|
}
|
|
101
|
+
return this.schema;
|
|
100
102
|
}
|
|
101
103
|
}
|
|
102
104
|
export const print = (schema, path) => {
|
|
@@ -120,9 +122,7 @@ export const print = (schema, path) => {
|
|
|
120
122
|
export const parse = (schema) => {
|
|
121
123
|
const parser = new SchemaParser(schema);
|
|
122
124
|
try {
|
|
123
|
-
parser.parse();
|
|
124
|
-
// @ts-ignore
|
|
125
|
-
return { schema };
|
|
125
|
+
return { schema: parser.parse() };
|
|
126
126
|
}
|
|
127
127
|
catch (e) {
|
|
128
128
|
const cause = parser.path.slice(0, Math.min(4, parser.lvl) + 1);
|
package/dist/parse/props.js
CHANGED
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { LangName } from './lang.js';
|
|
2
|
+
import { Validation } from './def/validation.js';
|
|
2
3
|
type Role = 'title' | 'source' | 'media' | string;
|
|
3
4
|
export declare const numberDisplays: readonly ["short", "human", "ratio", "bytes", "euro", "dollar", "pound", "meter"];
|
|
4
5
|
export declare const dateDisplays: readonly ["date", "date-time", "date-time-text", "date-time-human", "time", "time-precise"];
|
|
@@ -14,6 +15,7 @@ type QueryFn = Function;
|
|
|
14
15
|
type PropValues = {
|
|
15
16
|
type?: string;
|
|
16
17
|
default?: any;
|
|
18
|
+
validation?: Validation;
|
|
17
19
|
};
|
|
18
20
|
type Prop<V extends PropValues> = {
|
|
19
21
|
required?: boolean;
|
|
@@ -40,6 +42,7 @@ export type SchemaReferencesOneWay = Prop<{
|
|
|
40
42
|
export type SchemaText = Prop<{
|
|
41
43
|
type: 'text';
|
|
42
44
|
default?: Record<string, string>;
|
|
45
|
+
format?: StringFormat;
|
|
43
46
|
}>;
|
|
44
47
|
type NumberType = 'number' | 'int8' | 'uint8' | 'int16' | 'uint16' | 'int32' | 'uint32';
|
|
45
48
|
export type SchemaNumber = Prop<{
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@based/schema",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.9",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
"package.json",
|
|
9
9
|
"!dist/**/*.map"
|
|
10
10
|
],
|
|
11
|
+
"main": "./dist/index.js",
|
|
11
12
|
"exports": {
|
|
12
13
|
"./def": "./dist/def/index.js",
|
|
13
14
|
".": "./dist/index.js"
|
|
@@ -28,7 +29,7 @@
|
|
|
28
29
|
"typescript": "^5.6.3"
|
|
29
30
|
},
|
|
30
31
|
"dependencies": {
|
|
31
|
-
"@saulx/utils": "^
|
|
32
|
+
"@saulx/utils": "^6.1.1",
|
|
32
33
|
"picocolors": "^1.1.0"
|
|
33
34
|
}
|
|
34
35
|
}
|