@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,107 @@
|
|
|
1
|
+
import { Parser } from './types'
|
|
2
|
+
import { error, ParseError } from './error'
|
|
3
|
+
|
|
4
|
+
export const reference: Parser<'reference'> = async (
|
|
5
|
+
path,
|
|
6
|
+
value,
|
|
7
|
+
fieldSchema,
|
|
8
|
+
typeSchema,
|
|
9
|
+
target,
|
|
10
|
+
handlers
|
|
11
|
+
) => {
|
|
12
|
+
// $no root
|
|
13
|
+
// prob pass these as options
|
|
14
|
+
// value .default
|
|
15
|
+
// $value
|
|
16
|
+
|
|
17
|
+
if (typeof value !== 'string') {
|
|
18
|
+
error(path, ParseError.incorrectFormat)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if ('allowedTypes' in fieldSchema) {
|
|
22
|
+
const prefix = value.slice(0, 2)
|
|
23
|
+
const targetType = target.schema.prefixToTypeMapping[prefix]
|
|
24
|
+
if (!targetType) {
|
|
25
|
+
error(path, ParseError.referenceIsIncorrectType)
|
|
26
|
+
}
|
|
27
|
+
let typeMatches = false
|
|
28
|
+
for (const t of fieldSchema.allowedTypes) {
|
|
29
|
+
if (typeof t === 'string') {
|
|
30
|
+
if (t === targetType) {
|
|
31
|
+
typeMatches = true
|
|
32
|
+
break
|
|
33
|
+
}
|
|
34
|
+
} else {
|
|
35
|
+
if (t.type && t.type === targetType) {
|
|
36
|
+
typeMatches = true
|
|
37
|
+
if (t.$filter) {
|
|
38
|
+
if (!(await handlers.referenceFilterCondition(value, t.$filter))) {
|
|
39
|
+
error(path, ParseError.referenceIsIncorrectType)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
} else if (!t.type && t.$filter) {
|
|
43
|
+
if (!(await handlers.referenceFilterCondition(value, t.$filter))) {
|
|
44
|
+
error(path, ParseError.referenceIsIncorrectType)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (typeMatches === false) {
|
|
50
|
+
error(path, ParseError.referenceIsIncorrectType)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export const references: Parser<'references'> = async (
|
|
57
|
+
path,
|
|
58
|
+
value,
|
|
59
|
+
fieldSchema,
|
|
60
|
+
typeSchema,
|
|
61
|
+
target,
|
|
62
|
+
handlers
|
|
63
|
+
) => {
|
|
64
|
+
// default
|
|
65
|
+
// $no root
|
|
66
|
+
if (Array.isArray(value)) {
|
|
67
|
+
const handler = {
|
|
68
|
+
...handlers,
|
|
69
|
+
collect: () => {},
|
|
70
|
+
}
|
|
71
|
+
await Promise.all(
|
|
72
|
+
value.map((v, i) => {
|
|
73
|
+
return reference(
|
|
74
|
+
[...path, i],
|
|
75
|
+
v,
|
|
76
|
+
// not nice slow
|
|
77
|
+
{ ...fieldSchema, type: 'reference' },
|
|
78
|
+
typeSchema,
|
|
79
|
+
target,
|
|
80
|
+
handler
|
|
81
|
+
)
|
|
82
|
+
})
|
|
83
|
+
)
|
|
84
|
+
value = { $value: value }
|
|
85
|
+
} else if (typeof value === 'object') {
|
|
86
|
+
if (value.$add) {
|
|
87
|
+
const handler = {
|
|
88
|
+
...handlers,
|
|
89
|
+
collect: () => {},
|
|
90
|
+
}
|
|
91
|
+
await Promise.all(
|
|
92
|
+
value.$add.map((v, i) => {
|
|
93
|
+
return reference(
|
|
94
|
+
[...path, '$add', i],
|
|
95
|
+
v,
|
|
96
|
+
// not nice slow
|
|
97
|
+
{ ...fieldSchema, type: 'reference' },
|
|
98
|
+
typeSchema,
|
|
99
|
+
target,
|
|
100
|
+
handler
|
|
101
|
+
)
|
|
102
|
+
})
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
handlers.collect({ path, value, typeSchema, fieldSchema, target })
|
|
107
|
+
}
|
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,14 +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'
|
|
38
|
-
| '
|
|
37
|
+
| 'cardinality'
|
|
39
38
|
|
|
40
39
|
export const isCollection = (type: string): boolean => {
|
|
41
40
|
return type === 'array' || type === 'object' || type === 'record'
|
|
@@ -58,12 +57,17 @@ export type BasedSchemaContentMediaType =
|
|
|
58
57
|
| string
|
|
59
58
|
|
|
60
59
|
export type BasedSchemaFieldShared = {
|
|
60
|
+
hooks?:
|
|
61
|
+
| { interval?: number; hook: string }
|
|
62
|
+
| { interval?: number; hook: string }[]
|
|
63
|
+
|
|
61
64
|
type?: BasedSchemaFieldType
|
|
62
65
|
$id?: string
|
|
63
66
|
$schema?: string
|
|
64
67
|
isRequired?: boolean
|
|
65
68
|
title?: string
|
|
66
69
|
description?: string
|
|
70
|
+
index?: number // Determines the order of fields
|
|
67
71
|
readOnly?: boolean
|
|
68
72
|
writeOnly?: boolean
|
|
69
73
|
$comment?: string
|
|
@@ -79,25 +83,92 @@ export type BasedSchemaFieldShared = {
|
|
|
79
83
|
}
|
|
80
84
|
|
|
81
85
|
// -------------- Primitive ---------------
|
|
82
|
-
|
|
83
|
-
|
|
86
|
+
|
|
87
|
+
export type BasedSchemaStringShared = {
|
|
84
88
|
minLength?: number
|
|
85
89
|
maxLength?: number
|
|
86
90
|
contentMediaEncoding?: string // base64
|
|
87
91
|
contentMediaType?: BasedSchemaContentMediaType
|
|
88
92
|
pattern?: BasedSchemaPattern
|
|
89
|
-
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
|
+
|
|
90
160
|
// maybe add some more? e.g. phone
|
|
91
|
-
} & BasedSchemaFieldShared
|
|
161
|
+
} & BasedSchemaFieldShared &
|
|
162
|
+
BasedSchemaStringShared
|
|
92
163
|
|
|
93
164
|
export type BasedSchemaFieldEnum = {
|
|
94
|
-
enum: any[]
|
|
95
|
-
// important to type as well because we want to enum based on the type e.g. for references
|
|
165
|
+
enum: any[]
|
|
96
166
|
} & BasedSchemaFieldShared
|
|
97
167
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
168
|
+
// TODO: check if we want this later
|
|
169
|
+
// export type BasedSchemaFieldConst = {
|
|
170
|
+
// const: any
|
|
171
|
+
// } & BasedSchemaFieldShared
|
|
101
172
|
|
|
102
173
|
type NumberDefaults = {
|
|
103
174
|
multipleOf?: number
|
|
@@ -111,8 +182,8 @@ export type BasedSchemaFieldNumber = NumberDefaults & {
|
|
|
111
182
|
type: 'number'
|
|
112
183
|
}
|
|
113
184
|
|
|
114
|
-
export type
|
|
115
|
-
type: '
|
|
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: { $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;
|