@based/schema 0.0.4 → 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 -308
- 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/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 -478
- 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
package/src/set/rest.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Parser } from './types'
|
|
2
|
+
import { deepEqual } from '@saulx/utils'
|
|
3
|
+
import { hashObjectIgnoreKeyOrder, hash } from '@saulx/hash'
|
|
4
|
+
import { error, ParseError } from './error'
|
|
5
|
+
|
|
6
|
+
export const cardinality: Parser<'cardinality'> = async (
|
|
7
|
+
path,
|
|
8
|
+
value,
|
|
9
|
+
fieldSchema,
|
|
10
|
+
typeSchema,
|
|
11
|
+
target,
|
|
12
|
+
handlers
|
|
13
|
+
) => {
|
|
14
|
+
if (value && typeof value === 'object') {
|
|
15
|
+
value = hashObjectIgnoreKeyOrder(value).toString(16)
|
|
16
|
+
} else {
|
|
17
|
+
value = hash(value).toString(16)
|
|
18
|
+
}
|
|
19
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const boolean: Parser<'boolean'> = async (
|
|
23
|
+
path,
|
|
24
|
+
value,
|
|
25
|
+
fieldSchema,
|
|
26
|
+
typeSchema,
|
|
27
|
+
target,
|
|
28
|
+
handlers
|
|
29
|
+
) => {
|
|
30
|
+
// value .default
|
|
31
|
+
// $increment / $decrement
|
|
32
|
+
if (typeof value !== 'boolean') {
|
|
33
|
+
error(path, ParseError.incorrectFormat)
|
|
34
|
+
}
|
|
35
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const enumParser: Parser<'enum'> = async (
|
|
39
|
+
path,
|
|
40
|
+
value,
|
|
41
|
+
fieldSchema,
|
|
42
|
+
typeSchema,
|
|
43
|
+
target,
|
|
44
|
+
handlers
|
|
45
|
+
) => {
|
|
46
|
+
const enumValues = fieldSchema.enum
|
|
47
|
+
for (let i = 0; i < enumValues.length; i++) {
|
|
48
|
+
if (deepEqual(enumValues[i], value)) {
|
|
49
|
+
handlers.collect({ path, value: i, typeSchema, fieldSchema, target })
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
error(path, ParseError.incorrectFormat)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export const json: Parser<'json'> = async (
|
|
57
|
+
path,
|
|
58
|
+
value,
|
|
59
|
+
fieldSchema,
|
|
60
|
+
typeSchema,
|
|
61
|
+
target,
|
|
62
|
+
handlers
|
|
63
|
+
) => {
|
|
64
|
+
try {
|
|
65
|
+
const parsedValue = JSON.stringify(value)
|
|
66
|
+
handlers.collect({
|
|
67
|
+
path,
|
|
68
|
+
value: parsedValue,
|
|
69
|
+
typeSchema,
|
|
70
|
+
fieldSchema,
|
|
71
|
+
target,
|
|
72
|
+
})
|
|
73
|
+
} catch (err) {
|
|
74
|
+
throw err(path, ParseError.incorrectFormat)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { Parser } from './types'
|
|
2
|
+
import { error, ParseError } from './error'
|
|
3
|
+
import { BasedSchemaFieldString, BasedSchemaFieldText } from '../types'
|
|
4
|
+
import validators from 'validator'
|
|
5
|
+
|
|
6
|
+
const formatPatterns: Record<
|
|
7
|
+
BasedSchemaFieldString['format'],
|
|
8
|
+
(str: string) => boolean
|
|
9
|
+
> = {
|
|
10
|
+
email: validators.isEmail,
|
|
11
|
+
URL: validators.isURL,
|
|
12
|
+
MACAddress: validators.isMACAddress,
|
|
13
|
+
IP: validators.isIP,
|
|
14
|
+
IPRange: validators.isIPRange,
|
|
15
|
+
FQDN: validators.isFQDN,
|
|
16
|
+
IBAN: validators.isIBAN,
|
|
17
|
+
BIC: validators.isBIC,
|
|
18
|
+
alpha: validators.isAlpha,
|
|
19
|
+
alphaLocales: validators.isAlphaLocales,
|
|
20
|
+
alphanumeric: validators.isAlphanumeric,
|
|
21
|
+
alphanumericLocales: validators.isAlphanumericLocales,
|
|
22
|
+
passportNumber: validators.isPassportNumber,
|
|
23
|
+
port: validators.isPort,
|
|
24
|
+
lowercase: validators.isLowercase,
|
|
25
|
+
uppercase: validators.isUppercase,
|
|
26
|
+
ascii: validators.isAscii,
|
|
27
|
+
semVer: validators.isSemVer,
|
|
28
|
+
surrogatePair: validators.isSurrogatePair,
|
|
29
|
+
IMEI: validators.isIMEI,
|
|
30
|
+
hexadecimal: validators.isHexadecimal,
|
|
31
|
+
octal: validators.isOctal,
|
|
32
|
+
hexColor: validators.isHexColor,
|
|
33
|
+
rgbColor: validators.isRgbColor,
|
|
34
|
+
HSL: validators.isHSL,
|
|
35
|
+
ISRC: validators.isISRC,
|
|
36
|
+
MD5: validators.isMD5,
|
|
37
|
+
JWT: validators.isJWT,
|
|
38
|
+
UUID: validators.isUUID,
|
|
39
|
+
luhnNumber: validators.isLuhnNumber,
|
|
40
|
+
creditCard: validators.isCreditCard,
|
|
41
|
+
identityCard: validators.isIdentityCard,
|
|
42
|
+
EAN: validators.isEAN,
|
|
43
|
+
ISIN: validators.isISIN,
|
|
44
|
+
ISBN: validators.isISBN,
|
|
45
|
+
ISSN: validators.isISSN,
|
|
46
|
+
mobilePhone: validators.isMobilePhone,
|
|
47
|
+
mobilePhoneLocales: validators.isMobilePhoneLocales,
|
|
48
|
+
postalCode: validators.isPostalCode,
|
|
49
|
+
postalCodeLocales: validators.isPostalCodeLocales,
|
|
50
|
+
ethereumAddress: validators.isEthereumAddress,
|
|
51
|
+
currency: validators.isCurrency,
|
|
52
|
+
btcAddress: validators.isBtcAddress,
|
|
53
|
+
ISO6391: validators.isISO6391,
|
|
54
|
+
ISO8601: validators.isISO8601,
|
|
55
|
+
RFC3339: validators.isRFC3339,
|
|
56
|
+
ISO31661Alpha2: validators.isISO31661Alpha2,
|
|
57
|
+
ISO31661Alpha3: validators.isISO31661Alpha3,
|
|
58
|
+
ISO4217: validators.isISO4217,
|
|
59
|
+
base32: validators.isBase32,
|
|
60
|
+
base58: validators.isBase58,
|
|
61
|
+
base64: validators.isBase64,
|
|
62
|
+
dataURI: validators.isDataURI,
|
|
63
|
+
magnetURI: validators.isMagnetURI,
|
|
64
|
+
mimeType: validators.isMimeType,
|
|
65
|
+
latLong: validators.isLatLong,
|
|
66
|
+
slug: validators.isSlug,
|
|
67
|
+
strongPassword: validators.isStrongPassword,
|
|
68
|
+
taxID: validators.isTaxID,
|
|
69
|
+
licensePlate: validators.isLicensePlate,
|
|
70
|
+
VAT: validators.isVAT,
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const validate = (
|
|
74
|
+
path: (string | number)[],
|
|
75
|
+
value: string,
|
|
76
|
+
fieldSchema: BasedSchemaFieldText | BasedSchemaFieldString
|
|
77
|
+
) => {
|
|
78
|
+
if (typeof value !== 'string') {
|
|
79
|
+
error(path, ParseError.incorrectFormat)
|
|
80
|
+
}
|
|
81
|
+
if (fieldSchema.minLength && value.length < fieldSchema.minLength) {
|
|
82
|
+
error(path, ParseError.subceedsMinimum)
|
|
83
|
+
}
|
|
84
|
+
if (fieldSchema.maxLength && value.length > fieldSchema.maxLength) {
|
|
85
|
+
error(path, ParseError.exceedsMaximum)
|
|
86
|
+
}
|
|
87
|
+
if (fieldSchema.pattern) {
|
|
88
|
+
const re = new RegExp(fieldSchema.pattern)
|
|
89
|
+
if (!re.test(value)) {
|
|
90
|
+
error(path, ParseError.incorrectFormat)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (fieldSchema.format && !formatPatterns[fieldSchema.format](value)) {
|
|
94
|
+
error(path, ParseError.incorrectFormat)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export const string: Parser<'string'> = async (
|
|
99
|
+
path,
|
|
100
|
+
value,
|
|
101
|
+
fieldSchema,
|
|
102
|
+
typeSchema,
|
|
103
|
+
target,
|
|
104
|
+
handlers
|
|
105
|
+
) => {
|
|
106
|
+
validate(path, value, fieldSchema)
|
|
107
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export const text: Parser<'text'> = async (
|
|
111
|
+
path,
|
|
112
|
+
value,
|
|
113
|
+
fieldSchema,
|
|
114
|
+
typeSchema,
|
|
115
|
+
target,
|
|
116
|
+
handlers
|
|
117
|
+
) => {
|
|
118
|
+
const valueType = typeof value
|
|
119
|
+
if (target.$language && valueType === 'string') {
|
|
120
|
+
validate(path, value, fieldSchema)
|
|
121
|
+
handlers.collect({
|
|
122
|
+
path,
|
|
123
|
+
value: { [target.$language]: value },
|
|
124
|
+
typeSchema,
|
|
125
|
+
fieldSchema,
|
|
126
|
+
target,
|
|
127
|
+
})
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (valueType !== 'object') {
|
|
132
|
+
error(path, ParseError.incorrectFormat)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
for (const key in value) {
|
|
136
|
+
const newPath = [...path, key]
|
|
137
|
+
|
|
138
|
+
if (typeof value[key] === 'object' && value[key].$delete === true) {
|
|
139
|
+
handlers.collect({
|
|
140
|
+
path: newPath,
|
|
141
|
+
value: null,
|
|
142
|
+
typeSchema,
|
|
143
|
+
fieldSchema,
|
|
144
|
+
target,
|
|
145
|
+
})
|
|
146
|
+
continue
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
validate(newPath, value[key], fieldSchema)
|
|
150
|
+
|
|
151
|
+
handlers.collect({
|
|
152
|
+
path: newPath,
|
|
153
|
+
value: value[key],
|
|
154
|
+
typeSchema,
|
|
155
|
+
fieldSchema,
|
|
156
|
+
target,
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
}
|
package/src/set/types.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BasedSchemaType,
|
|
3
|
+
BasedSetHandlers,
|
|
4
|
+
BasedSetTarget,
|
|
5
|
+
BasedSchemaFields,
|
|
6
|
+
} from '../types'
|
|
7
|
+
|
|
8
|
+
export type Parser<K extends keyof BasedSchemaFields> = (
|
|
9
|
+
path: (string | number)[],
|
|
10
|
+
value: any,
|
|
11
|
+
fieldSchema: BasedSchemaFields[K],
|
|
12
|
+
typeSchema: BasedSchemaType,
|
|
13
|
+
target: BasedSetTarget,
|
|
14
|
+
handlers: BasedSetHandlers
|
|
15
|
+
) => Promise<void>
|
|
16
|
+
|
|
17
|
+
export type Parsers = {
|
|
18
|
+
[Key in keyof BasedSchemaFields]: Parser<Key>
|
|
19
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -28,13 +28,13 @@ export type BasedSchemaFieldType =
|
|
|
28
28
|
| 'string'
|
|
29
29
|
| 'boolean'
|
|
30
30
|
| 'number'
|
|
31
|
-
| 'float'
|
|
32
31
|
| 'json'
|
|
33
32
|
| 'integer'
|
|
34
33
|
| 'timestamp'
|
|
35
34
|
| 'reference'
|
|
36
35
|
| 'references'
|
|
37
36
|
| 'text'
|
|
37
|
+
| 'cardinality'
|
|
38
38
|
|
|
39
39
|
export const isCollection = (type: string): boolean => {
|
|
40
40
|
return type === 'array' || type === 'object' || type === 'record'
|
|
@@ -57,12 +57,17 @@ export type BasedSchemaContentMediaType =
|
|
|
57
57
|
| string
|
|
58
58
|
|
|
59
59
|
export type BasedSchemaFieldShared = {
|
|
60
|
+
hooks?:
|
|
61
|
+
| { interval?: number; hook: string }
|
|
62
|
+
| { interval?: number; hook: string }[]
|
|
63
|
+
|
|
60
64
|
type?: BasedSchemaFieldType
|
|
61
65
|
$id?: string
|
|
62
66
|
$schema?: string
|
|
63
67
|
isRequired?: boolean
|
|
64
68
|
title?: string
|
|
65
69
|
description?: string
|
|
70
|
+
index?: number // Determines the order of fields
|
|
66
71
|
readOnly?: boolean
|
|
67
72
|
writeOnly?: boolean
|
|
68
73
|
$comment?: string
|
|
@@ -78,25 +83,92 @@ export type BasedSchemaFieldShared = {
|
|
|
78
83
|
}
|
|
79
84
|
|
|
80
85
|
// -------------- Primitive ---------------
|
|
81
|
-
|
|
82
|
-
|
|
86
|
+
|
|
87
|
+
export type BasedSchemaStringShared = {
|
|
83
88
|
minLength?: number
|
|
84
89
|
maxLength?: number
|
|
85
90
|
contentMediaEncoding?: string // base64
|
|
86
91
|
contentMediaType?: BasedSchemaContentMediaType
|
|
87
92
|
pattern?: BasedSchemaPattern
|
|
88
|
-
format?:
|
|
93
|
+
format?:
|
|
94
|
+
| 'email'
|
|
95
|
+
| 'URL'
|
|
96
|
+
| 'MACAddress'
|
|
97
|
+
| 'IP'
|
|
98
|
+
| 'IPRange'
|
|
99
|
+
| 'FQDN'
|
|
100
|
+
| 'IBAN'
|
|
101
|
+
| 'BIC'
|
|
102
|
+
| 'alpha'
|
|
103
|
+
| 'alphaLocales'
|
|
104
|
+
| 'alphanumeric'
|
|
105
|
+
| 'alphanumericLocales'
|
|
106
|
+
| 'passportNumber'
|
|
107
|
+
| 'port'
|
|
108
|
+
| 'lowercase'
|
|
109
|
+
| 'uppercase'
|
|
110
|
+
| 'ascii'
|
|
111
|
+
| 'semVer'
|
|
112
|
+
| 'surrogatePair'
|
|
113
|
+
| 'IMEI'
|
|
114
|
+
| 'hexadecimal'
|
|
115
|
+
| 'octal'
|
|
116
|
+
| 'hexColor'
|
|
117
|
+
| 'rgbColor'
|
|
118
|
+
| 'HSL'
|
|
119
|
+
| 'ISRC'
|
|
120
|
+
| 'MD5'
|
|
121
|
+
| 'JWT'
|
|
122
|
+
| 'UUID'
|
|
123
|
+
| 'luhnNumber'
|
|
124
|
+
| 'creditCard'
|
|
125
|
+
| 'identityCard'
|
|
126
|
+
| 'EAN'
|
|
127
|
+
| 'ISIN'
|
|
128
|
+
| 'ISBN'
|
|
129
|
+
| 'ISSN'
|
|
130
|
+
| 'mobilePhone'
|
|
131
|
+
| 'mobilePhoneLocales'
|
|
132
|
+
| 'postalCode'
|
|
133
|
+
| 'postalCodeLocales'
|
|
134
|
+
| 'ethereumAddress'
|
|
135
|
+
| 'currency'
|
|
136
|
+
| 'btcAddress'
|
|
137
|
+
| 'ISO6391'
|
|
138
|
+
| 'ISO8601'
|
|
139
|
+
| 'RFC3339'
|
|
140
|
+
| 'ISO31661Alpha2'
|
|
141
|
+
| 'ISO31661Alpha3'
|
|
142
|
+
| 'ISO4217'
|
|
143
|
+
| 'base32'
|
|
144
|
+
| 'base58'
|
|
145
|
+
| 'base64'
|
|
146
|
+
| 'dataURI'
|
|
147
|
+
| 'magnetURI'
|
|
148
|
+
| 'mimeType'
|
|
149
|
+
| 'latLong'
|
|
150
|
+
| 'slug'
|
|
151
|
+
| 'strongPassword'
|
|
152
|
+
| 'taxID'
|
|
153
|
+
| 'licensePlate'
|
|
154
|
+
| 'VAT'
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export type BasedSchemaFieldString = {
|
|
158
|
+
type: 'string'
|
|
159
|
+
|
|
89
160
|
// maybe add some more? e.g. phone
|
|
90
|
-
} & BasedSchemaFieldShared
|
|
161
|
+
} & BasedSchemaFieldShared &
|
|
162
|
+
BasedSchemaStringShared
|
|
91
163
|
|
|
92
164
|
export type BasedSchemaFieldEnum = {
|
|
93
|
-
enum: any[]
|
|
94
|
-
// important to type as well because we want to enum based on the type e.g. for references
|
|
165
|
+
enum: any[]
|
|
95
166
|
} & BasedSchemaFieldShared
|
|
96
167
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
168
|
+
// TODO: check if we want this later
|
|
169
|
+
// export type BasedSchemaFieldConst = {
|
|
170
|
+
// const: any
|
|
171
|
+
// } & BasedSchemaFieldShared
|
|
100
172
|
|
|
101
173
|
type NumberDefaults = {
|
|
102
174
|
multipleOf?: number
|
|
@@ -110,9 +182,8 @@ export type BasedSchemaFieldNumber = NumberDefaults & {
|
|
|
110
182
|
type: 'number'
|
|
111
183
|
}
|
|
112
184
|
|
|
113
|
-
export type
|
|
114
|
-
type: '
|
|
115
|
-
// allow any (objects become hashes)
|
|
185
|
+
export type BasedSchemaFieldCardinality = {
|
|
186
|
+
type: 'cardinality'
|
|
116
187
|
}
|
|
117
188
|
|
|
118
189
|
export type BasedSchemaFieldInteger = NumberDefaults & {
|
|
@@ -146,12 +217,8 @@ export type BasedSchemaFieldPrimitive =
|
|
|
146
217
|
export type BasedSchemaFieldText = {
|
|
147
218
|
type: 'text'
|
|
148
219
|
required?: BasedSchemaLanguage[]
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
maxLength?: number
|
|
152
|
-
contentMediaEncoding?: string // base64
|
|
153
|
-
pattern?: BasedSchemaPattern
|
|
154
|
-
} & BasedSchemaFieldShared
|
|
220
|
+
} & BasedSchemaFieldShared &
|
|
221
|
+
BasedSchemaStringShared
|
|
155
222
|
|
|
156
223
|
export type BasedSchemaFieldObject = {
|
|
157
224
|
type: 'object'
|
|
@@ -208,13 +275,31 @@ export type BasedSchemaField =
|
|
|
208
275
|
| BasedSchemaFieldPrimitive
|
|
209
276
|
| BasedSchemaFieldReference
|
|
210
277
|
| BasedSchemaFieldReferences
|
|
211
|
-
|
|
|
278
|
+
| BasedSchemaFieldCardinality
|
|
212
279
|
| {
|
|
213
280
|
type?: BasedSchemaFieldType
|
|
214
281
|
isRequired?: boolean // our own
|
|
215
282
|
$ref: string // to mimic json schema will just load it in place (so only for setting)
|
|
216
283
|
}
|
|
217
284
|
|
|
285
|
+
export type BasedSchemaFields = {
|
|
286
|
+
enum: BasedSchemaFieldEnum
|
|
287
|
+
array: BasedSchemaFieldArray
|
|
288
|
+
object: BasedSchemaFieldObject
|
|
289
|
+
set: BasedSchemaFieldSet
|
|
290
|
+
record: BasedSchemaFieldRecord
|
|
291
|
+
string: BasedSchemaFieldString
|
|
292
|
+
boolean: BasedSchemaFieldBoolean
|
|
293
|
+
number: BasedSchemaFieldNumber
|
|
294
|
+
json: BasedSchemaFieldJSON
|
|
295
|
+
integer: BasedSchemaFieldInteger
|
|
296
|
+
timestamp: BasedSchemaFieldTimeStamp
|
|
297
|
+
reference: BasedSchemaFieldReference
|
|
298
|
+
references: BasedSchemaFieldReferences
|
|
299
|
+
text: BasedSchemaFieldText
|
|
300
|
+
cardinality: BasedSchemaFieldCardinality
|
|
301
|
+
}
|
|
302
|
+
|
|
218
303
|
export type BasedSchemaType = {
|
|
219
304
|
fields: {
|
|
220
305
|
[name: string]: BasedSchemaField
|
package/test/setWalker.ts
CHANGED
|
@@ -6,6 +6,9 @@ const schema: BasedSchema = {
|
|
|
6
6
|
bla: {
|
|
7
7
|
prefix: 'bl',
|
|
8
8
|
fields: {
|
|
9
|
+
visits: {
|
|
10
|
+
type: 'cardinality',
|
|
11
|
+
},
|
|
9
12
|
snurp: {
|
|
10
13
|
type: 'array',
|
|
11
14
|
values: {
|
|
@@ -141,6 +144,11 @@ test.serial('collect correctly', async (t) => {
|
|
|
141
144
|
schema,
|
|
142
145
|
{
|
|
143
146
|
$id: 'bl1',
|
|
147
|
+
visits: {
|
|
148
|
+
flap: true,
|
|
149
|
+
snurp: false,
|
|
150
|
+
ua: '123435',
|
|
151
|
+
},
|
|
144
152
|
bla: false,
|
|
145
153
|
time: now, // do more later
|
|
146
154
|
setje: [1, 2, 3],
|
|
@@ -194,6 +202,7 @@ test.serial('collect correctly', async (t) => {
|
|
|
194
202
|
)
|
|
195
203
|
|
|
196
204
|
const result = [
|
|
205
|
+
{ path: ['visits'], value: '3a9009740ee' },
|
|
197
206
|
{ path: ['bla'], value: false },
|
|
198
207
|
{ path: ['time'], value: now },
|
|
199
208
|
{ path: ['form', 'lastName'], value: 'de beer' },
|
|
@@ -201,21 +210,21 @@ test.serial('collect correctly', async (t) => {
|
|
|
201
210
|
{ path: ['form', 'snurp'], value: 'blx12' },
|
|
202
211
|
{ path: ['form', 'things'], value: 2 },
|
|
203
212
|
{ path: ['form', 'password'], value: 'mypassword!' },
|
|
213
|
+
{ path: ['snurp', 0, 'x', 0], value: 1 },
|
|
214
|
+
{ path: ['snurp', 0, 'x', 1], value: 2 },
|
|
215
|
+
{ path: ['snurp', 0, 'x', 2], value: 3 },
|
|
216
|
+
{ path: ['form', 'bla'], value: { $value: ['bl123', 'bl234'] } },
|
|
217
|
+
{ path: ['form', 'blab'], value: { $add: ['bl456'] } },
|
|
204
218
|
{
|
|
205
219
|
path: ['snurpArray'],
|
|
206
220
|
value: { $assign: { $idx: 0, $value: 100 } },
|
|
207
221
|
},
|
|
222
|
+
{ path: ['setje'], value: { $value: [1, 2, 3] } },
|
|
223
|
+
{ path: ['form', 'blub'], value: { $value: ['x'] } },
|
|
208
224
|
{
|
|
209
225
|
path: ['specialArray'],
|
|
210
226
|
value: { $insert: { $value: ['a', 'b', 'c'], $idx: 0 } },
|
|
211
227
|
},
|
|
212
|
-
{ path: ['snurp', 0, 'x', 0], value: 1 },
|
|
213
|
-
{ path: ['snurp', 0, 'x', 1], value: 2 },
|
|
214
|
-
{ path: ['snurp', 0, 'x', 2], value: 3 },
|
|
215
|
-
{ path: ['form', 'bla'], value: ['bl123', 'bl234'] },
|
|
216
|
-
{ path: ['form', 'blab'], value: { $add: ['bl456'] } },
|
|
217
|
-
{ path: ['setje'], value: { $value: [1, 2, 3] } },
|
|
218
|
-
{ path: ['form', 'blub'], value: { $value: ['x'] } },
|
|
219
228
|
]
|
|
220
229
|
|
|
221
230
|
t.deepEqual(results, result)
|
package/dist/deepPartial.d.ts
DELETED
|
File without changes
|
package/dist/deepPartial.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"deepPartial.js","sourceRoot":"","sources":["../src/deepPartial.ts"],"names":[],"mappings":""}
|
package/dist/set/enum.d.ts
DELETED
package/dist/set/enum.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
() => {
|
|
4
|
-
if ('enum' in fieldSchema) {
|
|
5
|
-
const enumValues = fieldSchema.enum;
|
|
6
|
-
for (let i = 0; i < enumValues.length; i++) {
|
|
7
|
-
if (deepEqual(enumValues[i], value)) {
|
|
8
|
-
collect(path, i, typeSchema, fieldSchema, target);
|
|
9
|
-
return;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
throw createError(path, target.type, 'enum', value);
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
//# sourceMappingURL=enum.js.map
|
package/dist/set/enum.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"enum.js","sourceRoot":"","sources":["../../src/set/enum.ts"],"names":[],"mappings":";;AAAoB,GAAG,EAAE;IAErB,IAAI,MAAM,IAAI,WAAW,EAAE;QACvB,MAAM,UAAU,GAAG,WAAW,CAAC,IAAI,CAAA;QACnC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YAC1C,IAAI,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE;gBACnC,OAAO,CAAC,IAAI,EAAE,CAAC,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,CAAC,CAAA;gBACjD,OAAM;aACP;SACF;QACD,MAAM,WAAW,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,CAAA;KACpD;AACP,CAAC,CAAA"}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { BasedSchemaField, BasedSchemaType, BasedSetTarget } from '../types';
|
|
2
|
-
declare const fieldValidator: {
|
|
3
|
-
[key: string]: (path: (string | number)[], value: any, fieldSchema: BasedSchemaField, typeSchema: BasedSchemaType, target: BasedSetTarget, collect: (path: (string | number)[], value: any, // parsed value
|
|
4
|
-
typeSchema: BasedSchemaType, fieldSchema: BasedSchemaField, target: BasedSetTarget) => void) => void;
|
|
5
|
-
};
|
|
6
|
-
export default fieldValidator;
|
|
@@ -1,144 +0,0 @@
|
|
|
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 fieldValidator = {
|
|
7
|
-
enum: (path, value, fieldSchema, typeSchema, target, collect) => {
|
|
8
|
-
// @ts-ignore
|
|
9
|
-
const enumValues = fieldSchema.enum;
|
|
10
|
-
for (let i = 0; i < enumValues.length; i++) {
|
|
11
|
-
if ((0, utils_1.deepEqual)(enumValues[i], value)) {
|
|
12
|
-
collect(path, i, typeSchema, fieldSchema, target);
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
throw (0, handleError_1.createError)(path, target.type, 'enum', value);
|
|
17
|
-
},
|
|
18
|
-
array: (path, value, fieldSchema, typeSchema, target, collect) => {
|
|
19
|
-
// TODO: ADD OPERATORS
|
|
20
|
-
const isArray = Array.isArray(value);
|
|
21
|
-
if (!isArray) {
|
|
22
|
-
throw (0, handleError_1.createError)(path, target.type, 'array', value);
|
|
23
|
-
}
|
|
24
|
-
for (let i = 0; i < value.length; i++) {
|
|
25
|
-
(0, _1.fieldWalker)([...path, i], value[i],
|
|
26
|
-
// @ts-ignore
|
|
27
|
-
fieldSchema.values, typeSchema, target, collect);
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
object: (path, value, fieldSchema, typeSchema, target, collect) => {
|
|
31
|
-
if (typeof value !== 'object') {
|
|
32
|
-
throw (0, handleError_1.createError)(path, target.type, 'object', value);
|
|
33
|
-
}
|
|
34
|
-
const isArray = Array.isArray(value);
|
|
35
|
-
if (isArray) {
|
|
36
|
-
throw (0, handleError_1.createError)(path, target.type, 'object', value);
|
|
37
|
-
}
|
|
38
|
-
for (const key in value) {
|
|
39
|
-
// @ts-ignore
|
|
40
|
-
const propDef = fieldSchema.properties[key];
|
|
41
|
-
if (!propDef) {
|
|
42
|
-
throw (0, handleError_1.createError)([...path, key], target.type, 'object', value[key], key);
|
|
43
|
-
}
|
|
44
|
-
(0, _1.fieldWalker)([...path, key], value[key], propDef, typeSchema, target, collect);
|
|
45
|
-
}
|
|
46
|
-
},
|
|
47
|
-
set: (path, value, fieldSchema, typeSchema, target, collect) => {
|
|
48
|
-
if (Array.isArray(value)) {
|
|
49
|
-
const parsedArray = [];
|
|
50
|
-
// @ts-ignore
|
|
51
|
-
const fieldDef = fieldSchema.items;
|
|
52
|
-
for (let i = 0; i < value.length; i++) {
|
|
53
|
-
(0, _1.fieldWalker)([...path, i], value[i], fieldDef, typeSchema, target, (path, value) => {
|
|
54
|
-
parsedArray.push(value);
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
collect(path, parsedArray, typeSchema, fieldSchema, target);
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
// TODO PARSE IF VALID
|
|
61
|
-
// $add / $remove
|
|
62
|
-
collect(path, value, typeSchema, fieldSchema, target);
|
|
63
|
-
}
|
|
64
|
-
},
|
|
65
|
-
json: (path, value, fieldSchema, typeSchema, target, collect) => {
|
|
66
|
-
try {
|
|
67
|
-
const parsedValue = JSON.stringify(value);
|
|
68
|
-
collect(path, parsedValue, typeSchema, fieldSchema, target);
|
|
69
|
-
}
|
|
70
|
-
catch (err) {
|
|
71
|
-
throw (0, handleError_1.createError)(path, target.type, 'json', value);
|
|
72
|
-
}
|
|
73
|
-
},
|
|
74
|
-
number: (path, value, fieldSchema, typeSchema, target, collect) => {
|
|
75
|
-
if (typeof value !== 'number') {
|
|
76
|
-
throw (0, handleError_1.createError)(path, target.type, 'number', value);
|
|
77
|
-
}
|
|
78
|
-
collect(path, value, typeSchema, fieldSchema, target);
|
|
79
|
-
},
|
|
80
|
-
integer: (path, value, fieldSchema, typeSchema, target, collect) => {
|
|
81
|
-
if (typeof value !== 'number' || value - Math.floor(value) !== 0) {
|
|
82
|
-
throw (0, handleError_1.createError)(path, target.type, 'integer', value);
|
|
83
|
-
}
|
|
84
|
-
collect(path, value, typeSchema, fieldSchema, target);
|
|
85
|
-
},
|
|
86
|
-
string: (path, value, fieldSchema, typeSchema, target, collect) => {
|
|
87
|
-
if (typeof value !== 'string') {
|
|
88
|
-
throw (0, handleError_1.createError)(path, target.type, 'string', value);
|
|
89
|
-
}
|
|
90
|
-
// @ts-ignore
|
|
91
|
-
if (fieldSchema.minLength && value.length < fieldSchema.minLength) {
|
|
92
|
-
throw (0, handleError_1.createError)(path, target.type, 'string', value);
|
|
93
|
-
}
|
|
94
|
-
// @ts-ignore
|
|
95
|
-
if (fieldSchema.maxLength && value.length > fieldSchema.maxLength) {
|
|
96
|
-
throw (0, handleError_1.createError)(path, target.type, 'string', value);
|
|
97
|
-
}
|
|
98
|
-
collect(path, value, typeSchema, fieldSchema, target);
|
|
99
|
-
},
|
|
100
|
-
text: (path, value, fieldSchema, typeSchema, target, collect) => {
|
|
101
|
-
const valueType = typeof value;
|
|
102
|
-
if (target.$language && valueType === 'string') {
|
|
103
|
-
// @ts-ignore
|
|
104
|
-
if (fieldSchema.minLength && value.length < fieldSchema.minLength) {
|
|
105
|
-
throw (0, handleError_1.createError)(path, target.type, 'text', value);
|
|
106
|
-
}
|
|
107
|
-
// @ts-ignore
|
|
108
|
-
if (fieldSchema.maxLength && value.length > fieldSchema.maxLength) {
|
|
109
|
-
throw (0, handleError_1.createError)(path, target.type, 'text', value);
|
|
110
|
-
}
|
|
111
|
-
collect(path, { [target.$language]: value }, typeSchema, fieldSchema, target);
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
if (valueType !== 'object') {
|
|
115
|
-
throw (0, handleError_1.createError)(path, target.type, 'text', value);
|
|
116
|
-
}
|
|
117
|
-
for (const key in value) {
|
|
118
|
-
// @ts-ignore
|
|
119
|
-
if (fieldSchema.minLength && value[key].length < fieldSchema.minLength) {
|
|
120
|
-
throw (0, handleError_1.createError)([...path, key], target.type, 'text', value);
|
|
121
|
-
}
|
|
122
|
-
// @ts-ignore
|
|
123
|
-
if (fieldSchema.maxLength && value[key].length > fieldSchema.maxLength) {
|
|
124
|
-
throw (0, handleError_1.createError)([...path, key], target.type, 'text', value);
|
|
125
|
-
}
|
|
126
|
-
if (typeof value[key] === 'object' && value[key].$delete === true) {
|
|
127
|
-
collect([...path, key], null, typeSchema, fieldSchema, target);
|
|
128
|
-
continue;
|
|
129
|
-
}
|
|
130
|
-
if (typeof value[key] !== 'string') {
|
|
131
|
-
throw (0, handleError_1.createError)([...path, key], target.type, 'text', value);
|
|
132
|
-
}
|
|
133
|
-
collect([...path, key], value[key], typeSchema, fieldSchema, target);
|
|
134
|
-
}
|
|
135
|
-
},
|
|
136
|
-
references: (path, value, fieldSchema, typeSchema, target, collect) => {
|
|
137
|
-
collect(path, value, typeSchema, fieldSchema, target);
|
|
138
|
-
},
|
|
139
|
-
reference: (path, value, fieldSchema, typeSchema, target, collect) => {
|
|
140
|
-
collect(path, value, typeSchema, fieldSchema, target);
|
|
141
|
-
},
|
|
142
|
-
};
|
|
143
|
-
exports.default = fieldValidator;
|
|
144
|
-
//# sourceMappingURL=fieldValidator.js.map
|