@based/schema 3.1.0 → 3.2.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/README.md +1 -20
- package/dist/display/index.d.ts +2 -0
- package/dist/display/index.js +26 -0
- package/dist/display/number.d.ts +3 -0
- package/dist/display/number.js +89 -0
- package/dist/display/string.d.ts +3 -0
- package/dist/display/string.js +23 -0
- package/dist/display/timestamp.d.ts +3 -0
- package/dist/display/timestamp.js +127 -0
- package/dist/error.d.ts +19 -0
- package/dist/error.js +24 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +22 -0
- package/dist/languages.d.ts +187 -0
- package/dist/languages.js +190 -0
- package/dist/set/fields/array.d.ts +2 -0
- package/dist/set/fields/array.js +123 -0
- package/dist/set/fields/index.d.ts +3 -0
- package/dist/set/fields/index.js +74 -0
- package/dist/set/fields/number.d.ts +4 -0
- package/dist/set/fields/number.js +129 -0
- package/dist/set/fields/object.d.ts +3 -0
- package/dist/set/fields/object.js +33 -0
- package/dist/set/fields/references.d.ts +3 -0
- package/dist/set/fields/references.js +128 -0
- package/dist/set/fields/set.d.ts +2 -0
- package/dist/set/fields/set.js +63 -0
- package/dist/set/fields/string.d.ts +3 -0
- package/dist/set/fields/string.js +284 -0
- package/dist/set/index.d.ts +3 -0
- package/dist/set/index.js +183 -0
- package/dist/set/isValidId.d.ts +2 -0
- package/dist/set/isValidId.js +21 -0
- package/dist/set/types.d.ts +0 -0
- package/dist/set/types.js +1 -0
- package/dist/src/compat/index.js +10 -2
- package/dist/src/compat/newToOld.d.ts +2 -2
- package/dist/src/compat/newToOld.js +173 -31
- package/dist/src/compat/oldToNew.d.ts +1 -1
- package/dist/src/compat/oldToNew.js +200 -25
- package/dist/src/generateQuery.d.ts +12 -0
- package/dist/src/generateQuery.js +75 -0
- package/dist/src/set/fields/references.js +40 -0
- package/dist/test/compat.js +25 -0
- package/dist/test/query.d.ts +1 -0
- package/dist/test/query.js +93 -0
- package/dist/types.d.ts +205 -0
- package/dist/types.js +27 -0
- package/dist/updateSchema.d.ts +2 -0
- package/dist/updateSchema.js +16 -0
- package/dist/validateSchema.d.ts +4 -0
- package/dist/validateSchema.js +41 -0
- package/dist/walker/args.d.ts +36 -0
- package/dist/walker/args.js +162 -0
- package/dist/walker/index.d.ts +6 -0
- package/dist/walker/index.js +49 -0
- package/dist/walker/parse.d.ts +3 -0
- package/dist/walker/parse.js +186 -0
- package/dist/walker/types.d.ts +45 -0
- package/dist/walker/types.js +10 -0
- package/package.json +1 -1
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parse = void 0;
|
|
4
|
+
const args_1 = require("./args");
|
|
5
|
+
const types_1 = require("./types");
|
|
6
|
+
// TODO needs cleaning
|
|
7
|
+
function createOrUseArgs(from, newArgs) {
|
|
8
|
+
if (!newArgs) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
if (newArgs instanceof args_1.ArgsClass) {
|
|
12
|
+
return newArgs;
|
|
13
|
+
}
|
|
14
|
+
const x = from.create(newArgs);
|
|
15
|
+
// x.collectedCommands = from.collectedCommands
|
|
16
|
+
// x.fromBackTrack = from.fromBackTrack
|
|
17
|
+
return x;
|
|
18
|
+
}
|
|
19
|
+
async function parseKey(from, key, parser) {
|
|
20
|
+
const keyArgs = new args_1.ArgsClass({
|
|
21
|
+
key,
|
|
22
|
+
value: from.value[key],
|
|
23
|
+
fieldSchema: from.fieldSchema,
|
|
24
|
+
}, from);
|
|
25
|
+
// if same
|
|
26
|
+
const newArgs = createOrUseArgs(keyArgs, await parser(keyArgs));
|
|
27
|
+
if (newArgs) {
|
|
28
|
+
return newArgs.parse();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function createFieldArgs(from, key, fieldSchema) {
|
|
32
|
+
return new args_1.ArgsClass({
|
|
33
|
+
key,
|
|
34
|
+
value: from.value[key],
|
|
35
|
+
// @ts-ignore needs key
|
|
36
|
+
fieldSchema,
|
|
37
|
+
}, from);
|
|
38
|
+
}
|
|
39
|
+
function getFieldParser(args) {
|
|
40
|
+
const fieldParser = 'enum' in args.fieldSchema
|
|
41
|
+
? args.root._opts.parsers.fields.enum
|
|
42
|
+
: args.root._opts.parsers.fields[args.fieldSchema.type];
|
|
43
|
+
return fieldParser;
|
|
44
|
+
}
|
|
45
|
+
async function parse(args) {
|
|
46
|
+
const opts = args.root._opts;
|
|
47
|
+
if (args.parseTopLevel) {
|
|
48
|
+
const parser = opts.parsers.any;
|
|
49
|
+
if (parser) {
|
|
50
|
+
const nArgs = await parser(args);
|
|
51
|
+
if (nArgs) {
|
|
52
|
+
// @ts-ignore
|
|
53
|
+
return parse(createOrUseArgs(args, nArgs));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (typeof args.value === 'object' && args.value !== null) {
|
|
58
|
+
const keyQ = [];
|
|
59
|
+
const keysHandled = new Set();
|
|
60
|
+
let allKeysHandled = false;
|
|
61
|
+
for (const key in opts.parsers.keys) {
|
|
62
|
+
if (key in args.value) {
|
|
63
|
+
keysHandled.add(key);
|
|
64
|
+
keyQ.push(parseKey(args, key, opts.parsers.keys[key]));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
await Promise.all(keyQ);
|
|
68
|
+
if (typeof args.value !== 'object' || args.value === null) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
// schema
|
|
72
|
+
if (args.stopped === undefined) {
|
|
73
|
+
const fieldQ = [];
|
|
74
|
+
if (args.typeSchema && !args.fieldSchema) {
|
|
75
|
+
for (const key in args.typeSchema.fields) {
|
|
76
|
+
const fieldSchema = args.typeSchema.fields[key];
|
|
77
|
+
if (key in args.value) {
|
|
78
|
+
keysHandled.add(key);
|
|
79
|
+
fieldQ.push(createFieldArgs(args, key, fieldSchema).parse());
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else if (args.fieldSchema && !args.stopped) {
|
|
84
|
+
if (args.fieldSchema.type === 'object') {
|
|
85
|
+
// @ts-ignore should detect from line above
|
|
86
|
+
const objFieldSchema = args.fieldSchema;
|
|
87
|
+
for (const key in objFieldSchema.properties) {
|
|
88
|
+
const fieldSchema = objFieldSchema.properties[key];
|
|
89
|
+
if (key in args.value) {
|
|
90
|
+
keysHandled.add(key);
|
|
91
|
+
fieldQ.push(createFieldArgs(args, key, fieldSchema).parse());
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else if (args.fieldSchema.type === 'record') {
|
|
96
|
+
// @ts-ignore should detect from line above
|
|
97
|
+
const objFieldSchema = args.fieldSchema;
|
|
98
|
+
for (const key in args.value) {
|
|
99
|
+
const fieldSchema = objFieldSchema.values;
|
|
100
|
+
keysHandled.add(key);
|
|
101
|
+
fieldQ.push(createFieldArgs(args, key, fieldSchema).parse());
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
else if (args.fieldSchema) {
|
|
105
|
+
const fieldParser = getFieldParser(args);
|
|
106
|
+
if (fieldParser) {
|
|
107
|
+
const newArgs = createOrUseArgs(args, await fieldParser(args));
|
|
108
|
+
if (newArgs) {
|
|
109
|
+
return newArgs.parse();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
await Promise.all(fieldQ);
|
|
115
|
+
if (args.fieldSchema &&
|
|
116
|
+
fieldQ.length > 0 &&
|
|
117
|
+
(args.fieldSchema.type === 'object' ||
|
|
118
|
+
args.fieldSchema.type === 'record')) {
|
|
119
|
+
const fieldParser = getFieldParser(args);
|
|
120
|
+
if (fieldParser) {
|
|
121
|
+
fieldParser(args);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (typeof args.value !== 'object' || args.value === null) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
// any
|
|
129
|
+
if (args.stopped !== types_1.Stopped.stopAll) {
|
|
130
|
+
const parser = opts.parsers.any || opts.parsers.catch;
|
|
131
|
+
if (parser) {
|
|
132
|
+
const q = [];
|
|
133
|
+
if (Array.isArray(args.value)) {
|
|
134
|
+
for (let i = 0; i < args.value.length; i++) {
|
|
135
|
+
if ((!opts.parsers.any && keysHandled.has(i)) || allKeysHandled) {
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
q.push(parseKey(args, i, parser));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
for (const key in args.value) {
|
|
143
|
+
if ((!opts.parsers.any && keysHandled.has(key)) || allKeysHandled) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
q.push(parseKey(args, key, parser));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
await Promise.all(q);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (opts.backtrack &&
|
|
153
|
+
!args.skipCollection &&
|
|
154
|
+
(args.fromBackTrack.length || args.collectedCommands.length)) {
|
|
155
|
+
const backtracked = opts.backtrack(args, args.fromBackTrack ?? [], args.collectedCommands ?? []);
|
|
156
|
+
if (backtracked) {
|
|
157
|
+
const target = args.getBackTrackTarget();
|
|
158
|
+
if (!target.fromBackTrack) {
|
|
159
|
+
target.fromBackTrack = [];
|
|
160
|
+
}
|
|
161
|
+
target.fromBackTrack.push(backtracked);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
// more
|
|
167
|
+
if (args.fieldSchema) {
|
|
168
|
+
const fieldParser = getFieldParser(args);
|
|
169
|
+
if (fieldParser) {
|
|
170
|
+
const newArgs = createOrUseArgs(args, await fieldParser(args));
|
|
171
|
+
if (newArgs) {
|
|
172
|
+
return newArgs.parse();
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
console.warn('fieldSchema type not implemented yet!', args.fieldSchema);
|
|
177
|
+
const anyParser = opts.parsers.any || opts.parsers.catch;
|
|
178
|
+
anyParser(args);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
exports.parse = parse;
|
|
186
|
+
//# sourceMappingURL=parse.js.map
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { ParseError } from '../error';
|
|
2
|
+
import { BasedSchemaType, BasedSchemaFields, BasedSchemaField, BasedSchema } from '../types';
|
|
3
|
+
import { ArgsClass } from './args';
|
|
4
|
+
export type Path = (string | number)[];
|
|
5
|
+
export type ErrorHandler<T> = (code: ParseError, args: ArgsClass<T> | ArgsOpts<T>) => void;
|
|
6
|
+
export type Collect<T> = (args: ArgsClass<T>) => any;
|
|
7
|
+
export type FieldParser<K extends keyof BasedSchemaFields, T = any> = (args: ArgsClass<T, K>) => Promise<ArgsClass<T> | ArgsOpts<T> | void>;
|
|
8
|
+
export type KeyParser<T = any> = (args: ArgsClass<T, keyof BasedSchemaFields>) => Promise<ArgsOpts<T> | ArgsClass<T> | void>;
|
|
9
|
+
export type FieldParsers<T = any> = {
|
|
10
|
+
[Key in keyof BasedSchemaFields]: FieldParser<Key, T>;
|
|
11
|
+
};
|
|
12
|
+
export type AsyncOperation<T> = (args: ArgsClass<T>, type?: string) => Promise<any>;
|
|
13
|
+
export type Opts<T> = {
|
|
14
|
+
init: (value: any, schema: BasedSchema, error: ErrorHandler<T>) => Promise<ArgsOpts<T>>;
|
|
15
|
+
parsers: {
|
|
16
|
+
fields: Partial<{
|
|
17
|
+
[Key in keyof BasedSchemaFields]: FieldParser<Key, T>;
|
|
18
|
+
}>;
|
|
19
|
+
keys: {
|
|
20
|
+
[key: string]: KeyParser<T>;
|
|
21
|
+
};
|
|
22
|
+
any?: KeyParser<T>;
|
|
23
|
+
catch?: KeyParser<T>;
|
|
24
|
+
};
|
|
25
|
+
collect?: (args: ArgsClass<T>) => any;
|
|
26
|
+
error?: ErrorHandler<T>;
|
|
27
|
+
backtrack?: (args: ArgsClass<T>, fromBackTrack: any[], collectedCommands: any[]) => any;
|
|
28
|
+
asyncOperationHandler?: AsyncOperation<T>;
|
|
29
|
+
};
|
|
30
|
+
export declare enum Stopped {
|
|
31
|
+
onlyStopFieldParser = 0,
|
|
32
|
+
stopAll = 1
|
|
33
|
+
}
|
|
34
|
+
export type ArgsOpts<T, K extends keyof BasedSchemaFields = keyof BasedSchemaFields> = {
|
|
35
|
+
parseTopLevel?: boolean;
|
|
36
|
+
target?: T;
|
|
37
|
+
key?: string | number;
|
|
38
|
+
path?: Path;
|
|
39
|
+
value?: any;
|
|
40
|
+
prev?: ArgsClass<T, K>;
|
|
41
|
+
fieldSchema?: BasedSchemaField;
|
|
42
|
+
typeSchema?: BasedSchemaType;
|
|
43
|
+
skipCollection?: boolean;
|
|
44
|
+
collect?: (args: ArgsClass<T, K>, value?: any) => any;
|
|
45
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Stopped = void 0;
|
|
4
|
+
var Stopped;
|
|
5
|
+
(function (Stopped) {
|
|
6
|
+
Stopped[Stopped["onlyStopFieldParser"] = 0] = "onlyStopFieldParser";
|
|
7
|
+
Stopped[Stopped["stopAll"] = 1] = "stopAll";
|
|
8
|
+
})(Stopped || (exports.Stopped = Stopped = {}));
|
|
9
|
+
// Add asyncOperations // requiresAsyncValidation -> asyncOperation: () => {}
|
|
10
|
+
//# sourceMappingURL=types.js.map
|