@palmares/schemas 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/.turbo/turbo-build$colon$watch.log +424 -0
- package/.turbo/turbo-build.log +13 -0
- package/.turbo/turbo-build:watch.log +26 -0
- package/CHANGELOG.md +11 -0
- package/LICENSE +21 -0
- package/dist/cjs/src/adapter/fields/array.js +157 -0
- package/dist/cjs/src/adapter/fields/boolean.js +167 -0
- package/dist/cjs/src/adapter/fields/datetime.js +167 -0
- package/dist/cjs/src/adapter/fields/index.js +311 -0
- package/dist/cjs/src/adapter/fields/number.js +167 -0
- package/dist/cjs/src/adapter/fields/object.js +167 -0
- package/dist/cjs/src/adapter/fields/string.js +167 -0
- package/dist/cjs/src/adapter/fields/union.js +167 -0
- package/dist/cjs/src/adapter/index.js +198 -0
- package/dist/cjs/src/adapter/types.js +4 -0
- package/dist/cjs/src/compile.js +262 -0
- package/dist/cjs/src/conf.js +27 -0
- package/dist/cjs/src/constants.js +42 -0
- package/dist/cjs/src/domain.js +12 -0
- package/dist/cjs/src/exceptions.js +168 -0
- package/dist/cjs/src/index.js +365 -0
- package/dist/cjs/src/model.js +628 -0
- package/dist/cjs/src/parsers/convert-from-number.js +20 -0
- package/dist/cjs/src/parsers/convert-from-string.js +24 -0
- package/dist/cjs/src/parsers/index.js +25 -0
- package/dist/cjs/src/schema/array.js +890 -0
- package/dist/cjs/src/schema/boolean.js +826 -0
- package/dist/cjs/src/schema/datetime.js +778 -0
- package/dist/cjs/src/schema/index.js +17 -0
- package/dist/cjs/src/schema/number.js +960 -0
- package/dist/cjs/src/schema/object.js +999 -0
- package/dist/cjs/src/schema/schema.js +1788 -0
- package/dist/cjs/src/schema/string.js +948 -0
- package/dist/cjs/src/schema/types.js +4 -0
- package/dist/cjs/src/schema/union.js +952 -0
- package/dist/cjs/src/types.js +4 -0
- package/dist/cjs/src/utils.js +627 -0
- package/dist/cjs/src/validators/array.js +457 -0
- package/dist/cjs/src/validators/boolean.js +199 -0
- package/dist/cjs/src/validators/datetime.js +287 -0
- package/dist/cjs/src/validators/number.js +403 -0
- package/dist/cjs/src/validators/object.js +290 -0
- package/dist/cjs/src/validators/schema.js +318 -0
- package/dist/cjs/src/validators/string.js +439 -0
- package/dist/cjs/src/validators/types.js +4 -0
- package/dist/cjs/src/validators/union.js +232 -0
- package/dist/cjs/src/validators/utils.js +426 -0
- package/dist/cjs/tsconfig.types.tsbuildinfo +1 -0
- package/dist/cjs/types/adapter/fields/array.d.ts +20 -0
- package/dist/cjs/types/adapter/fields/array.d.ts.map +1 -0
- package/dist/cjs/types/adapter/fields/boolean.d.ts +25 -0
- package/dist/cjs/types/adapter/fields/boolean.d.ts.map +1 -0
- package/dist/cjs/types/adapter/fields/datetime.d.ts +25 -0
- package/dist/cjs/types/adapter/fields/datetime.d.ts.map +1 -0
- package/dist/cjs/types/adapter/fields/index.d.ts +31 -0
- package/dist/cjs/types/adapter/fields/index.d.ts.map +1 -0
- package/dist/cjs/types/adapter/fields/number.d.ts +25 -0
- package/dist/cjs/types/adapter/fields/number.d.ts.map +1 -0
- package/dist/cjs/types/adapter/fields/object.d.ts +25 -0
- package/dist/cjs/types/adapter/fields/object.d.ts.map +1 -0
- package/dist/cjs/types/adapter/fields/string.d.ts +25 -0
- package/dist/cjs/types/adapter/fields/string.d.ts.map +1 -0
- package/dist/cjs/types/adapter/fields/union.d.ts +25 -0
- package/dist/cjs/types/adapter/fields/union.d.ts.map +1 -0
- package/dist/cjs/types/adapter/index.d.ts +25 -0
- package/dist/cjs/types/adapter/index.d.ts.map +1 -0
- package/dist/cjs/types/adapter/types.d.ts +144 -0
- package/dist/cjs/types/adapter/types.d.ts.map +1 -0
- package/dist/cjs/types/compile.d.ts +3 -0
- package/dist/cjs/types/compile.d.ts.map +1 -0
- package/dist/cjs/types/conf.d.ts +16 -0
- package/dist/cjs/types/conf.d.ts.map +1 -0
- package/dist/cjs/types/constants.d.ts +6 -0
- package/dist/cjs/types/constants.d.ts.map +1 -0
- package/dist/cjs/types/domain.d.ts +21 -0
- package/dist/cjs/types/domain.d.ts.map +1 -0
- package/dist/cjs/types/exceptions.d.ts +13 -0
- package/dist/cjs/types/exceptions.d.ts.map +1 -0
- package/dist/cjs/types/index.d.ts +240 -0
- package/dist/cjs/types/index.d.ts.map +1 -0
- package/dist/cjs/types/model.d.ts +136 -0
- package/dist/cjs/types/model.d.ts.map +1 -0
- package/dist/cjs/types/parsers/convert-from-number.d.ts +15 -0
- package/dist/cjs/types/parsers/convert-from-number.d.ts.map +1 -0
- package/dist/cjs/types/parsers/convert-from-string.d.ts +9 -0
- package/dist/cjs/types/parsers/convert-from-string.d.ts.map +1 -0
- package/dist/cjs/types/parsers/index.d.ts +3 -0
- package/dist/cjs/types/parsers/index.d.ts.map +1 -0
- package/dist/cjs/types/schema/array.d.ts +429 -0
- package/dist/cjs/types/schema/array.d.ts.map +1 -0
- package/dist/cjs/types/schema/boolean.d.ts +501 -0
- package/dist/cjs/types/schema/boolean.d.ts.map +1 -0
- package/dist/cjs/types/schema/datetime.d.ts +474 -0
- package/dist/cjs/types/schema/datetime.d.ts.map +1 -0
- package/dist/cjs/types/schema/index.d.ts +4 -0
- package/dist/cjs/types/schema/index.d.ts.map +1 -0
- package/dist/cjs/types/schema/number.d.ts +667 -0
- package/dist/cjs/types/schema/number.d.ts.map +1 -0
- package/dist/cjs/types/schema/object.d.ts +450 -0
- package/dist/cjs/types/schema/object.d.ts.map +1 -0
- package/dist/cjs/types/schema/schema.d.ts +646 -0
- package/dist/cjs/types/schema/schema.d.ts.map +1 -0
- package/dist/cjs/types/schema/string.d.ts +606 -0
- package/dist/cjs/types/schema/string.d.ts.map +1 -0
- package/dist/cjs/types/schema/types.d.ts +70 -0
- package/dist/cjs/types/schema/types.d.ts.map +1 -0
- package/dist/cjs/types/schema/union.d.ts +388 -0
- package/dist/cjs/types/schema/union.d.ts.map +1 -0
- package/dist/cjs/types/types.d.ts +11 -0
- package/dist/cjs/types/types.d.ts.map +1 -0
- package/dist/cjs/types/utils.d.ts +79 -0
- package/dist/cjs/types/utils.d.ts.map +1 -0
- package/dist/cjs/types/validators/array.d.ts +8 -0
- package/dist/cjs/types/validators/array.d.ts.map +1 -0
- package/dist/cjs/types/validators/boolean.d.ts +4 -0
- package/dist/cjs/types/validators/boolean.d.ts.map +1 -0
- package/dist/cjs/types/validators/datetime.d.ts +7 -0
- package/dist/cjs/types/validators/datetime.d.ts.map +1 -0
- package/dist/cjs/types/validators/number.d.ts +10 -0
- package/dist/cjs/types/validators/number.d.ts.map +1 -0
- package/dist/cjs/types/validators/object.d.ts +6 -0
- package/dist/cjs/types/validators/object.d.ts.map +1 -0
- package/dist/cjs/types/validators/schema.d.ts +10 -0
- package/dist/cjs/types/validators/schema.d.ts.map +1 -0
- package/dist/cjs/types/validators/string.d.ts +12 -0
- package/dist/cjs/types/validators/string.d.ts.map +1 -0
- package/dist/cjs/types/validators/types.d.ts +2 -0
- package/dist/cjs/types/validators/types.d.ts.map +1 -0
- package/dist/cjs/types/validators/union.d.ts +4 -0
- package/dist/cjs/types/validators/union.d.ts.map +1 -0
- package/dist/cjs/types/validators/utils.d.ts +83 -0
- package/dist/cjs/types/validators/utils.d.ts.map +1 -0
- package/dist/esm/src/adapter/fields/array.js +13 -0
- package/dist/esm/src/adapter/fields/boolean.js +20 -0
- package/dist/esm/src/adapter/fields/datetime.js +20 -0
- package/dist/esm/src/adapter/fields/index.js +37 -0
- package/dist/esm/src/adapter/fields/number.js +20 -0
- package/dist/esm/src/adapter/fields/object.js +20 -0
- package/dist/esm/src/adapter/fields/string.js +20 -0
- package/dist/esm/src/adapter/fields/union.js +20 -0
- package/dist/esm/src/adapter/index.js +18 -0
- package/dist/esm/src/adapter/types.js +1 -0
- package/dist/esm/src/compile.js +10 -0
- package/dist/esm/src/conf.js +19 -0
- package/dist/esm/src/constants.js +5 -0
- package/dist/esm/src/domain.js +2 -0
- package/dist/esm/src/exceptions.js +15 -0
- package/dist/esm/src/index.js +160 -0
- package/dist/esm/src/model.js +255 -0
- package/dist/esm/src/parsers/convert-from-number.js +8 -0
- package/dist/esm/src/parsers/convert-from-string.js +14 -0
- package/dist/esm/src/parsers/index.js +2 -0
- package/dist/esm/src/schema/array.js +403 -0
- package/dist/esm/src/schema/boolean.js +465 -0
- package/dist/esm/src/schema/datetime.js +423 -0
- package/dist/esm/src/schema/index.js +3 -0
- package/dist/esm/src/schema/number.js +592 -0
- package/dist/esm/src/schema/object.js +464 -0
- package/dist/esm/src/schema/schema.js +728 -0
- package/dist/esm/src/schema/string.js +579 -0
- package/dist/esm/src/schema/types.js +1 -0
- package/dist/esm/src/schema/union.js +388 -0
- package/dist/esm/src/types.js +1 -0
- package/dist/esm/src/utils.js +175 -0
- package/dist/esm/src/validators/array.js +135 -0
- package/dist/esm/src/validators/boolean.js +35 -0
- package/dist/esm/src/validators/datetime.js +85 -0
- package/dist/esm/src/validators/number.js +162 -0
- package/dist/esm/src/validators/object.js +38 -0
- package/dist/esm/src/validators/schema.js +114 -0
- package/dist/esm/src/validators/string.js +174 -0
- package/dist/esm/src/validators/types.js +1 -0
- package/dist/esm/src/validators/union.js +38 -0
- package/dist/esm/src/validators/utils.js +120 -0
- package/package.json +48 -0
- package/src/adapter/fields/array.ts +31 -0
- package/src/adapter/fields/boolean.ts +48 -0
- package/src/adapter/fields/datetime.ts +49 -0
- package/src/adapter/fields/index.ts +72 -0
- package/src/adapter/fields/number.ts +49 -0
- package/src/adapter/fields/object.ts +49 -0
- package/src/adapter/fields/string.ts +49 -0
- package/src/adapter/fields/union.ts +49 -0
- package/src/adapter/index.ts +34 -0
- package/src/adapter/types.ts +261 -0
- package/src/compile.ts +14 -0
- package/src/conf.ts +27 -0
- package/src/constants.ts +9 -0
- package/src/domain.ts +3 -0
- package/src/exceptions.ts +17 -0
- package/src/index.ts +338 -0
- package/src/model.ts +501 -0
- package/src/parsers/convert-from-number.ts +13 -0
- package/src/parsers/convert-from-string.ts +19 -0
- package/src/parsers/index.ts +2 -0
- package/src/schema/array.ts +633 -0
- package/src/schema/boolean.ts +700 -0
- package/src/schema/datetime.ts +613 -0
- package/src/schema/index.ts +5 -0
- package/src/schema/number.ts +885 -0
- package/src/schema/object.ts +699 -0
- package/src/schema/schema.ts +1093 -0
- package/src/schema/string.ts +807 -0
- package/src/schema/types.ts +126 -0
- package/src/schema/union.ts +596 -0
- package/src/types.ts +13 -0
- package/src/utils.ts +322 -0
- package/src/validators/array.ts +164 -0
- package/src/validators/boolean.ts +46 -0
- package/src/validators/datetime.ts +113 -0
- package/src/validators/number.ts +188 -0
- package/src/validators/object.ts +55 -0
- package/src/validators/schema.ts +134 -0
- package/src/validators/string.ts +215 -0
- package/src/validators/types.ts +1 -0
- package/src/validators/union.ts +52 -0
- package/src/validators/utils.ts +200 -0
- package/tsconfig.json +9 -0
- package/tsconfig.types.json +10 -0
@@ -0,0 +1,135 @@
|
|
1
|
+
export function arrayValidation(isTuple, schemas) {
|
2
|
+
return {
|
3
|
+
type: 'medium',
|
4
|
+
callback: async (value, path, options)=>{
|
5
|
+
const isNotAnArray = Array.isArray(value) === false;
|
6
|
+
if (isNotAnArray) return {
|
7
|
+
parsed: value,
|
8
|
+
preventChildValidation: true,
|
9
|
+
errors: [
|
10
|
+
{
|
11
|
+
isValid: false,
|
12
|
+
code: 'array',
|
13
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
14
|
+
path: path || [],
|
15
|
+
message: 'The value must be an array. Received: ' + typeof value
|
16
|
+
}
|
17
|
+
]
|
18
|
+
};
|
19
|
+
if (isTuple && value.length !== schemas.length) return {
|
20
|
+
parsed: value,
|
21
|
+
preventChildValidation: true,
|
22
|
+
errors: [
|
23
|
+
{
|
24
|
+
isValid: false,
|
25
|
+
code: 'tuple',
|
26
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
27
|
+
path: path || [],
|
28
|
+
message: 'The tuple must have exactly ' + schemas.length + ' elements. Received: ' + value.length
|
29
|
+
}
|
30
|
+
]
|
31
|
+
};
|
32
|
+
const errorsOfArray = [];
|
33
|
+
// To speed things up, we can do a simple type check, if the value is of type number and number is on index 1, and on index 0 is a string,
|
34
|
+
// if the value is a number we can skip checking at index 0.
|
35
|
+
const schemaIndexByTypeof = new Map();
|
36
|
+
let parsedValues = [];
|
37
|
+
parsedValues = await Promise.all(value.map(async (element, index)=>{
|
38
|
+
let errorsToAppendAfterLoopIfNoSchemaMatched = [];
|
39
|
+
const typeofElement = typeof element;
|
40
|
+
const schemaIndex = schemaIndexByTypeof.get(typeofElement);
|
41
|
+
const existsASchemaIndex = typeof schemaIndex === 'number';
|
42
|
+
const schemasToValidateAgainst = isTuple ? [
|
43
|
+
schemas[index]
|
44
|
+
] : existsASchemaIndex ? [
|
45
|
+
schemas[schemaIndex]
|
46
|
+
] : schemas;
|
47
|
+
for(let indexOfSchema = 0; indexOfSchema < schemasToValidateAgainst.length; indexOfSchema++){
|
48
|
+
const schemaToValidate = schemasToValidateAgainst[indexOfSchema];
|
49
|
+
const schemaWithProtected = schemaToValidate;
|
50
|
+
const { parsed, errors } = await schemaWithProtected.__parse(element, [
|
51
|
+
...path,
|
52
|
+
index
|
53
|
+
], options);
|
54
|
+
if (schemaWithProtected.__toInternal && options.toInternalToBubbleUp) options.toInternalToBubbleUp.push(async ()=>parsedValues[indexOfSchema] = await schemaWithProtected.__toInternal?.(parsed));
|
55
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
56
|
+
if ((errors || []).length <= 0) {
|
57
|
+
errorsToAppendAfterLoopIfNoSchemaMatched = [];
|
58
|
+
schemaIndexByTypeof.set(typeofElement, indexOfSchema);
|
59
|
+
return parsed;
|
60
|
+
}
|
61
|
+
errorsToAppendAfterLoopIfNoSchemaMatched.push(...errors);
|
62
|
+
}
|
63
|
+
errorsOfArray.push(...errorsToAppendAfterLoopIfNoSchemaMatched);
|
64
|
+
return element;
|
65
|
+
}));
|
66
|
+
return {
|
67
|
+
parsed: parsedValues,
|
68
|
+
errors: errorsOfArray
|
69
|
+
};
|
70
|
+
}
|
71
|
+
};
|
72
|
+
}
|
73
|
+
export function minLength(args) {
|
74
|
+
return {
|
75
|
+
type: 'low',
|
76
|
+
// eslint-disable-next-line ts/require-await
|
77
|
+
callback: async (value, path, _options)=>{
|
78
|
+
const isValid = args.inclusive ? value.length >= args.value : value.length > args.value;
|
79
|
+
return {
|
80
|
+
parsed: value,
|
81
|
+
errors: isValid ? [] : [
|
82
|
+
{
|
83
|
+
isValid: false,
|
84
|
+
code: 'minLength',
|
85
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
86
|
+
path: path || [],
|
87
|
+
message: args.message
|
88
|
+
}
|
89
|
+
]
|
90
|
+
};
|
91
|
+
}
|
92
|
+
};
|
93
|
+
}
|
94
|
+
export function maxLength(args) {
|
95
|
+
return {
|
96
|
+
type: 'low',
|
97
|
+
// eslint-disable-next-line ts/require-await
|
98
|
+
callback: async (value, path, _options)=>{
|
99
|
+
const isValid = args.inclusive ? value.length <= args.value : value.length < args.value;
|
100
|
+
return {
|
101
|
+
parsed: value,
|
102
|
+
errors: isValid ? [] : [
|
103
|
+
{
|
104
|
+
isValid: false,
|
105
|
+
code: 'maxLength',
|
106
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
107
|
+
path: path || [],
|
108
|
+
message: args.message
|
109
|
+
}
|
110
|
+
]
|
111
|
+
};
|
112
|
+
}
|
113
|
+
};
|
114
|
+
}
|
115
|
+
export function nonEmpty(args) {
|
116
|
+
return {
|
117
|
+
type: 'low',
|
118
|
+
// eslint-disable-next-line ts/require-await
|
119
|
+
callback: async (value, path, _options)=>{
|
120
|
+
const isValid = value.length > 0;
|
121
|
+
return {
|
122
|
+
parsed: value,
|
123
|
+
errors: isValid ? [] : [
|
124
|
+
{
|
125
|
+
isValid: false,
|
126
|
+
code: 'nonEmpty',
|
127
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
128
|
+
path: path || [],
|
129
|
+
message: args.message
|
130
|
+
}
|
131
|
+
]
|
132
|
+
};
|
133
|
+
}
|
134
|
+
};
|
135
|
+
}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
export function booleanValidation() {
|
2
|
+
return {
|
3
|
+
type: 'medium',
|
4
|
+
// eslint-disable-next-line ts/require-await
|
5
|
+
callback: async (value, path, _options)=>{
|
6
|
+
const isValid = typeof value === 'boolean';
|
7
|
+
return {
|
8
|
+
parsed: value,
|
9
|
+
errors: isValid ? [] : [
|
10
|
+
{
|
11
|
+
isValid: false,
|
12
|
+
code: 'boolean',
|
13
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
14
|
+
path: path || [],
|
15
|
+
message: 'Value is not a boolean'
|
16
|
+
}
|
17
|
+
],
|
18
|
+
preventChildValidation: true
|
19
|
+
};
|
20
|
+
}
|
21
|
+
};
|
22
|
+
}
|
23
|
+
export function allowStringParser() {
|
24
|
+
return {
|
25
|
+
type: 'high',
|
26
|
+
// eslint-disable-next-line ts/require-await
|
27
|
+
callback: async (value, _path, _options)=>{
|
28
|
+
const parsed = Boolean(value);
|
29
|
+
return {
|
30
|
+
parsed: parsed,
|
31
|
+
errors: []
|
32
|
+
};
|
33
|
+
}
|
34
|
+
};
|
35
|
+
}
|
@@ -0,0 +1,85 @@
|
|
1
|
+
export function datetimeValidation() {
|
2
|
+
return {
|
3
|
+
type: 'medium',
|
4
|
+
// eslint-disable-next-line ts/require-await
|
5
|
+
callback: async (value, path, _options)=>{
|
6
|
+
const isValid = value instanceof Date && !isNaN(value.getTime());
|
7
|
+
return {
|
8
|
+
parsed: value,
|
9
|
+
errors: isValid ? [] : [
|
10
|
+
{
|
11
|
+
isValid: false,
|
12
|
+
code: 'datetime',
|
13
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
14
|
+
path: path || [],
|
15
|
+
message: 'Value is not a date'
|
16
|
+
}
|
17
|
+
],
|
18
|
+
preventChildValidation: true
|
19
|
+
};
|
20
|
+
}
|
21
|
+
};
|
22
|
+
}
|
23
|
+
export function allowStringParser() {
|
24
|
+
return {
|
25
|
+
type: 'high',
|
26
|
+
// eslint-disable-next-line ts/require-await
|
27
|
+
callback: async (value, _path, _options)=>{
|
28
|
+
if (typeof value === 'string') {
|
29
|
+
const parsed = new Date(value);
|
30
|
+
if (parsed instanceof Date && !isNaN(parsed.getTime())) {
|
31
|
+
return {
|
32
|
+
parsed: parsed,
|
33
|
+
errors: []
|
34
|
+
};
|
35
|
+
}
|
36
|
+
}
|
37
|
+
return {
|
38
|
+
parsed: value,
|
39
|
+
errors: []
|
40
|
+
};
|
41
|
+
}
|
42
|
+
};
|
43
|
+
}
|
44
|
+
export function below(args) {
|
45
|
+
return {
|
46
|
+
type: 'low',
|
47
|
+
// eslint-disable-next-line ts/require-await
|
48
|
+
callback: async (value, path, _options)=>{
|
49
|
+
const isValid = args.inclusive ? value <= args.value : value < args.value;
|
50
|
+
return {
|
51
|
+
parsed: value,
|
52
|
+
errors: isValid ? [] : [
|
53
|
+
{
|
54
|
+
isValid: false,
|
55
|
+
code: 'below',
|
56
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
57
|
+
path: path || [],
|
58
|
+
message: args.message
|
59
|
+
}
|
60
|
+
]
|
61
|
+
};
|
62
|
+
}
|
63
|
+
};
|
64
|
+
}
|
65
|
+
export function above(args) {
|
66
|
+
return {
|
67
|
+
type: 'low',
|
68
|
+
// eslint-disable-next-line ts/require-await
|
69
|
+
callback: async (value, path, _options)=>{
|
70
|
+
const isValid = args.inclusive ? value <= args.value : value < args.value;
|
71
|
+
return {
|
72
|
+
parsed: value,
|
73
|
+
errors: isValid ? [] : [
|
74
|
+
{
|
75
|
+
isValid: false,
|
76
|
+
code: 'above',
|
77
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
78
|
+
path: path || [],
|
79
|
+
message: args.message
|
80
|
+
}
|
81
|
+
]
|
82
|
+
};
|
83
|
+
}
|
84
|
+
};
|
85
|
+
}
|
@@ -0,0 +1,162 @@
|
|
1
|
+
export function numberValidation() {
|
2
|
+
return {
|
3
|
+
type: 'medium',
|
4
|
+
// eslint-disable-next-line ts/require-await
|
5
|
+
callback: async (value, path, _options)=>{
|
6
|
+
return {
|
7
|
+
parsed: value,
|
8
|
+
errors: [
|
9
|
+
{
|
10
|
+
isValid: typeof value === 'number',
|
11
|
+
code: 'number',
|
12
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
13
|
+
path: path || [],
|
14
|
+
message: 'The value must be a number. Received: ' + typeof value
|
15
|
+
}
|
16
|
+
]
|
17
|
+
};
|
18
|
+
}
|
19
|
+
};
|
20
|
+
}
|
21
|
+
export function max(args) {
|
22
|
+
return {
|
23
|
+
type: 'low',
|
24
|
+
// eslint-disable-next-line ts/require-await
|
25
|
+
callback: async (value, path, _options)=>{
|
26
|
+
if (args.inclusive) return {
|
27
|
+
parsed: value,
|
28
|
+
errors: [
|
29
|
+
{
|
30
|
+
isValid: value <= args.value,
|
31
|
+
code: 'max',
|
32
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
33
|
+
path: path || [],
|
34
|
+
message: args.message
|
35
|
+
}
|
36
|
+
]
|
37
|
+
};
|
38
|
+
return {
|
39
|
+
parsed: value,
|
40
|
+
errors: [
|
41
|
+
{
|
42
|
+
isValid: value < args.value,
|
43
|
+
code: 'max',
|
44
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
45
|
+
path: path || [],
|
46
|
+
message: args.message
|
47
|
+
}
|
48
|
+
]
|
49
|
+
};
|
50
|
+
}
|
51
|
+
};
|
52
|
+
}
|
53
|
+
export function min(args) {
|
54
|
+
return {
|
55
|
+
type: 'low',
|
56
|
+
// eslint-disable-next-line ts/require-await
|
57
|
+
callback: async (value, path)=>{
|
58
|
+
if (args.inclusive) return {
|
59
|
+
parsed: value,
|
60
|
+
errors: [
|
61
|
+
{
|
62
|
+
isValid: value >= args.value,
|
63
|
+
message: args.message,
|
64
|
+
code: 'min',
|
65
|
+
path: path || []
|
66
|
+
}
|
67
|
+
]
|
68
|
+
};
|
69
|
+
return {
|
70
|
+
parsed: value,
|
71
|
+
errors: [
|
72
|
+
{
|
73
|
+
isValid: value > args.value,
|
74
|
+
message: args.message,
|
75
|
+
code: 'min',
|
76
|
+
path: path || []
|
77
|
+
}
|
78
|
+
]
|
79
|
+
};
|
80
|
+
}
|
81
|
+
};
|
82
|
+
}
|
83
|
+
export function negative(args) {
|
84
|
+
return {
|
85
|
+
type: 'low',
|
86
|
+
// eslint-disable-next-line ts/require-await
|
87
|
+
callback: async (value, path)=>{
|
88
|
+
const isValid = args.allowZero ? value < 0 : value <= 0;
|
89
|
+
return {
|
90
|
+
parsed: value,
|
91
|
+
errors: isValid ? [] : [
|
92
|
+
{
|
93
|
+
isValid: isValid,
|
94
|
+
message: args.message,
|
95
|
+
code: 'negative',
|
96
|
+
path: path || []
|
97
|
+
}
|
98
|
+
]
|
99
|
+
};
|
100
|
+
}
|
101
|
+
};
|
102
|
+
}
|
103
|
+
export function positive(args) {
|
104
|
+
return {
|
105
|
+
type: 'low',
|
106
|
+
// eslint-disable-next-line ts/require-await
|
107
|
+
callback: async (value, path)=>{
|
108
|
+
const isValid = args.allowZero ? value > 0 : value >= 0;
|
109
|
+
return {
|
110
|
+
parsed: value,
|
111
|
+
errors: isValid ? [] : [
|
112
|
+
{
|
113
|
+
isValid: isValid,
|
114
|
+
message: args.message,
|
115
|
+
code: 'positive',
|
116
|
+
path: path || []
|
117
|
+
}
|
118
|
+
]
|
119
|
+
};
|
120
|
+
}
|
121
|
+
};
|
122
|
+
}
|
123
|
+
export function maxDigits(args) {
|
124
|
+
return {
|
125
|
+
type: 'low',
|
126
|
+
// eslint-disable-next-line ts/require-await
|
127
|
+
callback: async (value, path)=>{
|
128
|
+
const isValid = value.toString().replace('.', '').length <= args.value;
|
129
|
+
return {
|
130
|
+
parsed: value,
|
131
|
+
errors: isValid ? [] : [
|
132
|
+
{
|
133
|
+
isValid: isValid,
|
134
|
+
message: args.message,
|
135
|
+
code: 'maxDigits',
|
136
|
+
path: path || []
|
137
|
+
}
|
138
|
+
]
|
139
|
+
};
|
140
|
+
}
|
141
|
+
};
|
142
|
+
}
|
143
|
+
export function decimalPlaces(args) {
|
144
|
+
return {
|
145
|
+
type: 'low',
|
146
|
+
// eslint-disable-next-line ts/require-await
|
147
|
+
callback: async (value, path)=>{
|
148
|
+
const isValid = value.toString().split('.')[1]?.length <= args.value;
|
149
|
+
return {
|
150
|
+
parsed: value,
|
151
|
+
errors: isValid ? [] : [
|
152
|
+
{
|
153
|
+
isValid: isValid,
|
154
|
+
message: args.message,
|
155
|
+
code: 'decimalPlaces',
|
156
|
+
path: path || []
|
157
|
+
}
|
158
|
+
]
|
159
|
+
};
|
160
|
+
}
|
161
|
+
};
|
162
|
+
}
|
@@ -0,0 +1,38 @@
|
|
1
|
+
export function objectValidation(keysToFallback) {
|
2
|
+
return {
|
3
|
+
type: 'low',
|
4
|
+
callback: async (value, path, options)=>{
|
5
|
+
const isNotAnObject = typeof value !== 'object' && Array.isArray(value) === false && value !== null;
|
6
|
+
if (isNotAnObject) return {
|
7
|
+
parsed: value,
|
8
|
+
preventChildValidation: true,
|
9
|
+
errors: [
|
10
|
+
{
|
11
|
+
isValid: false,
|
12
|
+
code: 'object',
|
13
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
14
|
+
path: path || [],
|
15
|
+
message: 'The value must be an object. Received: ' + typeof value
|
16
|
+
}
|
17
|
+
]
|
18
|
+
};
|
19
|
+
const errors = {};
|
20
|
+
const toValidateEntries = Object.entries(keysToFallback);
|
21
|
+
await Promise.all(toValidateEntries.map(async ([key, schema])=>{
|
22
|
+
const schemaWithProtected = schema;
|
23
|
+
const { parsed, errors: parseErrors } = await schemaWithProtected.__parse(value[key], [
|
24
|
+
...path,
|
25
|
+
key
|
26
|
+
], options);
|
27
|
+
if (Array.isArray(parseErrors) && parseErrors.length > 0) errors[key] = parseErrors;
|
28
|
+
else value[key] = parsed;
|
29
|
+
// We append the toInternalToBubbleUp to the parent toInternalToBubbleUp
|
30
|
+
if (schemaWithProtected.__toInternal && options.toInternalToBubbleUp) options.toInternalToBubbleUp.push(async ()=>value[key] = await schema.__toInternal(parsed));
|
31
|
+
}));
|
32
|
+
return {
|
33
|
+
parsed: value,
|
34
|
+
errors: Object.values(errors).flat()
|
35
|
+
};
|
36
|
+
}
|
37
|
+
};
|
38
|
+
}
|
@@ -0,0 +1,114 @@
|
|
1
|
+
export function optional(args) {
|
2
|
+
return {
|
3
|
+
type: 'high',
|
4
|
+
// eslint-disable-next-line ts/require-await
|
5
|
+
callback: async (value, path)=>{
|
6
|
+
if (value === undefined) {
|
7
|
+
if (args.allow === true) return {
|
8
|
+
parsed: value,
|
9
|
+
errors: [],
|
10
|
+
preventChildValidation: true
|
11
|
+
};
|
12
|
+
return {
|
13
|
+
parsed: value,
|
14
|
+
errors: [
|
15
|
+
{
|
16
|
+
isValid: false,
|
17
|
+
message: args.message,
|
18
|
+
code: 'required',
|
19
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
20
|
+
path: path || []
|
21
|
+
}
|
22
|
+
],
|
23
|
+
preventChildValidation: true
|
24
|
+
};
|
25
|
+
}
|
26
|
+
return {
|
27
|
+
parsed: value,
|
28
|
+
errors: [],
|
29
|
+
preventChildValidation: false
|
30
|
+
};
|
31
|
+
}
|
32
|
+
};
|
33
|
+
}
|
34
|
+
export function nullable(args) {
|
35
|
+
return {
|
36
|
+
type: 'high',
|
37
|
+
// eslint-disable-next-line ts/require-await
|
38
|
+
callback: async (value, path)=>{
|
39
|
+
if (value === null) {
|
40
|
+
if (args.allow === true) return {
|
41
|
+
parsed: value,
|
42
|
+
errors: [],
|
43
|
+
preventChildValidation: true
|
44
|
+
};
|
45
|
+
return {
|
46
|
+
parsed: value,
|
47
|
+
errors: [
|
48
|
+
{
|
49
|
+
isValid: false,
|
50
|
+
message: args.message,
|
51
|
+
code: 'cannot_be_null',
|
52
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
53
|
+
path: path || []
|
54
|
+
}
|
55
|
+
],
|
56
|
+
preventChildValidation: true
|
57
|
+
};
|
58
|
+
}
|
59
|
+
return {
|
60
|
+
parsed: value,
|
61
|
+
errors: [],
|
62
|
+
preventChildValidation: false
|
63
|
+
};
|
64
|
+
}
|
65
|
+
};
|
66
|
+
}
|
67
|
+
export function checkType(args) {
|
68
|
+
return {
|
69
|
+
type: 'medium',
|
70
|
+
// eslint-disable-next-line ts/require-await
|
71
|
+
callback: async (value, path)=>{
|
72
|
+
if (args.check(value)) return {
|
73
|
+
parsed: value,
|
74
|
+
errors: [],
|
75
|
+
preventChildValidation: false
|
76
|
+
};
|
77
|
+
return {
|
78
|
+
parsed: value,
|
79
|
+
errors: [
|
80
|
+
{
|
81
|
+
isValid: false,
|
82
|
+
message: args.message,
|
83
|
+
code: 'invalid_type',
|
84
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
85
|
+
path: path || []
|
86
|
+
}
|
87
|
+
],
|
88
|
+
preventChildValidation: true
|
89
|
+
};
|
90
|
+
}
|
91
|
+
};
|
92
|
+
}
|
93
|
+
export function is(args) {
|
94
|
+
return {
|
95
|
+
type: 'medium',
|
96
|
+
// eslint-disable-next-line ts/require-await
|
97
|
+
callback: async (value, path, _options)=>{
|
98
|
+
const isValid = Array.isArray(args.value) ? args.value.includes(value) : value === args.value;
|
99
|
+
return {
|
100
|
+
parsed: value,
|
101
|
+
errors: isValid ? [] : [
|
102
|
+
{
|
103
|
+
isValid: false,
|
104
|
+
code: 'is',
|
105
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
106
|
+
path: path || [],
|
107
|
+
message: 'Value is not a boolean'
|
108
|
+
}
|
109
|
+
],
|
110
|
+
preventChildValidation: true
|
111
|
+
};
|
112
|
+
}
|
113
|
+
};
|
114
|
+
}
|