@based/schema 3.2.0 → 4.1.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.
- package/dist/index.d.ts +3 -5
- package/dist/index.js +2 -21
- package/dist/parse/assert.d.ts +7 -0
- package/dist/parse/assert.js +33 -0
- package/dist/parse/errors.d.ts +19 -0
- package/dist/parse/errors.js +19 -0
- package/dist/parse/index.d.ts +20 -0
- package/dist/parse/index.js +132 -0
- package/dist/parse/props.d.ts +7 -0
- package/dist/parse/props.js +290 -0
- package/dist/parse/utils.d.ts +3 -0
- package/dist/parse/utils.js +29 -0
- package/dist/parsePayload/index.d.ts +3 -0
- package/dist/parsePayload/index.js +2 -0
- package/dist/parseSchema/assert.d.ts +6 -0
- package/dist/parseSchema/assert.js +27 -0
- package/dist/parseSchema/errors.d.ts +19 -0
- package/dist/parseSchema/errors.js +19 -0
- package/dist/parseSchema/index.d.ts +20 -0
- package/dist/parseSchema/index.js +132 -0
- package/dist/parseSchema/props.d.ts +7 -0
- package/dist/parseSchema/props.js +256 -0
- package/dist/parseSchema/utils.d.ts +3 -0
- package/dist/parseSchema/utils.js +29 -0
- package/dist/src/compat/index.js +2 -2
- package/dist/src/set/fields/references.js +3 -30
- package/dist/src/types.d.ts +5 -13
- package/dist/src/types.js +14 -0
- package/dist/src/validateSchema/fieldValidators.js +4 -4
- package/dist/test/compat.js +1 -1
- package/dist/test/data/newSchemas.js +19 -21
- package/dist/test/reference.js +4 -2
- package/dist/test/walker.js +2 -2
- package/dist/types.d.ts +129 -194
- package/dist/types.js +3 -25
- package/package.json +14 -25
- package/README.md +0 -2
- package/dist/display/index.d.ts +0 -2
- package/dist/display/index.js +0 -26
- package/dist/display/number.d.ts +0 -3
- package/dist/display/number.js +0 -89
- package/dist/display/string.d.ts +0 -3
- package/dist/display/string.js +0 -23
- package/dist/display/timestamp.d.ts +0 -3
- package/dist/display/timestamp.js +0 -127
- package/dist/error.d.ts +0 -19
- package/dist/error.js +0 -24
- package/dist/languages.d.ts +0 -187
- package/dist/languages.js +0 -190
- package/dist/set/fields/array.d.ts +0 -2
- package/dist/set/fields/array.js +0 -123
- package/dist/set/fields/index.d.ts +0 -3
- package/dist/set/fields/index.js +0 -74
- package/dist/set/fields/number.d.ts +0 -4
- package/dist/set/fields/number.js +0 -129
- package/dist/set/fields/object.d.ts +0 -3
- package/dist/set/fields/object.js +0 -33
- package/dist/set/fields/references.d.ts +0 -3
- package/dist/set/fields/references.js +0 -128
- package/dist/set/fields/set.d.ts +0 -2
- package/dist/set/fields/set.js +0 -63
- package/dist/set/fields/string.d.ts +0 -3
- package/dist/set/fields/string.js +0 -284
- package/dist/set/index.d.ts +0 -3
- package/dist/set/index.js +0 -183
- package/dist/set/isValidId.d.ts +0 -2
- package/dist/set/isValidId.js +0 -21
- package/dist/set/types.d.ts +0 -0
- package/dist/set/types.js +0 -1
- package/dist/src/compat/newToOld.d.ts +0 -3
- package/dist/src/compat/newToOld.js +0 -218
- package/dist/src/compat/oldToNew.d.ts +0 -3
- package/dist/src/compat/oldToNew.js +0 -210
- package/dist/src/generateQuery.d.ts +0 -12
- package/dist/src/generateQuery.js +0 -75
- package/dist/test/query.d.ts +0 -1
- package/dist/test/query.js +0 -93
- package/dist/updateSchema.d.ts +0 -2
- package/dist/updateSchema.js +0 -16
- package/dist/validateSchema.d.ts +0 -4
- package/dist/validateSchema.js +0 -41
- package/dist/walker/args.d.ts +0 -36
- package/dist/walker/args.js +0 -162
- package/dist/walker/index.d.ts +0 -6
- package/dist/walker/index.js +0 -49
- package/dist/walker/parse.d.ts +0 -3
- package/dist/walker/parse.js +0 -186
- package/dist/walker/types.d.ts +0 -45
- package/dist/walker/types.js +0 -10
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,22 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./types"), exports);
|
|
18
|
-
__exportStar(require("./validateSchema"), exports);
|
|
19
|
-
__exportStar(require("./walker"), exports);
|
|
20
|
-
__exportStar(require("./set"), exports);
|
|
21
|
-
__exportStar(require("./display"), exports);
|
|
1
|
+
export * from './types.js';
|
|
2
|
+
export * from './parse/index.js';
|
|
22
3
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const expectObject: (obj: any) => void;
|
|
2
|
+
export declare const expectString: (obj: any) => void;
|
|
3
|
+
export declare const expectBoolean: (v: any) => void;
|
|
4
|
+
export declare const expectFunction: (v: any) => void;
|
|
5
|
+
export declare const expectNumber: (v: any) => void;
|
|
6
|
+
export declare const expectPositiveNumber: (v: any) => void;
|
|
7
|
+
//# sourceMappingURL=assert.d.ts.map
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { EXPECTED_BOOL, EXPECTED_FN, EXPECTED_NUM, EXPECTED_OBJ, EXPECTED_STR, } from './errors.js';
|
|
2
|
+
export const expectObject = (obj) => {
|
|
3
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
4
|
+
throw Error(EXPECTED_OBJ);
|
|
5
|
+
}
|
|
6
|
+
};
|
|
7
|
+
export const expectString = (obj) => {
|
|
8
|
+
if (typeof obj !== 'string') {
|
|
9
|
+
throw Error(EXPECTED_STR);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
export const expectBoolean = (v) => {
|
|
13
|
+
if (v !== true && v !== false) {
|
|
14
|
+
throw Error(EXPECTED_BOOL);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
export const expectFunction = (v) => {
|
|
18
|
+
if (typeof v !== 'function') {
|
|
19
|
+
throw Error(EXPECTED_FN);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
export const expectNumber = (v) => {
|
|
23
|
+
if (typeof v !== 'number') {
|
|
24
|
+
throw Error(EXPECTED_NUM);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
export const expectPositiveNumber = (v) => {
|
|
28
|
+
expectNumber(v);
|
|
29
|
+
if (v < 0) {
|
|
30
|
+
throw Error('Expected positive number');
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
//# sourceMappingURL=assert.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare const OUT_OF_RANGE = "Value is out of range";
|
|
2
|
+
export declare const EXPECTED_VALUE_IN_ENUM = "Expected value in \"enum\" field";
|
|
3
|
+
export declare const EXPECTED_DATE = "Expected number or Date";
|
|
4
|
+
export declare const EXPECTED_BOOL = "Expected boolean";
|
|
5
|
+
export declare const EXPECTED_OBJ = "Expected object";
|
|
6
|
+
export declare const EXPECTED_ARR = "Expected array";
|
|
7
|
+
export declare const EXPECTED_NUM = "Expected number";
|
|
8
|
+
export declare const EXPECTED_STR = "Expected string";
|
|
9
|
+
export declare const EXPECTED_FN = "Expected function";
|
|
10
|
+
export declare const EXPECTED_PRIMITIVE = "Expected primitive value";
|
|
11
|
+
export declare const INVALID_VALUE = "Invalid value";
|
|
12
|
+
export declare const INVALID_SCHEMA = "Invalid schema";
|
|
13
|
+
export declare const MIN_MAX = "Max value must be larger than min value";
|
|
14
|
+
export declare const UNKNOWN_PROP = "Unknown property";
|
|
15
|
+
export declare const MISSING_TYPE = "Missing type";
|
|
16
|
+
export declare const INVALID_TYPE = "Invalid type";
|
|
17
|
+
export declare const TEXT_REQUIRES_LOCALES = "Type text requires locales";
|
|
18
|
+
export declare const TYPE_MISMATCH = "Types do not match";
|
|
19
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export const OUT_OF_RANGE = 'Value is out of range';
|
|
2
|
+
export const EXPECTED_VALUE_IN_ENUM = 'Expected value in "enum" field';
|
|
3
|
+
export const EXPECTED_DATE = 'Expected number or Date';
|
|
4
|
+
export const EXPECTED_BOOL = 'Expected boolean';
|
|
5
|
+
export const EXPECTED_OBJ = 'Expected object';
|
|
6
|
+
export const EXPECTED_ARR = 'Expected array';
|
|
7
|
+
export const EXPECTED_NUM = 'Expected number';
|
|
8
|
+
export const EXPECTED_STR = 'Expected string';
|
|
9
|
+
export const EXPECTED_FN = 'Expected function';
|
|
10
|
+
export const EXPECTED_PRIMITIVE = 'Expected primitive value';
|
|
11
|
+
export const INVALID_VALUE = 'Invalid value';
|
|
12
|
+
export const INVALID_SCHEMA = 'Invalid schema';
|
|
13
|
+
export const MIN_MAX = 'Max value must be larger than min value';
|
|
14
|
+
export const UNKNOWN_PROP = 'Unknown property';
|
|
15
|
+
export const MISSING_TYPE = 'Missing type';
|
|
16
|
+
export const INVALID_TYPE = 'Invalid type';
|
|
17
|
+
export const TEXT_REQUIRES_LOCALES = 'Type text requires locales';
|
|
18
|
+
export const TYPE_MISMATCH = 'Types do not match';
|
|
19
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Schema, SchemaType } from '../types.js';
|
|
2
|
+
import { getPropType } from './utils.js';
|
|
3
|
+
export { getPropType };
|
|
4
|
+
export declare class SchemaParser {
|
|
5
|
+
constructor(schema: Schema);
|
|
6
|
+
inQuery: boolean;
|
|
7
|
+
schema: Schema;
|
|
8
|
+
type: SchemaType;
|
|
9
|
+
parseType(type: SchemaType): void;
|
|
10
|
+
parseTypes(): void;
|
|
11
|
+
parseProps(props: any, schemaType?: SchemaType): void;
|
|
12
|
+
parseLocales(): void;
|
|
13
|
+
parse(): void;
|
|
14
|
+
}
|
|
15
|
+
export declare const print: (schema: Schema, path: string[]) => string;
|
|
16
|
+
export declare const debug: (schema: Schema) => void;
|
|
17
|
+
export declare const parse: (schema: Schema) => {
|
|
18
|
+
schema: Schema;
|
|
19
|
+
};
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { INVALID_VALUE, UNKNOWN_PROP } from './errors.js';
|
|
2
|
+
import { getPropType } from './utils.js';
|
|
3
|
+
import propParsers from './props.js';
|
|
4
|
+
import pc from 'picocolors';
|
|
5
|
+
import { expectBoolean, expectObject } from './assert.js';
|
|
6
|
+
export { getPropType };
|
|
7
|
+
export class SchemaParser {
|
|
8
|
+
constructor(schema) {
|
|
9
|
+
this.schema = schema;
|
|
10
|
+
}
|
|
11
|
+
inQuery;
|
|
12
|
+
schema;
|
|
13
|
+
type;
|
|
14
|
+
parseType(type) {
|
|
15
|
+
expectObject(type);
|
|
16
|
+
this.parseProps(type.props, type);
|
|
17
|
+
}
|
|
18
|
+
parseTypes() {
|
|
19
|
+
const { types } = this.schema;
|
|
20
|
+
expectObject(types);
|
|
21
|
+
for (const type in types) {
|
|
22
|
+
this.parseType(types[type]);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
parseProps(props, schemaType = null) {
|
|
26
|
+
expectObject(props);
|
|
27
|
+
this.type = schemaType;
|
|
28
|
+
for (const key in props) {
|
|
29
|
+
const prop = props[key];
|
|
30
|
+
const type = getPropType(prop);
|
|
31
|
+
if (type in propParsers) {
|
|
32
|
+
propParsers[type](prop, this);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
throw Error(INVALID_VALUE);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
parseLocales() {
|
|
40
|
+
const { locales } = this.schema;
|
|
41
|
+
expectObject(locales);
|
|
42
|
+
for (const locale in locales) {
|
|
43
|
+
const opts = locales[locale];
|
|
44
|
+
expectObject(opts);
|
|
45
|
+
for (const key in opts) {
|
|
46
|
+
const val = opts[key];
|
|
47
|
+
if (key === 'required') {
|
|
48
|
+
expectBoolean(val);
|
|
49
|
+
}
|
|
50
|
+
else if (key === 'fallback') {
|
|
51
|
+
if (!Array.isArray(val) || !val.every((v) => typeof v === 'string')) {
|
|
52
|
+
throw Error(INVALID_VALUE);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
throw Error(UNKNOWN_PROP);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
parse() {
|
|
62
|
+
expectObject(this.schema);
|
|
63
|
+
for (const key in this.schema) {
|
|
64
|
+
if (key === 'types') {
|
|
65
|
+
this.parseTypes();
|
|
66
|
+
}
|
|
67
|
+
else if (key === 'props') {
|
|
68
|
+
this.parseProps(this.schema.props);
|
|
69
|
+
}
|
|
70
|
+
else if (key === 'locales') {
|
|
71
|
+
this.parseLocales();
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
throw Error(UNKNOWN_PROP);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export const print = (schema, path) => {
|
|
80
|
+
let obj = schema;
|
|
81
|
+
const depth = path.length - 1;
|
|
82
|
+
const lines = path.map((key, lvl) => {
|
|
83
|
+
const v = obj[key];
|
|
84
|
+
const padding = ' '.repeat(lvl);
|
|
85
|
+
const prefix = key === Object.keys(obj)[0] ? '' : `${padding}...\n`;
|
|
86
|
+
if (lvl === depth) {
|
|
87
|
+
const err = key in obj
|
|
88
|
+
? `${key}: ${typeof v === 'object' && v !== null && !Array.isArray(v) ? `{..}` : JSON.stringify(v)}`
|
|
89
|
+
: key;
|
|
90
|
+
return `${prefix}${'--'.repeat(lvl - 1)}> ${pc.red(err)}`;
|
|
91
|
+
}
|
|
92
|
+
obj = v;
|
|
93
|
+
return `${prefix}${padding}${key}: {`;
|
|
94
|
+
});
|
|
95
|
+
return lines.join('\n');
|
|
96
|
+
};
|
|
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
|
+
export const parse = (schema) => {
|
|
124
|
+
try {
|
|
125
|
+
new SchemaParser(schema).parse();
|
|
126
|
+
return { schema };
|
|
127
|
+
}
|
|
128
|
+
catch (e) {
|
|
129
|
+
debug(schema);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { SchemaAnyProp } from '../types.js';
|
|
2
|
+
import type { SchemaParser } from './index.js';
|
|
3
|
+
type PropsFns<PropType> = Record<string, (val: any, prop: PropType, ctx: SchemaParser, key?: string) => void>;
|
|
4
|
+
declare function propParser<PropType extends SchemaAnyProp>(required: PropsFns<PropType>, optional: PropsFns<PropType>, allowShorthand?: number): (prop: any, ctx: SchemaParser) => void;
|
|
5
|
+
declare const p: Record<string, ReturnType<typeof propParser>>;
|
|
6
|
+
export default p;
|
|
7
|
+
//# sourceMappingURL=props.d.ts.map
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { expectBoolean, expectFunction, expectObject, expectString, expectNumber, } from './assert.js';
|
|
2
|
+
import { EXPECTED_ARR, EXPECTED_DATE, EXPECTED_OBJ, EXPECTED_PRIMITIVE, EXPECTED_VALUE_IN_ENUM, INVALID_VALUE, MIN_MAX, OUT_OF_RANGE, TEXT_REQUIRES_LOCALES, TYPE_MISMATCH, UNKNOWN_PROP, } from './errors.js';
|
|
3
|
+
import { getPropType } from './utils.js';
|
|
4
|
+
const STUB = {};
|
|
5
|
+
const shared = {
|
|
6
|
+
type() { },
|
|
7
|
+
required(val) {
|
|
8
|
+
expectBoolean(val);
|
|
9
|
+
},
|
|
10
|
+
query(val) {
|
|
11
|
+
expectFunction(val);
|
|
12
|
+
},
|
|
13
|
+
path(val, prop, ctx) {
|
|
14
|
+
expectString(val);
|
|
15
|
+
const path = val.split('.');
|
|
16
|
+
let t = ctx.type;
|
|
17
|
+
for (const key of path) {
|
|
18
|
+
if ('items' in t) {
|
|
19
|
+
t = t.items;
|
|
20
|
+
}
|
|
21
|
+
if ('ref' in t) {
|
|
22
|
+
t = ctx.schema.types[t.ref];
|
|
23
|
+
}
|
|
24
|
+
t = t.props[key];
|
|
25
|
+
expectObject(t);
|
|
26
|
+
}
|
|
27
|
+
if (t.type !== getPropType(prop)) {
|
|
28
|
+
throw Error(TYPE_MISMATCH);
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
function propParser(required, optional, allowShorthand) {
|
|
33
|
+
return (prop, ctx) => {
|
|
34
|
+
if (typeof prop === 'string') {
|
|
35
|
+
// allow string
|
|
36
|
+
if (allowShorthand === 0) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
throw Error(EXPECTED_OBJ);
|
|
40
|
+
}
|
|
41
|
+
if (Array.isArray(prop)) {
|
|
42
|
+
// allow array
|
|
43
|
+
if (allowShorthand === 1) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
throw Error(EXPECTED_OBJ);
|
|
47
|
+
}
|
|
48
|
+
for (const key in required) {
|
|
49
|
+
required[key](prop[key], prop, ctx);
|
|
50
|
+
}
|
|
51
|
+
for (const key in prop) {
|
|
52
|
+
const val = prop[key];
|
|
53
|
+
if (key in optional) {
|
|
54
|
+
optional[key](val, prop, ctx);
|
|
55
|
+
}
|
|
56
|
+
else if (key in shared) {
|
|
57
|
+
shared[key](val, prop, ctx);
|
|
58
|
+
}
|
|
59
|
+
else if (!(key in required)) {
|
|
60
|
+
if (key[0] === '$' && 'ref' in prop) {
|
|
61
|
+
optional.edge(val, prop, ctx, key);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
throw Error(UNKNOWN_PROP);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
const p = {};
|
|
71
|
+
p.alias = propParser(STUB, {
|
|
72
|
+
default(val) {
|
|
73
|
+
expectString(val);
|
|
74
|
+
},
|
|
75
|
+
}, 0);
|
|
76
|
+
p.boolean = propParser(STUB, {
|
|
77
|
+
default(val) {
|
|
78
|
+
expectBoolean(val);
|
|
79
|
+
},
|
|
80
|
+
}, 0);
|
|
81
|
+
p.enum = propParser({
|
|
82
|
+
enum(items) {
|
|
83
|
+
if (!Array.isArray(items)) {
|
|
84
|
+
throw Error(EXPECTED_ARR);
|
|
85
|
+
}
|
|
86
|
+
if (items.length > 255) {
|
|
87
|
+
throw Error('Max enum length (255) exceeded');
|
|
88
|
+
}
|
|
89
|
+
for (const item of items) {
|
|
90
|
+
if (typeof item === 'object') {
|
|
91
|
+
throw Error(EXPECTED_PRIMITIVE);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
}, {
|
|
96
|
+
default(val, prop) {
|
|
97
|
+
if (!prop.enum.includes(val)) {
|
|
98
|
+
throw Error(EXPECTED_VALUE_IN_ENUM);
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
}, 1);
|
|
102
|
+
const numberOpts = {
|
|
103
|
+
min(val) {
|
|
104
|
+
expectNumber(val);
|
|
105
|
+
},
|
|
106
|
+
max(val, prop) {
|
|
107
|
+
expectNumber(val);
|
|
108
|
+
if (prop.min > val) {
|
|
109
|
+
throw Error(MIN_MAX);
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
step(val) {
|
|
113
|
+
if (typeof val !== 'number' && val !== 'any') {
|
|
114
|
+
throw Error(INVALID_VALUE);
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
default(val, prop) {
|
|
118
|
+
expectNumber(val);
|
|
119
|
+
if (val > prop.max || val < prop.min) {
|
|
120
|
+
throw Error(OUT_OF_RANGE);
|
|
121
|
+
}
|
|
122
|
+
if (prop.step !== 'any') {
|
|
123
|
+
const min = typeof prop.min !== 'number' || prop.min === Infinity ? 0 : prop.min;
|
|
124
|
+
const v = val - min;
|
|
125
|
+
if (~~(v / prop.step) * prop.step !== v) {
|
|
126
|
+
throw Error(INVALID_VALUE);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
p.number = propParser(STUB, numberOpts, 0);
|
|
132
|
+
p.int8 = propParser(STUB, numberOpts, 0);
|
|
133
|
+
p.uint8 = propParser(STUB, numberOpts, 0);
|
|
134
|
+
p.int16 = propParser(STUB, numberOpts, 0);
|
|
135
|
+
p.uint16 = propParser(STUB, numberOpts, 0);
|
|
136
|
+
p.int32 = propParser(STUB, numberOpts, 0);
|
|
137
|
+
p.uint32 = propParser(STUB, numberOpts, 0);
|
|
138
|
+
p.object = propParser({
|
|
139
|
+
props(val, prop, ctx) {
|
|
140
|
+
ctx.parseProps(val, ctx.type);
|
|
141
|
+
},
|
|
142
|
+
}, {
|
|
143
|
+
default(val) {
|
|
144
|
+
console.warn('TODO object default value');
|
|
145
|
+
},
|
|
146
|
+
});
|
|
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
|
+
p.set = propParser({
|
|
205
|
+
items(items, prop, ctx) {
|
|
206
|
+
expectObject(items);
|
|
207
|
+
const itemsType = getPropType(items);
|
|
208
|
+
if (itemsType === 'string' ||
|
|
209
|
+
itemsType === 'number' ||
|
|
210
|
+
itemsType === 'timestamp' ||
|
|
211
|
+
itemsType === 'boolean') {
|
|
212
|
+
ctx.inQuery = 'query' in prop;
|
|
213
|
+
p[itemsType](items, ctx);
|
|
214
|
+
ctx.inQuery = false;
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
throw new Error(INVALID_VALUE);
|
|
218
|
+
}
|
|
219
|
+
},
|
|
220
|
+
}, {
|
|
221
|
+
default(val, prop) {
|
|
222
|
+
console.warn('TODO SET DEFAULT VALUE');
|
|
223
|
+
// if (typeof val === 'object') {
|
|
224
|
+
// throwErr(ERRORS.EXPECTED_PRIMITIVE, prop, 'default')
|
|
225
|
+
// }
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
p.references = propParser({
|
|
229
|
+
items(items, prop, ctx) {
|
|
230
|
+
expectObject(items);
|
|
231
|
+
const itemsType = getPropType(items);
|
|
232
|
+
if (itemsType === 'reference') {
|
|
233
|
+
ctx.inQuery = 'query' in prop;
|
|
234
|
+
p[itemsType](items, ctx);
|
|
235
|
+
ctx.inQuery = false;
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
throw new Error(INVALID_VALUE);
|
|
239
|
+
}
|
|
240
|
+
},
|
|
241
|
+
}, {
|
|
242
|
+
default(val, prop) {
|
|
243
|
+
console.warn('TODO SET DEFAULT VALUE');
|
|
244
|
+
// if (typeof val === 'object') {
|
|
245
|
+
// throwErr(ERRORS.EXPECTED_PRIMITIVE, prop, 'default')
|
|
246
|
+
// }
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
p.string = propParser(STUB, {
|
|
250
|
+
default(val) {
|
|
251
|
+
expectString(val);
|
|
252
|
+
},
|
|
253
|
+
maxBytes(val) {
|
|
254
|
+
expectNumber(val);
|
|
255
|
+
},
|
|
256
|
+
min(val) {
|
|
257
|
+
expectNumber(val);
|
|
258
|
+
},
|
|
259
|
+
max(val) {
|
|
260
|
+
expectNumber(val);
|
|
261
|
+
},
|
|
262
|
+
}, 0);
|
|
263
|
+
p.text = propParser({
|
|
264
|
+
type(_val, _prop, { schema }) {
|
|
265
|
+
if (schema.locales) {
|
|
266
|
+
for (const _ in schema.locales) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
throw Error(TEXT_REQUIRES_LOCALES);
|
|
271
|
+
},
|
|
272
|
+
}, {
|
|
273
|
+
default(val, prop) {
|
|
274
|
+
console.warn('MAKE DEFAULT VALUE FOR TEXT');
|
|
275
|
+
},
|
|
276
|
+
}, 0);
|
|
277
|
+
p.timestamp = propParser(STUB, {
|
|
278
|
+
default(val) {
|
|
279
|
+
if (typeof val !== 'number' && !(val instanceof Date)) {
|
|
280
|
+
throw Error(EXPECTED_DATE);
|
|
281
|
+
}
|
|
282
|
+
},
|
|
283
|
+
on(val) {
|
|
284
|
+
if (val !== 'create' && val !== 'update') {
|
|
285
|
+
throw Error(INVALID_VALUE);
|
|
286
|
+
}
|
|
287
|
+
},
|
|
288
|
+
}, 0);
|
|
289
|
+
export default p;
|
|
290
|
+
//# sourceMappingURL=props.js.map
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { INVALID_TYPE, MISSING_TYPE } from './errors.js';
|
|
2
|
+
export const getPropType = (prop) => {
|
|
3
|
+
if (typeof prop === 'string') {
|
|
4
|
+
return prop;
|
|
5
|
+
}
|
|
6
|
+
if ('type' in prop) {
|
|
7
|
+
if (typeof prop.type !== 'string') {
|
|
8
|
+
throw Error(INVALID_TYPE);
|
|
9
|
+
}
|
|
10
|
+
return prop.type;
|
|
11
|
+
}
|
|
12
|
+
if ('ref' in prop) {
|
|
13
|
+
return 'reference';
|
|
14
|
+
}
|
|
15
|
+
if ('items' in prop) {
|
|
16
|
+
if (getPropType(prop.items) === 'reference') {
|
|
17
|
+
return 'references';
|
|
18
|
+
}
|
|
19
|
+
return 'set';
|
|
20
|
+
}
|
|
21
|
+
if ('props' in prop) {
|
|
22
|
+
return 'object';
|
|
23
|
+
}
|
|
24
|
+
if ('enum' in prop || Array.isArray(prop)) {
|
|
25
|
+
return 'enum';
|
|
26
|
+
}
|
|
27
|
+
throw Error(MISSING_TYPE);
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare const expectObject: (obj: any) => void;
|
|
2
|
+
export declare const expectString: (obj: any) => void;
|
|
3
|
+
export declare const expectBoolean: (v: any) => void;
|
|
4
|
+
export declare const expectFunction: (v: any) => void;
|
|
5
|
+
export declare const expectNumber: (v: any) => void;
|
|
6
|
+
//# sourceMappingURL=assert.d.ts.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { EXPECTED_BOOL, EXPECTED_FN, EXPECTED_NUM, EXPECTED_OBJ, EXPECTED_STR, } from './errors.js';
|
|
2
|
+
export const expectObject = (obj) => {
|
|
3
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
4
|
+
throw Error(EXPECTED_OBJ);
|
|
5
|
+
}
|
|
6
|
+
};
|
|
7
|
+
export const expectString = (obj) => {
|
|
8
|
+
if (typeof obj !== 'string') {
|
|
9
|
+
throw Error(EXPECTED_STR);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
export const expectBoolean = (v) => {
|
|
13
|
+
if (v !== true && v !== false) {
|
|
14
|
+
throw Error(EXPECTED_BOOL);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
export const expectFunction = (v) => {
|
|
18
|
+
if (typeof v !== 'function') {
|
|
19
|
+
throw Error(EXPECTED_FN);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
export const expectNumber = (v) => {
|
|
23
|
+
if (typeof v !== 'number') {
|
|
24
|
+
throw Error(EXPECTED_NUM);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=assert.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare const OUT_OF_RANGE = "Value is out of range";
|
|
2
|
+
export declare const EXPECTED_VALUE_IN_ENUM = "Expected value in \"enum\" field";
|
|
3
|
+
export declare const EXPECTED_DATE = "Expected number or Date";
|
|
4
|
+
export declare const EXPECTED_BOOL = "Expected boolean";
|
|
5
|
+
export declare const EXPECTED_OBJ = "Expected object";
|
|
6
|
+
export declare const EXPECTED_ARR = "Expected array";
|
|
7
|
+
export declare const EXPECTED_NUM = "Expected number";
|
|
8
|
+
export declare const EXPECTED_STR = "Expected string";
|
|
9
|
+
export declare const EXPECTED_FN = "Expected function";
|
|
10
|
+
export declare const EXPECTED_PRIMITIVE = "Expected primitive value";
|
|
11
|
+
export declare const INVALID_VALUE = "Invalid value";
|
|
12
|
+
export declare const INVALID_SCHEMA = "Invalid schema";
|
|
13
|
+
export declare const MIN_MAX = "Max value must be larger than min value";
|
|
14
|
+
export declare const UNKNOWN_PROP = "Unknown property";
|
|
15
|
+
export declare const MISSING_TYPE = "Missing type";
|
|
16
|
+
export declare const INVALID_TYPE = "Invalid type";
|
|
17
|
+
export declare const TEXT_REQUIRES_LOCALES = "Type text requires locales";
|
|
18
|
+
export declare const TYPE_MISMATCH = "Types do not match";
|
|
19
|
+
//# sourceMappingURL=errors.d.ts.map
|