@based/schema 4.1.1 → 5.0.0-alpha.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.
- package/README.md +152 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/lang.d.ts +153 -0
- package/dist/lang.js +153 -0
- package/dist/mermaid.d.ts +3 -0
- package/dist/mermaid.js +24 -0
- package/dist/parse/assert.d.ts +2 -1
- package/dist/parse/assert.js +9 -3
- package/dist/parse/errors.d.ts +2 -0
- package/dist/parse/errors.js +2 -0
- package/dist/parse/index.d.ts +5 -4
- package/dist/parse/index.js +43 -41
- package/dist/parse/props.js +168 -75
- package/dist/parse/utils.d.ts +2 -2
- package/dist/parse/utils.js +11 -2
- package/dist/types.d.ts +72 -23
- package/dist/types.js +93 -0
- package/package.json +4 -3
- package/dist/parsePayload/index.d.ts +0 -3
- package/dist/parsePayload/index.js +0 -2
- package/dist/parseSchema/assert.d.ts +0 -6
- package/dist/parseSchema/assert.js +0 -27
- package/dist/parseSchema/errors.d.ts +0 -19
- package/dist/parseSchema/errors.js +0 -19
- package/dist/parseSchema/index.d.ts +0 -20
- package/dist/parseSchema/index.js +0 -132
- package/dist/parseSchema/props.d.ts +0 -7
- package/dist/parseSchema/props.js +0 -256
- package/dist/parseSchema/utils.d.ts +0 -3
- package/dist/parseSchema/utils.js +0 -29
- package/dist/src/compat/index.d.ts +0 -6
- package/dist/src/compat/index.js +0 -217
- package/dist/src/compat/oldSchemaType.d.ts +0 -93
- package/dist/src/compat/oldSchemaType.js +0 -2
- package/dist/src/display/index.d.ts +0 -2
- package/dist/src/display/index.js +0 -16
- package/dist/src/display/number.d.ts +0 -4
- package/dist/src/display/number.js +0 -97
- package/dist/src/display/string.d.ts +0 -5
- package/dist/src/display/string.js +0 -26
- package/dist/src/display/timestamp.d.ts +0 -5
- package/dist/src/display/timestamp.js +0 -134
- package/dist/src/error.d.ts +0 -21
- package/dist/src/error.js +0 -23
- package/dist/src/index.d.ts +0 -6
- package/dist/src/index.js +0 -7
- package/dist/src/languages.d.ts +0 -188
- package/dist/src/languages.js +0 -188
- package/dist/src/set/fields/array.d.ts +0 -2
- package/dist/src/set/fields/array.js +0 -119
- package/dist/src/set/fields/index.d.ts +0 -3
- package/dist/src/set/fields/index.js +0 -85
- package/dist/src/set/fields/number.d.ts +0 -4
- package/dist/src/set/fields/number.js +0 -123
- package/dist/src/set/fields/object.d.ts +0 -3
- package/dist/src/set/fields/object.js +0 -28
- package/dist/src/set/fields/references.d.ts +0 -3
- package/dist/src/set/fields/references.js +0 -136
- package/dist/src/set/fields/set.d.ts +0 -2
- package/dist/src/set/fields/set.js +0 -59
- package/dist/src/set/fields/string.d.ts +0 -3
- package/dist/src/set/fields/string.js +0 -289
- package/dist/src/set/index.d.ts +0 -3
- package/dist/src/set/index.js +0 -205
- package/dist/src/set/isValidId.d.ts +0 -2
- package/dist/src/set/isValidId.js +0 -17
- package/dist/src/types.d.ts +0 -205
- package/dist/src/types.js +0 -113
- package/dist/src/updateSchema.d.ts +0 -2
- package/dist/src/updateSchema.js +0 -12
- package/dist/src/validateSchema/basedSchemaTypeValidator.d.ts +0 -3
- package/dist/src/validateSchema/basedSchemaTypeValidator.js +0 -45
- package/dist/src/validateSchema/fieldValidators.d.ts +0 -27
- package/dist/src/validateSchema/fieldValidators.js +0 -360
- package/dist/src/validateSchema/index.d.ts +0 -17
- package/dist/src/validateSchema/index.js +0 -109
- package/dist/src/validateSchema/utils.d.ts +0 -25
- package/dist/src/validateSchema/utils.js +0 -61
- package/dist/src/walker/args.d.ts +0 -36
- package/dist/src/walker/args.js +0 -158
- package/dist/src/walker/index.d.ts +0 -6
- package/dist/src/walker/index.js +0 -31
- package/dist/src/walker/parse.d.ts +0 -3
- package/dist/src/walker/parse.js +0 -182
- package/dist/src/walker/types.d.ts +0 -45
- package/dist/src/walker/types.js +0 -7
- package/dist/test/array.d.ts +0 -1
- package/dist/test/array.js +0 -342
- package/dist/test/compat.d.ts +0 -1
- package/dist/test/compat.js +0 -97
- package/dist/test/data/newSchemas.d.ts +0 -2
- package/dist/test/data/newSchemas.js +0 -489
- package/dist/test/data/oldSchemas.d.ts +0 -2
- package/dist/test/data/oldSchemas.js +0 -5058
- package/dist/test/display.d.ts +0 -1
- package/dist/test/display.js +0 -103
- package/dist/test/json.d.ts +0 -1
- package/dist/test/json.js +0 -40
- package/dist/test/number.d.ts +0 -1
- package/dist/test/number.js +0 -374
- package/dist/test/reference.d.ts +0 -1
- package/dist/test/reference.js +0 -188
- package/dist/test/rest.d.ts +0 -1
- package/dist/test/rest.js +0 -168
- package/dist/test/set.d.ts +0 -1
- package/dist/test/set.js +0 -95
- package/dist/test/string.d.ts +0 -1
- package/dist/test/string.js +0 -120
- package/dist/test/text.d.ts +0 -1
- package/dist/test/text.js +0 -324
- package/dist/test/utils/index.d.ts +0 -11
- package/dist/test/utils/index.js +0 -17
- package/dist/test/validateSchema/basic.d.ts +0 -1
- package/dist/test/validateSchema/basic.js +0 -94
- package/dist/test/validateSchema/fields.d.ts +0 -1
- package/dist/test/validateSchema/fields.js +0 -436
- package/dist/test/validateSchema/languages.d.ts +0 -1
- package/dist/test/validateSchema/languages.js +0 -124
- package/dist/test/validateSchema/realWorld.d.ts +0 -1
- package/dist/test/validateSchema/realWorld.js +0 -13
- package/dist/test/walker.d.ts +0 -1
- package/dist/test/walker.js +0 -289
package/dist/parse/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { INVALID_VALUE, UNKNOWN_PROP } from './errors.js';
|
|
1
|
+
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';
|
|
@@ -8,33 +8,55 @@ export class SchemaParser {
|
|
|
8
8
|
constructor(schema) {
|
|
9
9
|
this.schema = schema;
|
|
10
10
|
}
|
|
11
|
+
isItems;
|
|
11
12
|
inQuery;
|
|
12
13
|
schema;
|
|
13
14
|
type;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
this.parseProps(type.props, type);
|
|
17
|
-
}
|
|
15
|
+
path = [];
|
|
16
|
+
lvl = 0;
|
|
18
17
|
parseTypes() {
|
|
18
|
+
this.path[this.lvl++] = 'types';
|
|
19
19
|
const { types } = this.schema;
|
|
20
20
|
expectObject(types);
|
|
21
21
|
for (const type in types) {
|
|
22
|
-
this.
|
|
22
|
+
this.lvl++;
|
|
23
|
+
if (type[0] === '_') {
|
|
24
|
+
throw new Error(INVALID_KEY);
|
|
25
|
+
}
|
|
26
|
+
this.path[this.lvl] = type;
|
|
27
|
+
expectObject(types[type]);
|
|
28
|
+
if (!('props' in types[type])) {
|
|
29
|
+
types[type] = { props: types[type] };
|
|
30
|
+
}
|
|
31
|
+
this.lvl--;
|
|
32
|
+
}
|
|
33
|
+
for (const type in types) {
|
|
34
|
+
this.path[this.lvl++] = type;
|
|
35
|
+
this.parseProps(types[type].props, types[type]);
|
|
36
|
+
this.lvl--;
|
|
23
37
|
}
|
|
38
|
+
this.lvl--;
|
|
24
39
|
}
|
|
25
40
|
parseProps(props, schemaType = null) {
|
|
41
|
+
this.path[this.lvl++] = 'props';
|
|
26
42
|
expectObject(props);
|
|
27
43
|
this.type = schemaType;
|
|
28
44
|
for (const key in props) {
|
|
45
|
+
if (key[0] === '_') {
|
|
46
|
+
throw new Error(INVALID_KEY);
|
|
47
|
+
}
|
|
29
48
|
const prop = props[key];
|
|
30
|
-
const type = getPropType(prop);
|
|
49
|
+
const type = getPropType(prop, props, key);
|
|
50
|
+
this.path[this.lvl++] = key;
|
|
31
51
|
if (type in propParsers) {
|
|
32
52
|
propParsers[type](prop, this);
|
|
33
53
|
}
|
|
34
54
|
else {
|
|
35
55
|
throw Error(INVALID_VALUE);
|
|
36
56
|
}
|
|
57
|
+
this.lvl--;
|
|
37
58
|
}
|
|
59
|
+
this.lvl--;
|
|
38
60
|
}
|
|
39
61
|
parseLocales() {
|
|
40
62
|
const { locales } = this.schema;
|
|
@@ -60,17 +82,18 @@ export class SchemaParser {
|
|
|
60
82
|
}
|
|
61
83
|
parse() {
|
|
62
84
|
expectObject(this.schema);
|
|
85
|
+
// always do types first because it removes props shorthand
|
|
86
|
+
if ('types' in this.schema) {
|
|
87
|
+
this.parseTypes();
|
|
88
|
+
}
|
|
63
89
|
for (const key in this.schema) {
|
|
64
|
-
if (key === '
|
|
65
|
-
this.parseTypes();
|
|
66
|
-
}
|
|
67
|
-
else if (key === 'props') {
|
|
90
|
+
if (key === 'props') {
|
|
68
91
|
this.parseProps(this.schema.props);
|
|
69
92
|
}
|
|
70
93
|
else if (key === 'locales') {
|
|
71
94
|
this.parseLocales();
|
|
72
95
|
}
|
|
73
|
-
else {
|
|
96
|
+
else if (key !== 'types') {
|
|
74
97
|
throw Error(UNKNOWN_PROP);
|
|
75
98
|
}
|
|
76
99
|
}
|
|
@@ -83,7 +106,7 @@ export const print = (schema, path) => {
|
|
|
83
106
|
const v = obj[key];
|
|
84
107
|
const padding = ' '.repeat(lvl);
|
|
85
108
|
const prefix = key === Object.keys(obj)[0] ? '' : `${padding}...\n`;
|
|
86
|
-
if (lvl === depth) {
|
|
109
|
+
if (lvl === depth && lvl !== 0) {
|
|
87
110
|
const err = key in obj
|
|
88
111
|
? `${key}: ${typeof v === 'object' && v !== null && !Array.isArray(v) ? `{..}` : JSON.stringify(v)}`
|
|
89
112
|
: key;
|
|
@@ -94,39 +117,18 @@ export const print = (schema, path) => {
|
|
|
94
117
|
});
|
|
95
118
|
return lines.join('\n');
|
|
96
119
|
};
|
|
97
|
-
export const debug = (schema) => {
|
|
98
|
-
let curr;
|
|
99
|
-
const proxy = (obj, path = []) => {
|
|
100
|
-
const copy = {};
|
|
101
|
-
return new Proxy(obj, {
|
|
102
|
-
get(_, key) {
|
|
103
|
-
const v = obj[key];
|
|
104
|
-
curr = [...path, key];
|
|
105
|
-
if (typeof v !== 'object' || v === null) {
|
|
106
|
-
return v;
|
|
107
|
-
}
|
|
108
|
-
copy[key] ??= proxy(obj[key], curr);
|
|
109
|
-
return copy[key];
|
|
110
|
-
},
|
|
111
|
-
});
|
|
112
|
-
};
|
|
113
|
-
const parser = new SchemaParser(proxy(schema));
|
|
114
|
-
try {
|
|
115
|
-
parser.parse();
|
|
116
|
-
}
|
|
117
|
-
catch (e) {
|
|
118
|
-
e.message += '\n\n' + print(schema, curr) + '\n';
|
|
119
|
-
e.cause = curr;
|
|
120
|
-
throw e;
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
120
|
export const parse = (schema) => {
|
|
121
|
+
const parser = new SchemaParser(schema);
|
|
124
122
|
try {
|
|
125
|
-
|
|
123
|
+
parser.parse();
|
|
124
|
+
// @ts-ignore
|
|
126
125
|
return { schema };
|
|
127
126
|
}
|
|
128
127
|
catch (e) {
|
|
129
|
-
|
|
128
|
+
const cause = parser.path.slice(0, Math.min(4, parser.lvl) + 1);
|
|
129
|
+
e.message += '\n\n' + print(schema, cause) + '\n';
|
|
130
|
+
e.cause = cause;
|
|
131
|
+
throw e;
|
|
130
132
|
}
|
|
131
133
|
};
|
|
132
134
|
//# sourceMappingURL=index.js.map
|
package/dist/parse/props.js
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
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';
|
|
3
4
|
import { getPropType } from './utils.js';
|
|
5
|
+
let stringFormatsSet;
|
|
6
|
+
let numberDisplaysSet;
|
|
7
|
+
let dateDisplaysSet;
|
|
4
8
|
const STUB = {};
|
|
5
9
|
const shared = {
|
|
6
10
|
type() { },
|
|
7
|
-
|
|
11
|
+
role(val) {
|
|
12
|
+
expectString(val);
|
|
13
|
+
},
|
|
14
|
+
required(val, _prop, ctx) {
|
|
15
|
+
if (ctx.isItems) {
|
|
16
|
+
throw new Error(NOT_ALLOWED_IN_ITEMS);
|
|
17
|
+
}
|
|
8
18
|
expectBoolean(val);
|
|
9
19
|
},
|
|
10
20
|
query(val) {
|
|
@@ -28,6 +38,10 @@ const shared = {
|
|
|
28
38
|
throw Error(TYPE_MISMATCH);
|
|
29
39
|
}
|
|
30
40
|
},
|
|
41
|
+
title(val) { },
|
|
42
|
+
description(val) { },
|
|
43
|
+
readOnly(val) { },
|
|
44
|
+
examples(val) { },
|
|
31
45
|
};
|
|
32
46
|
function propParser(required, optional, allowShorthand) {
|
|
33
47
|
return (prop, ctx) => {
|
|
@@ -46,15 +60,21 @@ function propParser(required, optional, allowShorthand) {
|
|
|
46
60
|
throw Error(EXPECTED_OBJ);
|
|
47
61
|
}
|
|
48
62
|
for (const key in required) {
|
|
49
|
-
|
|
63
|
+
ctx.path[ctx.lvl] = key;
|
|
64
|
+
const changed = required[key](prop[key], prop, ctx);
|
|
65
|
+
if (changed !== undefined) {
|
|
66
|
+
prop[key] = changed;
|
|
67
|
+
}
|
|
50
68
|
}
|
|
51
69
|
for (const key in prop) {
|
|
70
|
+
ctx.path[ctx.lvl] = key;
|
|
52
71
|
const val = prop[key];
|
|
72
|
+
let changed;
|
|
53
73
|
if (key in optional) {
|
|
54
|
-
optional[key](val, prop, ctx);
|
|
74
|
+
changed = optional[key](val, prop, ctx);
|
|
55
75
|
}
|
|
56
76
|
else if (key in shared) {
|
|
57
|
-
shared[key](val, prop, ctx);
|
|
77
|
+
changed = shared[key](val, prop, ctx);
|
|
58
78
|
}
|
|
59
79
|
else if (!(key in required)) {
|
|
60
80
|
if (key[0] === '$' && 'ref' in prop) {
|
|
@@ -64,18 +84,25 @@ function propParser(required, optional, allowShorthand) {
|
|
|
64
84
|
throw Error(UNKNOWN_PROP);
|
|
65
85
|
}
|
|
66
86
|
}
|
|
87
|
+
if (changed !== undefined) {
|
|
88
|
+
prop[key] = changed;
|
|
89
|
+
}
|
|
67
90
|
}
|
|
68
91
|
};
|
|
69
92
|
}
|
|
70
93
|
const p = {};
|
|
71
|
-
p.
|
|
94
|
+
p.boolean = propParser(STUB, {
|
|
72
95
|
default(val) {
|
|
73
|
-
|
|
96
|
+
expectBoolean(val);
|
|
74
97
|
},
|
|
75
98
|
}, 0);
|
|
76
|
-
p.
|
|
99
|
+
p.vector = propParser({
|
|
100
|
+
size(val) {
|
|
101
|
+
expectNumber(val);
|
|
102
|
+
},
|
|
103
|
+
}, {
|
|
77
104
|
default(val) {
|
|
78
|
-
|
|
105
|
+
expectFloat32Array(val);
|
|
79
106
|
},
|
|
80
107
|
}, 0);
|
|
81
108
|
p.enum = propParser({
|
|
@@ -100,6 +127,11 @@ p.enum = propParser({
|
|
|
100
127
|
},
|
|
101
128
|
}, 1);
|
|
102
129
|
const numberOpts = {
|
|
130
|
+
display(val) {
|
|
131
|
+
expectString(val);
|
|
132
|
+
numberDisplaysSet ??= new Set(numberDisplays);
|
|
133
|
+
numberDisplaysSet.has(val);
|
|
134
|
+
},
|
|
103
135
|
min(val) {
|
|
104
136
|
expectNumber(val);
|
|
105
137
|
},
|
|
@@ -144,63 +176,6 @@ p.object = propParser({
|
|
|
144
176
|
console.warn('TODO object default value');
|
|
145
177
|
},
|
|
146
178
|
});
|
|
147
|
-
p.reference = propParser({
|
|
148
|
-
ref(ref, _prop, { schema }) {
|
|
149
|
-
schema.types[ref].props;
|
|
150
|
-
},
|
|
151
|
-
prop(propKey, prop, { schema, type, inQuery }) {
|
|
152
|
-
const propAllowed = type && !inQuery;
|
|
153
|
-
if (propAllowed) {
|
|
154
|
-
expectString(propKey);
|
|
155
|
-
const propPath = propKey.split('.');
|
|
156
|
-
let targetProp = schema.types[prop.ref];
|
|
157
|
-
for (const key of propPath) {
|
|
158
|
-
targetProp = targetProp.props[key];
|
|
159
|
-
}
|
|
160
|
-
if ('items' in targetProp) {
|
|
161
|
-
targetProp = targetProp.items;
|
|
162
|
-
}
|
|
163
|
-
if ('ref' in targetProp && 'prop' in targetProp) {
|
|
164
|
-
const inversePath = targetProp.prop.split('.');
|
|
165
|
-
let inverseProp = schema.types[targetProp.ref];
|
|
166
|
-
for (const key of inversePath) {
|
|
167
|
-
inverseProp = inverseProp.props[key];
|
|
168
|
-
}
|
|
169
|
-
if ('items' in inverseProp) {
|
|
170
|
-
inverseProp = inverseProp.items;
|
|
171
|
-
}
|
|
172
|
-
if (inverseProp === prop) {
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
throw Error(INVALID_VALUE);
|
|
177
|
-
}
|
|
178
|
-
if (propKey !== undefined) {
|
|
179
|
-
throw Error('ref prop not supported on root or edge p');
|
|
180
|
-
}
|
|
181
|
-
},
|
|
182
|
-
}, {
|
|
183
|
-
default(val) {
|
|
184
|
-
expectString(val);
|
|
185
|
-
},
|
|
186
|
-
edge(val, prop, ctx, key) {
|
|
187
|
-
const edgeAllowed = ctx.type && !ctx.inQuery;
|
|
188
|
-
if (edgeAllowed) {
|
|
189
|
-
let t = ctx.schema.types[prop.ref].props[prop.prop];
|
|
190
|
-
t = t.items || t;
|
|
191
|
-
if (t[key]) {
|
|
192
|
-
throw Error('Edge can not be defined on both props');
|
|
193
|
-
}
|
|
194
|
-
const edgePropType = getPropType(val);
|
|
195
|
-
const inType = ctx.type;
|
|
196
|
-
ctx.type = null;
|
|
197
|
-
p[edgePropType](val, ctx);
|
|
198
|
-
ctx.type = inType;
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
throw Error('ref edge not supported on root or edge p');
|
|
202
|
-
},
|
|
203
|
-
});
|
|
204
179
|
p.set = propParser({
|
|
205
180
|
items(items, prop, ctx) {
|
|
206
181
|
expectObject(items);
|
|
@@ -210,8 +185,10 @@ p.set = propParser({
|
|
|
210
185
|
itemsType === 'timestamp' ||
|
|
211
186
|
itemsType === 'boolean') {
|
|
212
187
|
ctx.inQuery = 'query' in prop;
|
|
188
|
+
ctx.isItems = true;
|
|
213
189
|
p[itemsType](items, ctx);
|
|
214
190
|
ctx.inQuery = false;
|
|
191
|
+
ctx.isItems = false;
|
|
215
192
|
}
|
|
216
193
|
else {
|
|
217
194
|
throw new Error(INVALID_VALUE);
|
|
@@ -231,8 +208,10 @@ p.references = propParser({
|
|
|
231
208
|
const itemsType = getPropType(items);
|
|
232
209
|
if (itemsType === 'reference') {
|
|
233
210
|
ctx.inQuery = 'query' in prop;
|
|
211
|
+
ctx.isItems = true;
|
|
234
212
|
p[itemsType](items, ctx);
|
|
235
213
|
ctx.inQuery = false;
|
|
214
|
+
ctx.isItems = false;
|
|
236
215
|
}
|
|
237
216
|
else {
|
|
238
217
|
throw new Error(INVALID_VALUE);
|
|
@@ -246,21 +225,34 @@ p.references = propParser({
|
|
|
246
225
|
// }
|
|
247
226
|
},
|
|
248
227
|
});
|
|
249
|
-
|
|
228
|
+
const binaryOpts = {
|
|
250
229
|
default(val) {
|
|
251
|
-
|
|
252
|
-
},
|
|
253
|
-
maxBytes(val) {
|
|
254
|
-
expectNumber(val);
|
|
230
|
+
expectString(val);
|
|
255
231
|
},
|
|
256
|
-
|
|
257
|
-
p.string = propParser(STUB, {
|
|
258
|
-
default(val) {
|
|
232
|
+
format(val) {
|
|
259
233
|
expectString(val);
|
|
234
|
+
stringFormatsSet ??= new Set(stringFormats);
|
|
235
|
+
stringFormatsSet.has(val);
|
|
236
|
+
},
|
|
237
|
+
mime(val) {
|
|
238
|
+
if (Array.isArray(val)) {
|
|
239
|
+
val.forEach(expectString);
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
expectString(val);
|
|
243
|
+
}
|
|
260
244
|
},
|
|
261
245
|
maxBytes(val) {
|
|
262
246
|
expectNumber(val);
|
|
263
247
|
},
|
|
248
|
+
compression(val) {
|
|
249
|
+
// return the actualy string!
|
|
250
|
+
return val;
|
|
251
|
+
},
|
|
252
|
+
};
|
|
253
|
+
p.binary = propParser(STUB, binaryOpts, 0);
|
|
254
|
+
p.string = propParser(STUB, {
|
|
255
|
+
...binaryOpts,
|
|
264
256
|
min(val) {
|
|
265
257
|
expectNumber(val);
|
|
266
258
|
},
|
|
@@ -283,6 +275,11 @@ p.text = propParser({
|
|
|
283
275
|
},
|
|
284
276
|
}, 0);
|
|
285
277
|
p.timestamp = propParser(STUB, {
|
|
278
|
+
display(val) {
|
|
279
|
+
expectString(val);
|
|
280
|
+
dateDisplaysSet ??= new Set(dateDisplays);
|
|
281
|
+
dateDisplaysSet.has(val);
|
|
282
|
+
},
|
|
286
283
|
default(val) {
|
|
287
284
|
if (typeof val !== 'number' && !(val instanceof Date)) {
|
|
288
285
|
throw Error(EXPECTED_DATE);
|
|
@@ -294,5 +291,101 @@ p.timestamp = propParser(STUB, {
|
|
|
294
291
|
}
|
|
295
292
|
},
|
|
296
293
|
}, 0);
|
|
294
|
+
p.reference = propParser({
|
|
295
|
+
ref(ref, _prop, { schema }) {
|
|
296
|
+
if (!schema.types[ref]?.props) {
|
|
297
|
+
throw Error(MISSING_TYPE);
|
|
298
|
+
}
|
|
299
|
+
},
|
|
300
|
+
prop(propKey, prop, { schema, type, inQuery, path }) {
|
|
301
|
+
const propAllowed = type && !inQuery;
|
|
302
|
+
if (propAllowed) {
|
|
303
|
+
expectString(propKey);
|
|
304
|
+
const propPath = propKey.split('.');
|
|
305
|
+
let targetProp = schema.types[prop.ref];
|
|
306
|
+
expectObject(targetProp, 'expected type');
|
|
307
|
+
let create;
|
|
308
|
+
for (const key of propPath) {
|
|
309
|
+
if (!targetProp.props[key]) {
|
|
310
|
+
create = true;
|
|
311
|
+
targetProp.props[key] = {};
|
|
312
|
+
}
|
|
313
|
+
targetProp = targetProp.props[key];
|
|
314
|
+
}
|
|
315
|
+
if (create) {
|
|
316
|
+
const ref = path[1];
|
|
317
|
+
let prop = '';
|
|
318
|
+
for (let i = 3; i < path.length - 1; i += 2) {
|
|
319
|
+
prop += prop ? `.${path[i]}` : path[i];
|
|
320
|
+
}
|
|
321
|
+
targetProp.readOnly = true;
|
|
322
|
+
targetProp.items = {
|
|
323
|
+
ref,
|
|
324
|
+
prop,
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
if ('items' in targetProp) {
|
|
328
|
+
targetProp = targetProp.items;
|
|
329
|
+
}
|
|
330
|
+
if ('ref' in targetProp && 'prop' in targetProp) {
|
|
331
|
+
const inversePath = targetProp.prop.split('.');
|
|
332
|
+
let inverseProp = schema.types[targetProp.ref];
|
|
333
|
+
for (const key of inversePath) {
|
|
334
|
+
inverseProp = inverseProp.props[key];
|
|
335
|
+
}
|
|
336
|
+
if (inverseProp && 'items' in inverseProp) {
|
|
337
|
+
inverseProp = inverseProp.items;
|
|
338
|
+
}
|
|
339
|
+
if (prop === inverseProp) {
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
throw Error('expected inverse property');
|
|
344
|
+
}
|
|
345
|
+
if (propKey !== undefined) {
|
|
346
|
+
throw Error('ref prop not supported on root or edge p');
|
|
347
|
+
}
|
|
348
|
+
},
|
|
349
|
+
}, {
|
|
350
|
+
mime: binaryOpts.mime,
|
|
351
|
+
default(val) {
|
|
352
|
+
expectString(val);
|
|
353
|
+
},
|
|
354
|
+
edge(val, prop, ctx, key) {
|
|
355
|
+
const edgeAllowed = ctx.type && !ctx.inQuery;
|
|
356
|
+
if (edgeAllowed) {
|
|
357
|
+
let t = ctx.schema.types[prop.ref].props[prop.prop];
|
|
358
|
+
t = t.items || t;
|
|
359
|
+
if (t[key] && t !== prop) {
|
|
360
|
+
throw Error('Edge can not be defined on both props');
|
|
361
|
+
}
|
|
362
|
+
const edgePropType = getPropType(val);
|
|
363
|
+
const inType = ctx.type;
|
|
364
|
+
ctx.type = null;
|
|
365
|
+
p[edgePropType](val, ctx);
|
|
366
|
+
ctx.type = inType;
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
throw Error('ref edge not supported on root or edge property');
|
|
370
|
+
},
|
|
371
|
+
dependent(val, prop, ctx, key) {
|
|
372
|
+
expectBoolean(val);
|
|
373
|
+
const dependentAllowed = ctx.type && !ctx.inQuery;
|
|
374
|
+
if (!dependentAllowed) {
|
|
375
|
+
throw Error('ref dependency not supported on root or edge property');
|
|
376
|
+
}
|
|
377
|
+
},
|
|
378
|
+
});
|
|
379
|
+
p.alias = propParser(STUB, {
|
|
380
|
+
default(val) {
|
|
381
|
+
expectString(val);
|
|
382
|
+
},
|
|
383
|
+
format: binaryOpts.format,
|
|
384
|
+
}, 0);
|
|
385
|
+
p.hll = propParser(STUB, {
|
|
386
|
+
default(val) {
|
|
387
|
+
expectNumber(val);
|
|
388
|
+
},
|
|
389
|
+
}, 0);
|
|
297
390
|
export default p;
|
|
298
391
|
//# sourceMappingURL=props.js.map
|
package/dist/parse/utils.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { SchemaAnyProp, SchemaPropTypes } from '../types.js';
|
|
2
|
-
export declare const getPropType: (prop: SchemaAnyProp) => SchemaPropTypes;
|
|
1
|
+
import { SchemaAnyProp, SchemaProps, SchemaPropTypes } from '../types.js';
|
|
2
|
+
export declare const getPropType: (prop: SchemaAnyProp, props?: SchemaProps, key?: string) => SchemaPropTypes;
|
|
3
3
|
//# sourceMappingURL=utils.d.ts.map
|
package/dist/parse/utils.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { INVALID_TYPE, MISSING_TYPE } from './errors.js';
|
|
2
|
-
export const getPropType = (prop) => {
|
|
2
|
+
export const getPropType = (prop, props, key) => {
|
|
3
3
|
if (typeof prop === 'string') {
|
|
4
|
+
if (props) {
|
|
5
|
+
props[key] = { type: prop };
|
|
6
|
+
}
|
|
4
7
|
return prop;
|
|
5
8
|
}
|
|
6
9
|
if ('type' in prop) {
|
|
@@ -21,7 +24,13 @@ export const getPropType = (prop) => {
|
|
|
21
24
|
if ('props' in prop) {
|
|
22
25
|
return 'object';
|
|
23
26
|
}
|
|
24
|
-
if ('enum' in prop
|
|
27
|
+
if ('enum' in prop) {
|
|
28
|
+
return 'enum';
|
|
29
|
+
}
|
|
30
|
+
if (Array.isArray(prop)) {
|
|
31
|
+
if (props) {
|
|
32
|
+
props[key] = { enum: prop };
|
|
33
|
+
}
|
|
25
34
|
return 'enum';
|
|
26
35
|
}
|
|
27
36
|
throw Error(MISSING_TYPE);
|