@based/schema 0.0.5 → 0.0.6
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/parse.d.ts +2 -0
- package/dist/parse.js +9 -0
- package/dist/parse.js.map +1 -0
- package/dist/set/collections.d.ts +5 -0
- package/dist/set/collections.js +139 -0
- package/dist/set/collections.js.map +1 -0
- package/dist/set/error.d.ts +10 -0
- package/dist/set/error.js +19 -0
- package/dist/set/error.js.map +1 -0
- package/dist/set/index.js +8 -14
- package/dist/set/index.js.map +1 -1
- package/dist/set/number.d.ts +4 -0
- package/dist/set/number.js +57 -0
- package/dist/set/number.js.map +1 -0
- package/dist/set/parsers.d.ts +2 -5
- package/dist/set/parsers.js +35 -357
- package/dist/set/parsers.js.map +1 -1
- package/dist/set/references.d.ts +3 -0
- package/dist/set/references.js +81 -0
- package/dist/set/references.js.map +1 -0
- package/dist/set/rest.d.ts +5 -0
- package/dist/set/rest.js +53 -0
- package/dist/set/rest.js.map +1 -0
- package/dist/set/string.d.ts +3 -0
- package/dist/set/string.js +136 -0
- package/dist/set/string.js.map +1 -0
- package/dist/set/types.d.ts +5 -0
- package/dist/{deepPartial.js → set/types.js} +1 -1
- package/dist/set/types.js.map +1 -0
- package/dist/types.d.ts +36 -17
- package/dist/types.js.map +1 -1
- package/package.json +3 -1
- package/src/set/collections.ts +246 -0
- package/src/set/error.ts +17 -0
- package/src/set/index.ts +8 -18
- package/src/set/number.ts +78 -0
- package/src/set/parsers.ts +17 -553
- package/src/set/references.ts +107 -0
- package/src/set/rest.ts +76 -0
- package/src/set/string.ts +159 -0
- package/src/set/types.ts +19 -0
- package/src/types.ts +105 -20
- package/test/setWalker.ts +16 -7
- package/dist/deepPartial.d.ts +0 -0
- package/dist/deepPartial.js.map +0 -1
- package/dist/set/enum.d.ts +0 -2
- package/dist/set/enum.js +0 -15
- package/dist/set/enum.js.map +0 -1
- package/dist/set/fieldValidator.d.ts +0 -6
- package/dist/set/fieldValidator.js +0 -144
- package/dist/set/fieldValidator.js.map +0 -1
- package/dist/set/handleError.d.ts +0 -1
- package/dist/set/handleError.js +0 -9
- package/dist/set/handleError.js.map +0 -1
- package/dist/setWalker.d.ts +0 -11
- package/dist/setWalker.js +0 -189
- package/dist/setWalker.js.map +0 -1
- package/dist/transformers.d.ts +0 -3
- package/dist/transformers.js +0 -18
- package/dist/transformers.js.map +0 -1
- package/dist/typeWalker.d.ts +0 -3
- package/dist/typeWalker.js +0 -18
- package/dist/typeWalker.js.map +0 -1
- package/dist/validate.d.ts +0 -4
- package/dist/validate.js +0 -34
- package/dist/validate.js.map +0 -1
- package/dist/validateFields.d.ts +0 -4
- package/dist/validateFields.js +0 -34
- package/dist/validateFields.js.map +0 -1
- package/dist/validateSchema copy.d.ts +0 -4
- package/dist/validateSchema copy.js +0 -34
- package/dist/validateSchema copy.js.map +0 -1
- package/src/set/handleError.ts +0 -15
|
@@ -0,0 +1,136 @@
|
|
|
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.text = exports.string = void 0;
|
|
7
|
+
const error_1 = require("./error");
|
|
8
|
+
const validator_1 = __importDefault(require("validator"));
|
|
9
|
+
const formatPatterns = {
|
|
10
|
+
email: validator_1.default.isEmail,
|
|
11
|
+
URL: validator_1.default.isURL,
|
|
12
|
+
MACAddress: validator_1.default.isMACAddress,
|
|
13
|
+
IP: validator_1.default.isIP,
|
|
14
|
+
IPRange: validator_1.default.isIPRange,
|
|
15
|
+
FQDN: validator_1.default.isFQDN,
|
|
16
|
+
IBAN: validator_1.default.isIBAN,
|
|
17
|
+
BIC: validator_1.default.isBIC,
|
|
18
|
+
alpha: validator_1.default.isAlpha,
|
|
19
|
+
alphaLocales: validator_1.default.isAlphaLocales,
|
|
20
|
+
alphanumeric: validator_1.default.isAlphanumeric,
|
|
21
|
+
alphanumericLocales: validator_1.default.isAlphanumericLocales,
|
|
22
|
+
passportNumber: validator_1.default.isPassportNumber,
|
|
23
|
+
port: validator_1.default.isPort,
|
|
24
|
+
lowercase: validator_1.default.isLowercase,
|
|
25
|
+
uppercase: validator_1.default.isUppercase,
|
|
26
|
+
ascii: validator_1.default.isAscii,
|
|
27
|
+
semVer: validator_1.default.isSemVer,
|
|
28
|
+
surrogatePair: validator_1.default.isSurrogatePair,
|
|
29
|
+
IMEI: validator_1.default.isIMEI,
|
|
30
|
+
hexadecimal: validator_1.default.isHexadecimal,
|
|
31
|
+
octal: validator_1.default.isOctal,
|
|
32
|
+
hexColor: validator_1.default.isHexColor,
|
|
33
|
+
rgbColor: validator_1.default.isRgbColor,
|
|
34
|
+
HSL: validator_1.default.isHSL,
|
|
35
|
+
ISRC: validator_1.default.isISRC,
|
|
36
|
+
MD5: validator_1.default.isMD5,
|
|
37
|
+
JWT: validator_1.default.isJWT,
|
|
38
|
+
UUID: validator_1.default.isUUID,
|
|
39
|
+
luhnNumber: validator_1.default.isLuhnNumber,
|
|
40
|
+
creditCard: validator_1.default.isCreditCard,
|
|
41
|
+
identityCard: validator_1.default.isIdentityCard,
|
|
42
|
+
EAN: validator_1.default.isEAN,
|
|
43
|
+
ISIN: validator_1.default.isISIN,
|
|
44
|
+
ISBN: validator_1.default.isISBN,
|
|
45
|
+
ISSN: validator_1.default.isISSN,
|
|
46
|
+
mobilePhone: validator_1.default.isMobilePhone,
|
|
47
|
+
mobilePhoneLocales: validator_1.default.isMobilePhoneLocales,
|
|
48
|
+
postalCode: validator_1.default.isPostalCode,
|
|
49
|
+
postalCodeLocales: validator_1.default.isPostalCodeLocales,
|
|
50
|
+
ethereumAddress: validator_1.default.isEthereumAddress,
|
|
51
|
+
currency: validator_1.default.isCurrency,
|
|
52
|
+
btcAddress: validator_1.default.isBtcAddress,
|
|
53
|
+
ISO6391: validator_1.default.isISO6391,
|
|
54
|
+
ISO8601: validator_1.default.isISO8601,
|
|
55
|
+
RFC3339: validator_1.default.isRFC3339,
|
|
56
|
+
ISO31661Alpha2: validator_1.default.isISO31661Alpha2,
|
|
57
|
+
ISO31661Alpha3: validator_1.default.isISO31661Alpha3,
|
|
58
|
+
ISO4217: validator_1.default.isISO4217,
|
|
59
|
+
base32: validator_1.default.isBase32,
|
|
60
|
+
base58: validator_1.default.isBase58,
|
|
61
|
+
base64: validator_1.default.isBase64,
|
|
62
|
+
dataURI: validator_1.default.isDataURI,
|
|
63
|
+
magnetURI: validator_1.default.isMagnetURI,
|
|
64
|
+
mimeType: validator_1.default.isMimeType,
|
|
65
|
+
latLong: validator_1.default.isLatLong,
|
|
66
|
+
slug: validator_1.default.isSlug,
|
|
67
|
+
strongPassword: validator_1.default.isStrongPassword,
|
|
68
|
+
taxID: validator_1.default.isTaxID,
|
|
69
|
+
licensePlate: validator_1.default.isLicensePlate,
|
|
70
|
+
VAT: validator_1.default.isVAT,
|
|
71
|
+
};
|
|
72
|
+
const validate = (path, value, fieldSchema) => {
|
|
73
|
+
if (typeof value !== 'string') {
|
|
74
|
+
(0, error_1.error)(path, error_1.ParseError.incorrectFormat);
|
|
75
|
+
}
|
|
76
|
+
if (fieldSchema.minLength && value.length < fieldSchema.minLength) {
|
|
77
|
+
(0, error_1.error)(path, error_1.ParseError.subceedsMinimum);
|
|
78
|
+
}
|
|
79
|
+
if (fieldSchema.maxLength && value.length > fieldSchema.maxLength) {
|
|
80
|
+
(0, error_1.error)(path, error_1.ParseError.exceedsMaximum);
|
|
81
|
+
}
|
|
82
|
+
if (fieldSchema.pattern) {
|
|
83
|
+
const re = new RegExp(fieldSchema.pattern);
|
|
84
|
+
if (!re.test(value)) {
|
|
85
|
+
(0, error_1.error)(path, error_1.ParseError.incorrectFormat);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (fieldSchema.format && !formatPatterns[fieldSchema.format](value)) {
|
|
89
|
+
(0, error_1.error)(path, error_1.ParseError.incorrectFormat);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
const string = async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
93
|
+
validate(path, value, fieldSchema);
|
|
94
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target });
|
|
95
|
+
};
|
|
96
|
+
exports.string = string;
|
|
97
|
+
const text = async (path, value, fieldSchema, typeSchema, target, handlers) => {
|
|
98
|
+
const valueType = typeof value;
|
|
99
|
+
if (target.$language && valueType === 'string') {
|
|
100
|
+
validate(path, value, fieldSchema);
|
|
101
|
+
handlers.collect({
|
|
102
|
+
path,
|
|
103
|
+
value: { [target.$language]: value },
|
|
104
|
+
typeSchema,
|
|
105
|
+
fieldSchema,
|
|
106
|
+
target,
|
|
107
|
+
});
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (valueType !== 'object') {
|
|
111
|
+
(0, error_1.error)(path, error_1.ParseError.incorrectFormat);
|
|
112
|
+
}
|
|
113
|
+
for (const key in value) {
|
|
114
|
+
const newPath = [...path, key];
|
|
115
|
+
if (typeof value[key] === 'object' && value[key].$delete === true) {
|
|
116
|
+
handlers.collect({
|
|
117
|
+
path: newPath,
|
|
118
|
+
value: null,
|
|
119
|
+
typeSchema,
|
|
120
|
+
fieldSchema,
|
|
121
|
+
target,
|
|
122
|
+
});
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
validate(newPath, value[key], fieldSchema);
|
|
126
|
+
handlers.collect({
|
|
127
|
+
path: newPath,
|
|
128
|
+
value: value[key],
|
|
129
|
+
typeSchema,
|
|
130
|
+
fieldSchema,
|
|
131
|
+
target,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
exports.text = text;
|
|
136
|
+
//# sourceMappingURL=string.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"string.js","sourceRoot":"","sources":["../../src/set/string.ts"],"names":[],"mappings":";;;;;;AACA,mCAA2C;AAE3C,0DAAkC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get, BasedSchemaFields } from '../types';
|
|
2
|
+
export type Parser<K extends keyof BasedSchemaFields> = (path: (string | number)[], value: any, fieldSchema: BasedSchemaFields[K], typeSchema: BasedSchemaType, target: BasedSetTarget, handlers: BasedSetHandlers) => Promise<void>;
|
|
3
|
+
export type Parsers = {
|
|
4
|
+
[Key in keyof BasedSchemaFields]: Parser<Key>;
|
|
5
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/set/types.ts"],"names":[],"mappings":""}
|
package/dist/types.d.ts
CHANGED
|
@@ -4,19 +4,27 @@ export type AllowedTypes = (string | {
|
|
|
4
4
|
type?: string;
|
|
5
5
|
$filter: any | any[];
|
|
6
6
|
})[];
|
|
7
|
-
export type BasedSchemaFieldType = 'array' | 'object' | 'record' | 'set' | 'string' | 'boolean' | 'number' | '
|
|
7
|
+
export type BasedSchemaFieldType = 'array' | 'object' | 'record' | 'set' | 'string' | 'boolean' | 'number' | 'json' | 'integer' | 'timestamp' | 'reference' | 'references' | 'text' | 'cardinality';
|
|
8
8
|
export declare const isCollection: (type: string) => boolean;
|
|
9
9
|
export type BasedSchemaPattern = string;
|
|
10
10
|
export type BasedSchemaLanguage = Language;
|
|
11
11
|
export type BasedSchemaTypePrefix = string;
|
|
12
12
|
export type BasedSchemaContentMediaType = 'text/html' | 'text/plain' | 'text/markdown' | 'image/png' | 'image/jpeg' | 'video/mp4' | string;
|
|
13
13
|
export type BasedSchemaFieldShared = {
|
|
14
|
+
hooks?: {
|
|
15
|
+
interval?: number;
|
|
16
|
+
hook: string;
|
|
17
|
+
} | {
|
|
18
|
+
interval?: number;
|
|
19
|
+
hook: string;
|
|
20
|
+
}[];
|
|
14
21
|
type?: BasedSchemaFieldType;
|
|
15
22
|
$id?: string;
|
|
16
23
|
$schema?: string;
|
|
17
24
|
isRequired?: boolean;
|
|
18
25
|
title?: string;
|
|
19
26
|
description?: string;
|
|
27
|
+
index?: number;
|
|
20
28
|
readOnly?: boolean;
|
|
21
29
|
writeOnly?: boolean;
|
|
22
30
|
$comment?: string;
|
|
@@ -27,21 +35,20 @@ export type BasedSchemaFieldShared = {
|
|
|
27
35
|
[key: string]: BasedSchemaField;
|
|
28
36
|
};
|
|
29
37
|
};
|
|
30
|
-
export type
|
|
31
|
-
type: 'string';
|
|
38
|
+
export type BasedSchemaStringShared = {
|
|
32
39
|
minLength?: number;
|
|
33
40
|
maxLength?: number;
|
|
34
41
|
contentMediaEncoding?: string;
|
|
35
42
|
contentMediaType?: BasedSchemaContentMediaType;
|
|
36
43
|
pattern?: BasedSchemaPattern;
|
|
37
|
-
format?: 'email' | '
|
|
38
|
-
}
|
|
44
|
+
format?: 'email' | 'URL' | 'MACAddress' | 'IP' | 'IPRange' | 'FQDN' | 'IBAN' | 'BIC' | 'alpha' | 'alphaLocales' | 'alphanumeric' | 'alphanumericLocales' | 'passportNumber' | 'port' | 'lowercase' | 'uppercase' | 'ascii' | 'semVer' | 'surrogatePair' | 'IMEI' | 'hexadecimal' | 'octal' | 'hexColor' | 'rgbColor' | 'HSL' | 'ISRC' | 'MD5' | 'JWT' | 'UUID' | 'luhnNumber' | 'creditCard' | 'identityCard' | 'EAN' | 'ISIN' | 'ISBN' | 'ISSN' | 'mobilePhone' | 'mobilePhoneLocales' | 'postalCode' | 'postalCodeLocales' | 'ethereumAddress' | 'currency' | 'btcAddress' | 'ISO6391' | 'ISO8601' | 'RFC3339' | 'ISO31661Alpha2' | 'ISO31661Alpha3' | 'ISO4217' | 'base32' | 'base58' | 'base64' | 'dataURI' | 'magnetURI' | 'mimeType' | 'latLong' | 'slug' | 'strongPassword' | 'taxID' | 'licensePlate' | 'VAT';
|
|
45
|
+
};
|
|
46
|
+
export type BasedSchemaFieldString = {
|
|
47
|
+
type: 'string';
|
|
48
|
+
} & BasedSchemaFieldShared & BasedSchemaStringShared;
|
|
39
49
|
export type BasedSchemaFieldEnum = {
|
|
40
50
|
enum: any[];
|
|
41
51
|
} & BasedSchemaFieldShared;
|
|
42
|
-
export type BasedSchemaFieldConst = {
|
|
43
|
-
const: any;
|
|
44
|
-
} & BasedSchemaFieldShared;
|
|
45
52
|
type NumberDefaults = {
|
|
46
53
|
multipleOf?: number;
|
|
47
54
|
minimum?: number;
|
|
@@ -52,8 +59,8 @@ type NumberDefaults = {
|
|
|
52
59
|
export type BasedSchemaFieldNumber = NumberDefaults & {
|
|
53
60
|
type: 'number';
|
|
54
61
|
};
|
|
55
|
-
export type
|
|
56
|
-
type: '
|
|
62
|
+
export type BasedSchemaFieldCardinality = {
|
|
63
|
+
type: 'cardinality';
|
|
57
64
|
};
|
|
58
65
|
export type BasedSchemaFieldInteger = NumberDefaults & {
|
|
59
66
|
type: 'integer';
|
|
@@ -71,12 +78,7 @@ export type BasedSchemaFieldPrimitive = BasedSchemaFieldString | BasedSchemaFiel
|
|
|
71
78
|
export type BasedSchemaFieldText = {
|
|
72
79
|
type: 'text';
|
|
73
80
|
required?: BasedSchemaLanguage[];
|
|
74
|
-
|
|
75
|
-
minLength?: number;
|
|
76
|
-
maxLength?: number;
|
|
77
|
-
contentMediaEncoding?: string;
|
|
78
|
-
pattern?: BasedSchemaPattern;
|
|
79
|
-
} & BasedSchemaFieldShared;
|
|
81
|
+
} & BasedSchemaFieldShared & BasedSchemaStringShared;
|
|
80
82
|
export type BasedSchemaFieldObject = {
|
|
81
83
|
type: 'object';
|
|
82
84
|
properties: {
|
|
@@ -111,11 +113,28 @@ export type BasedSchemaFieldReferences = {
|
|
|
111
113
|
};
|
|
112
114
|
allowedTypes?: AllowedTypes;
|
|
113
115
|
} & BasedSchemaFieldShared;
|
|
114
|
-
export type BasedSchemaField = BasedSchemaFieldEnumerable | BasedSchemaFieldPrimitive | BasedSchemaFieldReference | BasedSchemaFieldReferences |
|
|
116
|
+
export type BasedSchemaField = BasedSchemaFieldEnumerable | BasedSchemaFieldPrimitive | BasedSchemaFieldReference | BasedSchemaFieldReferences | BasedSchemaFieldCardinality | {
|
|
115
117
|
type?: BasedSchemaFieldType;
|
|
116
118
|
isRequired?: boolean;
|
|
117
119
|
$ref: string;
|
|
118
120
|
};
|
|
121
|
+
export type BasedSchemaFields = {
|
|
122
|
+
enum: BasedSchemaFieldEnum;
|
|
123
|
+
array: BasedSchemaFieldArray;
|
|
124
|
+
object: BasedSchemaFieldObject;
|
|
125
|
+
set: BasedSchemaFieldSet;
|
|
126
|
+
record: BasedSchemaFieldRecord;
|
|
127
|
+
string: BasedSchemaFieldString;
|
|
128
|
+
boolean: BasedSchemaFieldBoolean;
|
|
129
|
+
number: BasedSchemaFieldNumber;
|
|
130
|
+
json: BasedSchemaFieldJSON;
|
|
131
|
+
integer: BasedSchemaFieldInteger;
|
|
132
|
+
timestamp: BasedSchemaFieldTimeStamp;
|
|
133
|
+
reference: BasedSchemaFieldReference;
|
|
134
|
+
references: BasedSchemaFieldReferences;
|
|
135
|
+
text: BasedSchemaFieldText;
|
|
136
|
+
cardinality: BasedSchemaFieldCardinality;
|
|
137
|
+
};
|
|
119
138
|
export type BasedSchemaType = {
|
|
120
139
|
fields: {
|
|
121
140
|
[name: string]: BasedSchemaField;
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAsCO,MAAM,YAAY,GAAG,CAAC,IAAY,EAAW,EAAE;IACpD,OAAO,IAAI,KAAK,OAAO,IAAI,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,QAAQ,CAAA;AACnE,CAAC,CAAA;AAFY,QAAA,YAAY,gBAExB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@based/schema",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
},
|
|
12
12
|
"sideEffects": false,
|
|
13
13
|
"dependencies": {
|
|
14
|
+
"validator": "^13.9.0",
|
|
15
|
+
"@saulx/hash": "^2.0.0",
|
|
14
16
|
"@saulx/utils": "^3.2.2"
|
|
15
17
|
},
|
|
16
18
|
"ava": {
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { Parser } from './types'
|
|
2
|
+
import { error, ParseError } from './error'
|
|
3
|
+
import { fieldWalker } from '.'
|
|
4
|
+
|
|
5
|
+
export const set: Parser<'set'> = async (
|
|
6
|
+
path,
|
|
7
|
+
value,
|
|
8
|
+
fieldSchema,
|
|
9
|
+
typeSchema,
|
|
10
|
+
target,
|
|
11
|
+
handlers
|
|
12
|
+
) => {
|
|
13
|
+
const q: Promise<void>[] = []
|
|
14
|
+
const fieldDef = fieldSchema.items
|
|
15
|
+
if (Array.isArray(value)) {
|
|
16
|
+
const handlerNest = {
|
|
17
|
+
...handlers,
|
|
18
|
+
collect: ({ value }) => {
|
|
19
|
+
parsedArray.push(value)
|
|
20
|
+
},
|
|
21
|
+
}
|
|
22
|
+
const parsedArray = []
|
|
23
|
+
for (let i = 0; i < value.length; i++) {
|
|
24
|
+
q.push(
|
|
25
|
+
fieldWalker(
|
|
26
|
+
[...path, i],
|
|
27
|
+
value[i],
|
|
28
|
+
fieldDef,
|
|
29
|
+
typeSchema,
|
|
30
|
+
target,
|
|
31
|
+
handlerNest
|
|
32
|
+
)
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
await Promise.all(q)
|
|
36
|
+
handlers.collect({
|
|
37
|
+
path,
|
|
38
|
+
value: { $value: parsedArray },
|
|
39
|
+
typeSchema,
|
|
40
|
+
fieldSchema,
|
|
41
|
+
target,
|
|
42
|
+
})
|
|
43
|
+
} else {
|
|
44
|
+
const handlerNest = {
|
|
45
|
+
...handlers,
|
|
46
|
+
collect: () => {},
|
|
47
|
+
}
|
|
48
|
+
if (value.$add) {
|
|
49
|
+
for (let i = 0; i < value.$add.length; i++) {
|
|
50
|
+
q.push(
|
|
51
|
+
fieldWalker(
|
|
52
|
+
[...path, '$add', i],
|
|
53
|
+
value.$add[i],
|
|
54
|
+
fieldDef,
|
|
55
|
+
typeSchema,
|
|
56
|
+
target,
|
|
57
|
+
handlerNest
|
|
58
|
+
)
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (value.$delete) {
|
|
63
|
+
for (let i = 0; i < value.$add.length; i++) {
|
|
64
|
+
q.push(
|
|
65
|
+
fieldWalker(
|
|
66
|
+
[...path, '$delete', i],
|
|
67
|
+
value.$delete[i],
|
|
68
|
+
fieldDef,
|
|
69
|
+
typeSchema,
|
|
70
|
+
target,
|
|
71
|
+
handlerNest
|
|
72
|
+
)
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
await Promise.all(q)
|
|
77
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export const object: Parser<'object'> = async (
|
|
82
|
+
path,
|
|
83
|
+
value,
|
|
84
|
+
fieldSchema,
|
|
85
|
+
typeSchema,
|
|
86
|
+
target,
|
|
87
|
+
handlers
|
|
88
|
+
) => {
|
|
89
|
+
if (typeof value !== 'object') {
|
|
90
|
+
error(path, ParseError.incorrectFormat)
|
|
91
|
+
}
|
|
92
|
+
const isArray = Array.isArray(value)
|
|
93
|
+
if (isArray) {
|
|
94
|
+
error(path, ParseError.incorrectFormat)
|
|
95
|
+
}
|
|
96
|
+
const q: Promise<void>[] = []
|
|
97
|
+
for (const key in value) {
|
|
98
|
+
const propDef = fieldSchema.properties[key]
|
|
99
|
+
if (!propDef) {
|
|
100
|
+
error([...path, key], ParseError.fieldDoesNotExist)
|
|
101
|
+
}
|
|
102
|
+
q.push(
|
|
103
|
+
fieldWalker(
|
|
104
|
+
[...path, key],
|
|
105
|
+
value[key],
|
|
106
|
+
propDef,
|
|
107
|
+
typeSchema,
|
|
108
|
+
target,
|
|
109
|
+
handlers
|
|
110
|
+
)
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
await Promise.all(q)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export const array: Parser<'array'> = async (
|
|
117
|
+
path,
|
|
118
|
+
value,
|
|
119
|
+
fieldSchema,
|
|
120
|
+
typeSchema,
|
|
121
|
+
target,
|
|
122
|
+
handlers
|
|
123
|
+
) => {
|
|
124
|
+
const isArray = Array.isArray(value)
|
|
125
|
+
if (typeof value === 'object' && !isArray) {
|
|
126
|
+
if (value.$insert) {
|
|
127
|
+
if (
|
|
128
|
+
typeof value.$insert !== 'object' ||
|
|
129
|
+
value.$insert.$idx === undefined
|
|
130
|
+
) {
|
|
131
|
+
error([...path, '$insert'], ParseError.incorrectFormat)
|
|
132
|
+
} else {
|
|
133
|
+
const nestedHandler = {
|
|
134
|
+
...handlers,
|
|
135
|
+
collect: () => {},
|
|
136
|
+
}
|
|
137
|
+
const q: Promise<void>[] = []
|
|
138
|
+
if (Array.isArray(value.$insert.$value)) {
|
|
139
|
+
for (let i = 0; i < value.$insert.$value.length; i++) {
|
|
140
|
+
q.push(
|
|
141
|
+
fieldWalker(
|
|
142
|
+
[...path, 'insert', i],
|
|
143
|
+
value.$insert.$value[i],
|
|
144
|
+
fieldSchema.values,
|
|
145
|
+
typeSchema,
|
|
146
|
+
target,
|
|
147
|
+
nestedHandler
|
|
148
|
+
)
|
|
149
|
+
)
|
|
150
|
+
}
|
|
151
|
+
} else {
|
|
152
|
+
q.push(
|
|
153
|
+
fieldWalker(
|
|
154
|
+
[...path, '$insert'],
|
|
155
|
+
value.$insert.$value,
|
|
156
|
+
fieldSchema.values,
|
|
157
|
+
typeSchema,
|
|
158
|
+
target,
|
|
159
|
+
nestedHandler
|
|
160
|
+
)
|
|
161
|
+
)
|
|
162
|
+
}
|
|
163
|
+
await Promise.all(q)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (value.$remove && value.$remove.$idx === undefined) {
|
|
167
|
+
error([...path, '$remove'], ParseError.incorrectFormat)
|
|
168
|
+
}
|
|
169
|
+
if (value.$push) {
|
|
170
|
+
const q: Promise<void>[] = []
|
|
171
|
+
const nestedHandler = {
|
|
172
|
+
...handlers,
|
|
173
|
+
collect: () => {},
|
|
174
|
+
}
|
|
175
|
+
if (Array.isArray(value.$push)) {
|
|
176
|
+
for (let i = 0; i < value.$push.length; i++) {
|
|
177
|
+
q.push(
|
|
178
|
+
fieldWalker(
|
|
179
|
+
[...path, i],
|
|
180
|
+
value.$push[i],
|
|
181
|
+
fieldSchema.values,
|
|
182
|
+
typeSchema,
|
|
183
|
+
target,
|
|
184
|
+
nestedHandler
|
|
185
|
+
)
|
|
186
|
+
)
|
|
187
|
+
}
|
|
188
|
+
} else {
|
|
189
|
+
q.push(
|
|
190
|
+
fieldWalker(
|
|
191
|
+
[...path, '$push'],
|
|
192
|
+
value.$push,
|
|
193
|
+
fieldSchema.values,
|
|
194
|
+
typeSchema,
|
|
195
|
+
target,
|
|
196
|
+
nestedHandler
|
|
197
|
+
)
|
|
198
|
+
)
|
|
199
|
+
}
|
|
200
|
+
await Promise.all(q)
|
|
201
|
+
}
|
|
202
|
+
if (value.$assign) {
|
|
203
|
+
if (
|
|
204
|
+
typeof value.$assign !== 'object' ||
|
|
205
|
+
value.$assign.$idx === undefined
|
|
206
|
+
) {
|
|
207
|
+
error([...path, '$assign'], ParseError.incorrectFormat)
|
|
208
|
+
} else {
|
|
209
|
+
await fieldWalker(
|
|
210
|
+
[...path, '$assign', '$value'],
|
|
211
|
+
value.$assign.$value,
|
|
212
|
+
fieldSchema.values,
|
|
213
|
+
typeSchema,
|
|
214
|
+
target,
|
|
215
|
+
{
|
|
216
|
+
...handlers,
|
|
217
|
+
collect: () => {},
|
|
218
|
+
}
|
|
219
|
+
)
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
223
|
+
return
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (!isArray) {
|
|
227
|
+
error(path, ParseError.incorrectFieldType)
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const q: Promise<void>[] = []
|
|
231
|
+
for (let i = 0; i < value.length; i++) {
|
|
232
|
+
q.push(
|
|
233
|
+
fieldWalker(
|
|
234
|
+
[...path, i],
|
|
235
|
+
value[i],
|
|
236
|
+
fieldSchema.values,
|
|
237
|
+
typeSchema,
|
|
238
|
+
target,
|
|
239
|
+
handlers
|
|
240
|
+
)
|
|
241
|
+
)
|
|
242
|
+
}
|
|
243
|
+
await Promise.all(q)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export const record: Parser<'record'> = async (path, value, fieldSchema) => {}
|
package/src/set/error.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export enum ParseError {
|
|
2
|
+
'incorrectFieldType',
|
|
3
|
+
'incorrectNodeType',
|
|
4
|
+
'exceedsMaximum',
|
|
5
|
+
'subceedsMinimum',
|
|
6
|
+
'fieldDoesNotExist',
|
|
7
|
+
'incorrectFormat',
|
|
8
|
+
'referenceIsIncorrectType',
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const error = (
|
|
12
|
+
path: (number | string)[],
|
|
13
|
+
error: ParseError,
|
|
14
|
+
type?: string // nice to give as option
|
|
15
|
+
) => {
|
|
16
|
+
throw new Error(`Field: "${path.join('.')}" ${ParseError[error]}`)
|
|
17
|
+
}
|
package/src/set/index.ts
CHANGED
|
@@ -5,13 +5,9 @@ import {
|
|
|
5
5
|
BasedSchema,
|
|
6
6
|
BasedSetTarget,
|
|
7
7
|
} from '../types'
|
|
8
|
-
import {
|
|
8
|
+
import { error, ParseError } from './error'
|
|
9
9
|
import parsers from './parsers'
|
|
10
10
|
|
|
11
|
-
// Collect is a pretty good place for checking if a user is allowed to set something
|
|
12
|
-
// also make collect async
|
|
13
|
-
// add extra function for loading required
|
|
14
|
-
|
|
15
11
|
export const fieldWalker = async (
|
|
16
12
|
path: (string | number)[],
|
|
17
13
|
value: any,
|
|
@@ -35,13 +31,13 @@ export const fieldWalker = async (
|
|
|
35
31
|
const typeDef = fieldSchema.type ?? ('enum' in fieldSchema ? 'enum' : '')
|
|
36
32
|
|
|
37
33
|
if (!typeDef) {
|
|
38
|
-
|
|
34
|
+
error(path, ParseError.fieldDoesNotExist)
|
|
39
35
|
}
|
|
40
36
|
|
|
41
37
|
if ('customValidator' in fieldSchema) {
|
|
42
38
|
const customValidator = fieldSchema.customValidator
|
|
43
39
|
if (!(await customValidator(value, path, target))) {
|
|
44
|
-
|
|
40
|
+
error(path, ParseError.incorrectFormat)
|
|
45
41
|
}
|
|
46
42
|
}
|
|
47
43
|
|
|
@@ -62,15 +58,13 @@ export const setWalker = async (
|
|
|
62
58
|
if (value.$id) {
|
|
63
59
|
type = schema.prefixToTypeMapping[value.$id.slice(0, 2)]
|
|
64
60
|
if (!type) {
|
|
65
|
-
|
|
61
|
+
error([value.$id], ParseError.incorrectNodeType)
|
|
66
62
|
}
|
|
67
63
|
}
|
|
68
64
|
|
|
69
65
|
if (value.type) {
|
|
70
66
|
if (type && value.type !== type) {
|
|
71
|
-
|
|
72
|
-
`type from "$id" ${value.$id} does not match "type" field ${value.type}`
|
|
73
|
-
)
|
|
67
|
+
error([value.$id, value.type], ParseError.incorrectNodeType)
|
|
74
68
|
}
|
|
75
69
|
type = value.type
|
|
76
70
|
}
|
|
@@ -78,7 +72,7 @@ export const setWalker = async (
|
|
|
78
72
|
const schemaType = schema.types[type]
|
|
79
73
|
|
|
80
74
|
if (!schemaType) {
|
|
81
|
-
|
|
75
|
+
error([type], ParseError.incorrectNodeType)
|
|
82
76
|
}
|
|
83
77
|
|
|
84
78
|
const target: BasedSetTarget = {
|
|
@@ -95,14 +89,10 @@ export const setWalker = async (
|
|
|
95
89
|
const q: Promise<void>[] = []
|
|
96
90
|
|
|
97
91
|
for (const key in value) {
|
|
98
|
-
if (key[0]
|
|
99
|
-
console.info('key is operator', key)
|
|
100
|
-
} else {
|
|
92
|
+
if (key[0] !== '$') {
|
|
101
93
|
const fieldSchema = schemaType.fields[key]
|
|
102
94
|
if (!fieldSchema) {
|
|
103
|
-
|
|
104
|
-
`Field does not exist in schema "${key}" on type "${type}"`
|
|
105
|
-
)
|
|
95
|
+
error([key], ParseError.fieldDoesNotExist)
|
|
106
96
|
} else {
|
|
107
97
|
q.push(
|
|
108
98
|
fieldWalker(
|