@based/schema 0.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.
- package/README.md +21 -0
- package/dist/deepPartial.d.ts +0 -0
- package/dist/deepPartial.js +3 -0
- package/dist/deepPartial.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/languages.d.ts +187 -0
- package/dist/languages.js +190 -0
- package/dist/languages.js.map +1 -0
- package/dist/schema.d.ts +1 -0
- package/dist/schema.js +3 -0
- package/dist/schema.js.map +1 -0
- package/dist/set/enum.d.ts +2 -0
- package/dist/set/enum.js +15 -0
- package/dist/set/enum.js.map +1 -0
- package/dist/set/fieldValidator.d.ts +6 -0
- package/dist/set/fieldValidator.js +144 -0
- package/dist/set/fieldValidator.js.map +1 -0
- package/dist/set/handleError.d.ts +1 -0
- package/dist/set/handleError.js +9 -0
- package/dist/set/handleError.js.map +1 -0
- package/dist/set/index.d.ts +5 -0
- package/dist/set/index.js +93 -0
- package/dist/set/index.js.map +1 -0
- package/dist/set/parsers.d.ts +6 -0
- package/dist/set/parsers.js +287 -0
- package/dist/set/parsers.js.map +1 -0
- package/dist/setWalker.d.ts +11 -0
- package/dist/setWalker.js +189 -0
- package/dist/setWalker.js.map +1 -0
- package/dist/transformers.d.ts +3 -0
- package/dist/transformers.js +18 -0
- package/dist/transformers.js.map +1 -0
- package/dist/typeWalker.d.ts +3 -0
- package/dist/typeWalker.js +18 -0
- package/dist/typeWalker.js.map +1 -0
- package/dist/types.d.ts +163 -0
- package/dist/types.js +8 -0
- package/dist/types.js.map +1 -0
- package/dist/validate.d.ts +4 -0
- package/dist/validate.js +34 -0
- package/dist/validate.js.map +1 -0
- package/dist/validateFields.d.ts +4 -0
- package/dist/validateFields.js +34 -0
- package/dist/validateFields.js.map +1 -0
- package/dist/validateSchema copy.d.ts +4 -0
- package/dist/validateSchema copy.js +34 -0
- package/dist/validateSchema copy.js.map +1 -0
- package/dist/validateSchema.d.ts +4 -0
- package/dist/validateSchema.js +34 -0
- package/dist/validateSchema.js.map +1 -0
- package/package.json +35 -0
- package/src/index.ts +5 -0
- package/src/languages.ts +188 -0
- package/src/set/handleError.ts +15 -0
- package/src/set/index.ts +135 -0
- package/src/set/parsers.ts +448 -0
- package/src/types.ts +277 -0
- package/src/validateSchema.ts +56 -0
- package/test/setWalker.ts +197 -0
- package/test/validateSchema.ts +42 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.setWalker = exports.fieldWalker = void 0;
|
|
7
|
+
const handleError_1 = require("./handleError");
|
|
8
|
+
const parsers_1 = __importDefault(require("./parsers"));
|
|
9
|
+
// Collect is a pretty good place for checking if a user is allowed to set something
|
|
10
|
+
// also make collect async
|
|
11
|
+
// add extra function for loading required
|
|
12
|
+
const fieldWalker = async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
13
|
+
if ('$ref' in fieldSchema) {
|
|
14
|
+
// TODO: when we have this it has to get it from the schema and redo the parsing with the correct fieldSchema
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const valueType = typeof value;
|
|
18
|
+
const valueIsObject = value && valueType === 'object';
|
|
19
|
+
if (valueIsObject && value.$delete === true) {
|
|
20
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target });
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const typeDef = 'type' in fieldSchema
|
|
24
|
+
? fieldSchema.type
|
|
25
|
+
: 'enum' in fieldSchema
|
|
26
|
+
? 'enum'
|
|
27
|
+
: '';
|
|
28
|
+
if (!typeDef) {
|
|
29
|
+
throw (0, handleError_1.createError)(path, target.type, typeDef, path[path.length - 1]);
|
|
30
|
+
}
|
|
31
|
+
if ('customValidator' in fieldSchema) {
|
|
32
|
+
const customValidator = fieldSchema.customValidator;
|
|
33
|
+
if (!(await customValidator(value, path, target))) {
|
|
34
|
+
throw (0, handleError_1.createError)(path, target.type, typeDef, value);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const parse = parsers_1.default[typeDef];
|
|
38
|
+
await parse(path, value, fieldSchema, typeSchema, target, handlers);
|
|
39
|
+
return;
|
|
40
|
+
};
|
|
41
|
+
exports.fieldWalker = fieldWalker;
|
|
42
|
+
const setWalker = async (schema, value, handlers) => {
|
|
43
|
+
let type;
|
|
44
|
+
if (value.$id) {
|
|
45
|
+
type = schema.prefixToTypeMapping[value.$id.slice(0, 2)];
|
|
46
|
+
if (!type) {
|
|
47
|
+
throw new Error(`Cannot find type for $id ${value.$id}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (value.type) {
|
|
51
|
+
if (type && value.type !== type) {
|
|
52
|
+
throw new Error(`type from "$id" ${value.$id} does not match "type" field ${value.type}`);
|
|
53
|
+
}
|
|
54
|
+
type = value.type;
|
|
55
|
+
}
|
|
56
|
+
const schemaType = schema.types[type];
|
|
57
|
+
if (!schemaType) {
|
|
58
|
+
throw new Error(`Cannot find schema definition for type ${type}`);
|
|
59
|
+
}
|
|
60
|
+
const target = {
|
|
61
|
+
type,
|
|
62
|
+
schema,
|
|
63
|
+
};
|
|
64
|
+
if (value.$id) {
|
|
65
|
+
target.$id = value.$id;
|
|
66
|
+
}
|
|
67
|
+
else if (value.$alias) {
|
|
68
|
+
target.$alias = value.$alias;
|
|
69
|
+
}
|
|
70
|
+
const q = [];
|
|
71
|
+
for (const key in value) {
|
|
72
|
+
if (key[0] === '$') {
|
|
73
|
+
console.info('key is operator', key);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
const fieldSchema = schemaType.fields[key];
|
|
77
|
+
if (!fieldSchema) {
|
|
78
|
+
throw new Error(`Field does not exist in schema "${key}" on type "${type}"`);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
q.push((0, exports.fieldWalker)([key], value[key], fieldSchema, schemaType, target, handlers));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
await Promise.all(q);
|
|
86
|
+
// required fields (collect them!)
|
|
87
|
+
// if (!(await handlers.requiredFields(value, [], target))) {
|
|
88
|
+
// throw new Error('Missing required fields')
|
|
89
|
+
// }
|
|
90
|
+
return target;
|
|
91
|
+
};
|
|
92
|
+
exports.setWalker = setWalker;
|
|
93
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/set/index.ts"],"names":[],"mappings":";;;;;;AAOA,+CAA2C;AAC3C,wDAA+B;AAE/B,oFAAoF;AACpF,0BAA0B;AAE1B,0CAA0C;AAEnC,MAAM,WAAW,GAAG,KAAK,EAC9B,IAAyB,EACzB,KAAU,EACV,WAA6B,EAC7B,UAA2B,EAC3B,MAAsB,EACtB,QAA0B,EACX,EAAE;IACjB,IAAI,MAAM,IAAI,WAAW,EAAE;QACzB,6GAA6G;QAC7G,OAAM;KACP;IACD,MAAM,SAAS,GAAG,OAAO,KAAK,CAAA;IAE9B,MAAM,aAAa,GAAG,KAAK,IAAI,SAAS,KAAK,QAAQ,CAAA;IACrD,IAAI,aAAa,IAAI,KAAK,CAAC,OAAO,KAAK,IAAI,EAAE;QAC3C,QAAQ,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,EAAE,CAAC,CAAA;QAClE,OAAM;KACP;IAED,MAAM,OAAO,GACX,MAAM,IAAI,WAAW;QACnB,CAAC,CAAC,WAAW,CAAC,IAAI;QAClB,CAAC,CAAC,MAAM,IAAI,WAAW;YACvB,CAAC,CAAC,MAAM;YACR,CAAC,CAAC,EAAE,CAAA;IAER,IAAI,CAAC,OAAO,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
|
+
import { BasedSchemaField, BasedSchemaType, BasedSetHandlers, BasedSetTarget } from '../types';
|
|
2
|
+
type Parser = (path: (string | number)[], value: any, fieldSchema: BasedSchemaField, typeSchema: BasedSchemaType, target: BasedSetTarget, handlers: BasedSetHandlers) => Promise<void>;
|
|
3
|
+
declare const parsers: {
|
|
4
|
+
[key: string]: Parser;
|
|
5
|
+
};
|
|
6
|
+
export default parsers;
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const utils_1 = require("@saulx/utils");
|
|
4
|
+
const handleError_1 = require("./handleError");
|
|
5
|
+
const _1 = require(".");
|
|
6
|
+
const reference = async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
7
|
+
// $no root
|
|
8
|
+
// prob pass these as options
|
|
9
|
+
// value .default
|
|
10
|
+
// $value
|
|
11
|
+
if (typeof value !== 'string') {
|
|
12
|
+
throw (0, handleError_1.createError)(path, target.type, 'reference', value);
|
|
13
|
+
}
|
|
14
|
+
if ('allowedTypes' in fieldSchema) {
|
|
15
|
+
const prefix = value.slice(0, 2);
|
|
16
|
+
const targetType = target.schema.prefixToTypeMapping[prefix];
|
|
17
|
+
if (!targetType) {
|
|
18
|
+
throw (0, handleError_1.createError)(path, target.type, 'reference', value, '', `${prefix} does not exist in database`);
|
|
19
|
+
}
|
|
20
|
+
let typeMatches = false;
|
|
21
|
+
for (const t of fieldSchema.allowedTypes) {
|
|
22
|
+
if (typeof t === 'string') {
|
|
23
|
+
if (t === targetType) {
|
|
24
|
+
typeMatches = true;
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
if (t.type && t.type === targetType) {
|
|
30
|
+
typeMatches = true;
|
|
31
|
+
if (t.$filter) {
|
|
32
|
+
if (!(await handlers.referenceFilterCondition(value, t.$filter))) {
|
|
33
|
+
throw (0, handleError_1.createError)(path, target.type, 'reference', value, '', `${targetType} does not match allowedReferences`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else if (!t.type && t.$filter) {
|
|
38
|
+
if (!(await handlers.referenceFilterCondition(value, t.$filter))) {
|
|
39
|
+
throw (0, handleError_1.createError)(path, target.type, 'reference', value, '', `${targetType} does not match allowedReferences`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (typeMatches === false) {
|
|
45
|
+
throw (0, handleError_1.createError)(path, target.type, 'reference', value, '', `${targetType} does not match allowedReferences`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target });
|
|
49
|
+
};
|
|
50
|
+
const parsers = {
|
|
51
|
+
enum: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
52
|
+
// value .default
|
|
53
|
+
// @ts-ignore
|
|
54
|
+
const enumValues = fieldSchema.enum;
|
|
55
|
+
for (let i = 0; i < enumValues.length; i++) {
|
|
56
|
+
if ((0, utils_1.deepEqual)(enumValues[i], value)) {
|
|
57
|
+
handlers.collect({ path, value: i, typeSchema, fieldSchema, target });
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
throw (0, handleError_1.createError)(path, target.type, 'enum', value);
|
|
62
|
+
},
|
|
63
|
+
array: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
64
|
+
// value .default
|
|
65
|
+
// TODO: ADD ARRAY OPERATORS
|
|
66
|
+
const isArray = Array.isArray(value);
|
|
67
|
+
if (typeof value === 'object' && !isArray) {
|
|
68
|
+
const q = [];
|
|
69
|
+
if (value.$insert) {
|
|
70
|
+
}
|
|
71
|
+
if (value.$remove) {
|
|
72
|
+
}
|
|
73
|
+
if (value.$push) {
|
|
74
|
+
}
|
|
75
|
+
if (value.$assign) {
|
|
76
|
+
}
|
|
77
|
+
// value.$value :/
|
|
78
|
+
// fix
|
|
79
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target });
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (!isArray) {
|
|
83
|
+
throw (0, handleError_1.createError)(path, target.type, 'array', value);
|
|
84
|
+
}
|
|
85
|
+
const q = [];
|
|
86
|
+
for (let i = 0; i < value.length; i++) {
|
|
87
|
+
q.push((0, _1.fieldWalker)([...path, i], value[i],
|
|
88
|
+
// @ts-ignore
|
|
89
|
+
fieldSchema.values, typeSchema, target, handlers));
|
|
90
|
+
}
|
|
91
|
+
await Promise.all(q);
|
|
92
|
+
},
|
|
93
|
+
object: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
94
|
+
// value .default
|
|
95
|
+
if (typeof value !== 'object') {
|
|
96
|
+
throw (0, handleError_1.createError)(path, target.type, 'object', value);
|
|
97
|
+
}
|
|
98
|
+
const isArray = Array.isArray(value);
|
|
99
|
+
if (isArray) {
|
|
100
|
+
throw (0, handleError_1.createError)(path, target.type, 'object', value);
|
|
101
|
+
}
|
|
102
|
+
const q = [];
|
|
103
|
+
for (const key in value) {
|
|
104
|
+
// @ts-ignore
|
|
105
|
+
const propDef = fieldSchema.properties[key];
|
|
106
|
+
if (!propDef) {
|
|
107
|
+
throw (0, handleError_1.createError)([...path, key], target.type, 'object', value[key], key);
|
|
108
|
+
}
|
|
109
|
+
q.push((0, _1.fieldWalker)([...path, key], value[key], propDef, typeSchema, target, handlers));
|
|
110
|
+
}
|
|
111
|
+
await Promise.all(q);
|
|
112
|
+
},
|
|
113
|
+
set: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
114
|
+
// value .default
|
|
115
|
+
const q = [];
|
|
116
|
+
// @ts-ignore
|
|
117
|
+
const fieldDef = fieldSchema.items;
|
|
118
|
+
if (Array.isArray(value)) {
|
|
119
|
+
const handlerNest = {
|
|
120
|
+
...handlers,
|
|
121
|
+
collect: ({ path, value }) => {
|
|
122
|
+
parsedArray.push(value);
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
const parsedArray = [];
|
|
126
|
+
for (let i = 0; i < value.length; i++) {
|
|
127
|
+
q.push((0, _1.fieldWalker)([...path, i], value[i], fieldDef, typeSchema, target, handlerNest));
|
|
128
|
+
}
|
|
129
|
+
await Promise.all(q);
|
|
130
|
+
handlers.collect({
|
|
131
|
+
path,
|
|
132
|
+
value: parsedArray,
|
|
133
|
+
typeSchema,
|
|
134
|
+
fieldSchema,
|
|
135
|
+
target,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
const handlerNest = {
|
|
140
|
+
...handlers,
|
|
141
|
+
collect: ({ path, value }) => { },
|
|
142
|
+
};
|
|
143
|
+
if (value.$add) {
|
|
144
|
+
for (let i = 0; i < value.$add.length; i++) {
|
|
145
|
+
q.push((0, _1.fieldWalker)([...path, '$add', i], value.$add[i], fieldDef, typeSchema, target, handlerNest));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (value.$delete) {
|
|
149
|
+
for (let i = 0; i < value.$add.length; i++) {
|
|
150
|
+
q.push((0, _1.fieldWalker)([...path, '$delete', i], value.$delete[i], fieldDef, typeSchema, target, handlerNest));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
await Promise.all(q);
|
|
154
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target });
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
json: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
158
|
+
// value .default
|
|
159
|
+
try {
|
|
160
|
+
const parsedValue = JSON.stringify(value);
|
|
161
|
+
handlers.collect({
|
|
162
|
+
path,
|
|
163
|
+
value: parsedValue,
|
|
164
|
+
typeSchema,
|
|
165
|
+
fieldSchema,
|
|
166
|
+
target,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
catch (err) {
|
|
170
|
+
throw (0, handleError_1.createError)(path, target.type, 'json', value);
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
number: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
174
|
+
// value .default
|
|
175
|
+
// $increment / $decrement
|
|
176
|
+
if (typeof value !== 'number') {
|
|
177
|
+
throw (0, handleError_1.createError)(path, target.type, 'number', value);
|
|
178
|
+
}
|
|
179
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target });
|
|
180
|
+
},
|
|
181
|
+
integer: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
182
|
+
// value .default
|
|
183
|
+
// $increment / $decrement
|
|
184
|
+
if (typeof value !== 'number' || value - Math.floor(value) !== 0) {
|
|
185
|
+
throw (0, handleError_1.createError)(path, target.type, 'integer', value);
|
|
186
|
+
}
|
|
187
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target });
|
|
188
|
+
},
|
|
189
|
+
string: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
190
|
+
// default
|
|
191
|
+
if (typeof value !== 'string') {
|
|
192
|
+
throw (0, handleError_1.createError)(path, target.type, 'string', value);
|
|
193
|
+
}
|
|
194
|
+
// @ts-ignore
|
|
195
|
+
if (fieldSchema.minLength && value.length < fieldSchema.minLength) {
|
|
196
|
+
throw (0, handleError_1.createError)(path, target.type, 'string', value);
|
|
197
|
+
}
|
|
198
|
+
// @ts-ignore
|
|
199
|
+
if (fieldSchema.maxLength && value.length > fieldSchema.maxLength) {
|
|
200
|
+
throw (0, handleError_1.createError)(path, target.type, 'string', value);
|
|
201
|
+
}
|
|
202
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target });
|
|
203
|
+
},
|
|
204
|
+
text: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
205
|
+
// default
|
|
206
|
+
const valueType = typeof value;
|
|
207
|
+
if (target.$language && valueType === 'string') {
|
|
208
|
+
// @ts-ignore
|
|
209
|
+
if (fieldSchema.minLength && value.length < fieldSchema.minLength) {
|
|
210
|
+
throw (0, handleError_1.createError)(path, target.type, 'text', value);
|
|
211
|
+
}
|
|
212
|
+
// @ts-ignore
|
|
213
|
+
if (fieldSchema.maxLength && value.length > fieldSchema.maxLength) {
|
|
214
|
+
throw (0, handleError_1.createError)(path, target.type, 'text', value);
|
|
215
|
+
}
|
|
216
|
+
handlers.collect({
|
|
217
|
+
path,
|
|
218
|
+
value: { [target.$language]: value },
|
|
219
|
+
typeSchema,
|
|
220
|
+
fieldSchema,
|
|
221
|
+
target,
|
|
222
|
+
});
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
if (valueType !== 'object') {
|
|
226
|
+
throw (0, handleError_1.createError)(path, target.type, 'text', value);
|
|
227
|
+
}
|
|
228
|
+
for (const key in value) {
|
|
229
|
+
// @ts-ignore
|
|
230
|
+
if (fieldSchema.minLength && value[key].length < fieldSchema.minLength) {
|
|
231
|
+
throw (0, handleError_1.createError)([...path, key], target.type, 'text', value);
|
|
232
|
+
}
|
|
233
|
+
// @ts-ignore
|
|
234
|
+
if (fieldSchema.maxLength && value[key].length > fieldSchema.maxLength) {
|
|
235
|
+
throw (0, handleError_1.createError)([...path, key], target.type, 'text', value);
|
|
236
|
+
}
|
|
237
|
+
if (typeof value[key] === 'object' && value[key].$delete === true) {
|
|
238
|
+
handlers.collect({
|
|
239
|
+
path: [...path, key],
|
|
240
|
+
value: null,
|
|
241
|
+
typeSchema,
|
|
242
|
+
fieldSchema,
|
|
243
|
+
target,
|
|
244
|
+
});
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
if (typeof value[key] !== 'string') {
|
|
248
|
+
throw (0, handleError_1.createError)([...path, key], target.type, 'text', value);
|
|
249
|
+
}
|
|
250
|
+
handlers.collect({
|
|
251
|
+
path: [...path, key],
|
|
252
|
+
value: value[key],
|
|
253
|
+
typeSchema,
|
|
254
|
+
fieldSchema,
|
|
255
|
+
target,
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
},
|
|
259
|
+
reference,
|
|
260
|
+
references: async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
261
|
+
// default
|
|
262
|
+
// $no root
|
|
263
|
+
if (Array.isArray(value)) {
|
|
264
|
+
const handler = {
|
|
265
|
+
...handlers,
|
|
266
|
+
collect: () => { },
|
|
267
|
+
};
|
|
268
|
+
await Promise.all(value.map((v, i) => {
|
|
269
|
+
return reference([...path, i], v, fieldSchema, typeSchema, target, handler);
|
|
270
|
+
}));
|
|
271
|
+
}
|
|
272
|
+
else if (typeof value === 'object') {
|
|
273
|
+
if (value.$add) {
|
|
274
|
+
const handler = {
|
|
275
|
+
...handlers,
|
|
276
|
+
collect: () => { },
|
|
277
|
+
};
|
|
278
|
+
await Promise.all(value.$add.map((v, i) => {
|
|
279
|
+
return reference([...path, '$add', i], v, fieldSchema, typeSchema, target, handler);
|
|
280
|
+
}));
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target });
|
|
284
|
+
},
|
|
285
|
+
};
|
|
286
|
+
exports.default = parsers;
|
|
287
|
+
//# sourceMappingURL=parsers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parsers.js","sourceRoot":"","sources":["../../src/set/parsers.ts"],"names":[],"mappings":";;AAMA,wCAAwC;AACxC,+CAA2C;AAC3C,wBAA+B;AAW/B,MAAM,SAAS,GAAW,KAAK,EAC7B,IAAI,EACJ,KAAK,EACL,WAAW,EACX,UAAU,EACV,MAAM,EACN,QAAQ,EACR,EAAE;IACF,WAAW;IAEX,6BAA6B;IAC7B,iBAAiB;IACjB,SAAS;IAET,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;QAC7B,MAAM,IAAA,yBAAW,EAAC,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,WAAW,EAAE,KAAK,CAAC,CAAA;KACzD;IACD,IAAI,cAAc,IAAI,WAAW,EAAE;QACjC,MAAM,MAAM,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;QAChC,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,mBA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ACR,CAAA;YACH,CAAC,CAAC,CACH,CAAA;SACF;aAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;YACpC,IAAI,KAAK,CAAC,IAAI,EAAE;gBACd,MAAM,OAAO,GAAG;oBACd,GAAG,QAAQ;oBACX,OAAO,EAAE,GAAG,EAAE,GAAE,CAAC;iBAClB,CAAA;gBACD,MAAM,OAAO,CAAC,GAAG,CACf,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;oBACtB,OAAO,SAAS,CACd,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC,EACpB,CAAC,EACD,WAAW,EACX,UAAU,EACV,MAAM,EACN,OAAO,CACR,CAAA;gBACH,CAAC,CAAC,CACH,CAAA;aACF;SACF;QAED,QAAQ,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,EAAE,CAAC,CAAA;IACpE,CAAC;CACF,CAAA;AAED,kBAAe,OAAO,CAAA"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { BasedSchemaField, BasedSchemaType, BasedSchema, BasedSchemaLanguage } from './types';
|
|
2
|
+
type Target = {
|
|
3
|
+
type: string;
|
|
4
|
+
$alias?: string;
|
|
5
|
+
$id?: string;
|
|
6
|
+
$language?: BasedSchemaLanguage;
|
|
7
|
+
};
|
|
8
|
+
export declare const setWalker: (schema: BasedSchema, value: {
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
}, collect: (path: (string | number)[], value: any, typeSchema: BasedSchemaType, fieldSchema: BasedSchemaField, target: Target) => void) => Target;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setWalker = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
const utils_1 = require("@saulx/utils");
|
|
6
|
+
// collect is a pretty good place for checking if a user is allowed to set something
|
|
7
|
+
// also make collect async
|
|
8
|
+
const fieldWalker = (path, value, fieldSchema, typeSchema, target, collect) => {
|
|
9
|
+
if ('$ref' in fieldSchema) {
|
|
10
|
+
// TODO: when we have this it has to get it from the schema and redo the parsing with the correct fieldSchema
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const valueType = typeof value;
|
|
14
|
+
const valueIsObject = value && valueType === 'object';
|
|
15
|
+
if (valueIsObject && value.$delete === true) {
|
|
16
|
+
collect(path, value, typeSchema, fieldSchema, target);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if ('enum' in fieldSchema) {
|
|
20
|
+
const enumValues = fieldSchema.enum;
|
|
21
|
+
for (let i = 0; i < enumValues.length; i++) {
|
|
22
|
+
if ((0, utils_1.deepEqual)(enumValues[i], value)) {
|
|
23
|
+
collect(path, i, typeSchema, fieldSchema, target);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
throw new Error(`Type: "${target.type}" Field: "${path.join('.')}" is not a valid value for enum`);
|
|
28
|
+
}
|
|
29
|
+
if ('type' in fieldSchema && (0, types_1.isCollection)(fieldSchema.type)) {
|
|
30
|
+
const typeDef = fieldSchema.type;
|
|
31
|
+
const isArray = Array.isArray(value);
|
|
32
|
+
if (typeDef === 'array') {
|
|
33
|
+
if (!isArray) {
|
|
34
|
+
throw new Error(`Type: "${target.type}" Field: "${path.join('.')}" is not of type "array"`);
|
|
35
|
+
}
|
|
36
|
+
for (let i = 0; i < value.length; i++) {
|
|
37
|
+
fieldWalker([...path, i], value[i],
|
|
38
|
+
// @ts-ignore
|
|
39
|
+
fieldSchema.values, typeSchema, target, collect);
|
|
40
|
+
}
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
else if (isArray) {
|
|
44
|
+
throw new Error(`Type: "${target.type}" Field: "${path.join('.')}" is not of type "${typeDef}"`);
|
|
45
|
+
}
|
|
46
|
+
if (valueType !== 'object') {
|
|
47
|
+
throw new Error(`Type: "${target.type}" Field: "${path.join('.')}" is not of type "${typeDef}"`);
|
|
48
|
+
}
|
|
49
|
+
for (const key in value) {
|
|
50
|
+
// @ts-ignore
|
|
51
|
+
const propDef = fieldSchema.properties[key];
|
|
52
|
+
if (!propDef) {
|
|
53
|
+
throw new Error(`Field does not exist in schema "${[...path, key].join('.')}" on type "${target.type}"`);
|
|
54
|
+
}
|
|
55
|
+
fieldWalker([...path, key], value[key], propDef, typeSchema, target, collect);
|
|
56
|
+
}
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if ('type' in fieldSchema) {
|
|
60
|
+
const typeDef = fieldSchema.type;
|
|
61
|
+
if (typeDef === 'set') {
|
|
62
|
+
if (Array.isArray(value)) {
|
|
63
|
+
const parsedArray = [];
|
|
64
|
+
const fieldDef = fieldSchema.items;
|
|
65
|
+
for (let i = 0; i < value.length; i++) {
|
|
66
|
+
fieldWalker([...path, i], value[i], fieldDef, typeSchema, target, (path, value) => {
|
|
67
|
+
parsedArray.push(value);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
collect(path, parsedArray, typeSchema, fieldSchema, target);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
// TODO PARSE IF VALID
|
|
74
|
+
// $add / $remove
|
|
75
|
+
collect(path, value, typeSchema, fieldSchema, target);
|
|
76
|
+
}
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (typeDef === 'json') {
|
|
80
|
+
try {
|
|
81
|
+
const parsedValue = JSON.stringify(value);
|
|
82
|
+
collect(path, parsedValue, typeSchema, fieldSchema, target);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
throw new Error(`${value} cannot be parsed to json "${path.join('.')}" on type "${target.type}"`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if ((typeDef === 'number' || typeDef === 'integer') &&
|
|
90
|
+
valueType !== 'number') {
|
|
91
|
+
throw new Error(`${value} is not a number "${path.join('.')}" on type "${target.type}"`);
|
|
92
|
+
}
|
|
93
|
+
if (typeDef === 'integer' && value - Math.floor(value) !== 0) {
|
|
94
|
+
throw new Error(`${value} is not an integer "${path.join('.')}" on type "${target.type}"`);
|
|
95
|
+
}
|
|
96
|
+
if (typeDef === 'string') {
|
|
97
|
+
if (valueType !== 'string') {
|
|
98
|
+
throw new Error(`${value} is not a string "${path.join('.')}" on type "${target.type}"`);
|
|
99
|
+
}
|
|
100
|
+
if (fieldSchema.minLength && value.length < fieldSchema.minLength) {
|
|
101
|
+
throw new Error(`${value} is smaller then minLength "${fieldSchema.minLength}" "${path.join('.')}" on type "${target.type}"`);
|
|
102
|
+
}
|
|
103
|
+
if (fieldSchema.maxLength && value.length > fieldSchema.maxLength) {
|
|
104
|
+
throw new Error(`${value} is larger then maxLength "${fieldSchema.maxLength}" "${path.join('.')}" on type "${target.type}"`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (typeDef === 'text') {
|
|
108
|
+
if (target.$language && valueType === 'string') {
|
|
109
|
+
if (fieldSchema.minLength && value.length < fieldSchema.minLength) {
|
|
110
|
+
throw new Error(`${value} is smaller then minLength "${fieldSchema.minLength}" "${path.join('.')}" on type "${target.type}"`);
|
|
111
|
+
}
|
|
112
|
+
if (fieldSchema.maxLength && value.length > fieldSchema.maxLength) {
|
|
113
|
+
throw new Error(`${value} is larger then maxLength "${fieldSchema.maxLength}" "${path.join('.')}" on type "${target.type}"`);
|
|
114
|
+
}
|
|
115
|
+
collect(path, { [target.$language]: value }, typeSchema, fieldSchema, target);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (valueType !== 'object') {
|
|
119
|
+
throw new Error(`${value} is not a language object "${path.join('.')}" on type "${target.type}"`);
|
|
120
|
+
}
|
|
121
|
+
for (const key in value) {
|
|
122
|
+
if (fieldSchema.minLength &&
|
|
123
|
+
value[key].length < fieldSchema.minLength) {
|
|
124
|
+
throw new Error(`${value[key]} is smaller then minLength "${fieldSchema.minLength}" "${path.join('.')}" on type "${target.type}"`);
|
|
125
|
+
}
|
|
126
|
+
if (fieldSchema.maxLength &&
|
|
127
|
+
value[key].length > fieldSchema.maxLength) {
|
|
128
|
+
throw new Error(`${value[key]} is larger then maxLength "${fieldSchema.maxLength}" "${path.join('.')}" on type "${target.type}"`);
|
|
129
|
+
}
|
|
130
|
+
if (typeof value[key] === 'object' && value[key].$delete === true) {
|
|
131
|
+
collect([...path, key], null, typeSchema, fieldSchema, target);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (typeof value[key] !== 'string') {
|
|
135
|
+
throw new Error(`${value} is not a string "${[...path, key].join('.')}" on type "${target.type}"`);
|
|
136
|
+
}
|
|
137
|
+
collect([...path, key], value[key], typeSchema, fieldSchema, target);
|
|
138
|
+
}
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
collect(path, value, typeSchema, fieldSchema, target);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
const setWalker = (schema, value, collect) => {
|
|
146
|
+
let type;
|
|
147
|
+
if (value.$id) {
|
|
148
|
+
type = schema.prefixToTypeMapping[value.$id.slice(0, 2)];
|
|
149
|
+
if (!type) {
|
|
150
|
+
throw new Error(`Cannot find type for $id ${value.$id}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (value.type) {
|
|
154
|
+
if (type && value.type !== type) {
|
|
155
|
+
throw new Error(`type from "$id" ${value.$id} does not match "type" field ${value.type}`);
|
|
156
|
+
}
|
|
157
|
+
type = value.type;
|
|
158
|
+
}
|
|
159
|
+
const schemaType = schema.types[type];
|
|
160
|
+
if (!schemaType) {
|
|
161
|
+
throw new Error(`Cannot find schema definition for type ${type}`);
|
|
162
|
+
}
|
|
163
|
+
const target = {
|
|
164
|
+
type,
|
|
165
|
+
};
|
|
166
|
+
if (value.$id) {
|
|
167
|
+
target.$id = value.$id;
|
|
168
|
+
}
|
|
169
|
+
else if (value.$alias) {
|
|
170
|
+
target.$alias = value.$alias;
|
|
171
|
+
}
|
|
172
|
+
for (const key in value) {
|
|
173
|
+
if (key[0] === '$') {
|
|
174
|
+
console.info('key is operator', key);
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
const fieldSchema = schemaType.fields[key];
|
|
178
|
+
if (!fieldSchema) {
|
|
179
|
+
throw new Error(`Field does not exist in schema "${key}" on type "${type}"`);
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
fieldWalker([key], value[key], fieldSchema, schemaType, target, collect);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return target;
|
|
187
|
+
};
|
|
188
|
+
exports.setWalker = setWalker;
|
|
189
|
+
//# sourceMappingURL=setWalker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setWalker.js","sourceRoot":"","sources":["../src/setWalker.ts"],"names":[],"mappings":";;;AAAA,mCAMgB;AAChB,wCAAwC;AAExC,oFAAoF;AACpF,0BAA0B;AAE1B,MAAM,WAAW,GAAG,CAClB,IAAyB,EACzB,KAAU,EACV,WAA6B,EAC7B,UAA2B,EAC3B,MAAc,EACd,OAMS,EACT,EAAE;IACF,IAAI,MAAM,IAAI,WAAW,EAAE;QACzB,6GAA6G;QAC7G,OAAM;KACP;IAED,MAAM,SAAS,GAAG,OAAO,KAAK,CAAA;IAE9B,MAAM,aAAa,GAAG,KAAK,IAAI,SAAS,KAAK,QAAQ,CAAA;IAErD,IAAI,aAAa,IAAI,KAAK,CAAC,OAAO,KAAK,IAAI,EAAE;QAC3C,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,CAAC,CAAA;QACrD,OAAM;KACP;IAED,IAAI,MAAM,IAAI,WAAW,EAAE;QACzB,MAAM,UAAU,GAAG,WAAW,CAAC,IAAI,CAAA;QACnC,KAAK,IAAI,CAAC,GAAG,CAAC,E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KAAK,CAAC,MAAM,CAAA;KAC7B;IAED,KAAK,MAAM,GAAG,IAAI,KAAK,EAAE;QACvB,IAAI,GAAG,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE;YAClB,OAAO,CAAC,IAAI,CAAC,iBAAiB,EAAE,GAAG,CAAC,CAAA;SACrC;aAAM;YACL,MAAM,WAAW,GAAG,UAAU,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;YAC1C,IAAI,CAAC,WAAW,EAAE;gBAChB,MAAM,IAAI,KAAK,CACb,mCAAmC,GAAG,cAAc,IAAI,GAAG,CAC5D,CAAA;aACF;iBAAM;gBACL,WAAW,CAAC,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,EAAE,OAAO,CAAC,CAAA;aACzE;SACF;KACF;IAED,OAAO,MAAM,CAAA;AACf,CAAC,CAAA;AA7DY,QAAA,SAAS,aA6DrB"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { BasedSchemaPartial, BasedSchemaTypePartial } from './types';
|
|
2
|
+
export declare const transformField: (fromSchema: BasedSchemaPartial, typeName: string, type: BasedSchemaTypePartial) => void;
|
|
3
|
+
export declare const transformType: (fromSchema: BasedSchemaPartial, typeName: string, type: BasedSchemaTypePartial) => void;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.transformType = exports.transformField = void 0;
|
|
4
|
+
const transformField = (fromSchema, typeName, type) => {
|
|
5
|
+
if (type.prefix &&
|
|
6
|
+
(typeof type.prefix !== 'string' || type.prefix.length !== 2)) {
|
|
7
|
+
throw new Error(`Incorrect prefix "${type.prefix}" for type "${typeName}" has to be a string of 2 alphanumerical characters e.g. "Az", "ab", "cc", "10"`);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
exports.transformField = transformField;
|
|
11
|
+
const transformType = (fromSchema, typeName, type) => {
|
|
12
|
+
if (type.prefix &&
|
|
13
|
+
(typeof type.prefix !== 'string' || type.prefix.length !== 2)) {
|
|
14
|
+
throw new Error(`Incorrect prefix "${type.prefix}" for type "${typeName}" has to be a string of 2 alphanumerical characters e.g. "Az", "ab", "cc", "10"`);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
exports.transformType = transformType;
|
|
18
|
+
//# sourceMappingURL=transformers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transformers.js","sourceRoot":"","sources":["../src/transformers.ts"],"names":[],"mappings":";;;AAOO,MAAM,cAAc,GAAG,CAC5B,UAA8B,EAC9B,QAAgB,EAChB,IAA4B,EAC5B,EAAE;IACF,IACE,IAAI,CAAC,MAAM;QACX,CAAC,OAAO,IAAI,CAAC,MAAM,KAAK,QAAQ,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,CAAC,EAC7D;QACA,MAAM,IAAI,KAAK,CACb,qBAAqB,IAAI,CAAC,MAAM,eAAe,QAAQ,iFAAiF,CACzI,CAAA;KACF;AACH,CAAC,CAAA;AAbY,QAAA,cAAc,kBAa1B;AAEM,MAAM,aAAa,GAAG,CAC3B,UAA8B,EAC9B,QAAgB,EAChB,IAA4B,EAC5B,EAAE;IACF,IACE,IAAI,CAAC,MAAM;QACX,CAAC,OAAO,IAAI,CAAC,MAAM,KAAK,QAAQ,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,CAAC,EAC7D;QACA,MAAM,IAAI,KAAK,CACb,qBAAqB,IAAI,CAAC,MAAM,eAAe,QAAQ,iFAAiF,CACzI,CAAA;KACF;AACH,CAAC,CAAA;AAbY,QAAA,aAAa,iBAazB"}
|