@palmares/schemas 0.1.24 → 0.1.26
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/CHANGELOG.md +22 -0
- package/dist/src/adapter/fields/array.cjs +96 -0
- package/dist/src/adapter/fields/array.d.ts.map +1 -0
- package/dist/src/adapter/fields/array.js +70 -0
- package/dist/src/adapter/fields/boolean.cjs +102 -0
- package/dist/src/adapter/fields/boolean.d.ts.map +1 -0
- package/dist/src/adapter/fields/boolean.js +76 -0
- package/dist/src/adapter/fields/datetime.cjs +102 -0
- package/dist/src/adapter/fields/datetime.d.ts.map +1 -0
- package/dist/src/adapter/fields/datetime.js +76 -0
- package/dist/src/adapter/fields/index.cjs +87 -0
- package/dist/src/adapter/fields/index.d.ts.map +1 -0
- package/dist/src/adapter/fields/index.js +61 -0
- package/dist/src/adapter/fields/number.cjs +102 -0
- package/dist/src/adapter/fields/number.d.ts.map +1 -0
- package/dist/src/adapter/fields/number.js +76 -0
- package/dist/src/adapter/fields/object.cjs +108 -0
- package/dist/src/adapter/fields/object.d.ts.map +1 -0
- package/dist/src/adapter/fields/object.js +82 -0
- package/dist/src/adapter/fields/string.cjs +102 -0
- package/dist/src/adapter/fields/string.d.ts.map +1 -0
- package/dist/src/adapter/fields/string.js +76 -0
- package/dist/src/adapter/fields/union.cjs +102 -0
- package/dist/src/adapter/fields/union.d.ts.map +1 -0
- package/dist/src/adapter/fields/union.js +76 -0
- package/dist/src/adapter/index.cjs +65 -0
- package/dist/src/adapter/index.d.ts.map +1 -0
- package/dist/src/adapter/index.js +40 -0
- package/dist/src/adapter/types.cjs +18 -0
- package/dist/src/adapter/types.d.ts.map +1 -0
- package/dist/src/adapter/types.js +0 -0
- package/dist/src/compile.cjs +59 -0
- package/dist/src/compile.d.ts.map +1 -0
- package/dist/src/compile.js +36 -0
- package/dist/src/conf.cjs +53 -0
- package/dist/src/conf.d.ts.map +1 -0
- package/dist/src/conf.js +27 -0
- package/dist/src/constants.cjs +40 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/constants.js +14 -0
- package/dist/src/domain.cjs +63 -0
- package/dist/src/domain.d.ts.map +1 -0
- package/dist/src/domain.js +40 -0
- package/dist/src/exceptions.cjs +58 -0
- package/dist/src/exceptions.d.ts.map +1 -0
- package/dist/src/exceptions.js +33 -0
- package/dist/src/index.cjs +6520 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +6456 -0
- package/dist/src/middleware.cjs +58 -0
- package/dist/src/middleware.d.ts.map +1 -0
- package/dist/src/middleware.js +35 -0
- package/dist/src/model.cjs +6003 -0
- package/dist/src/model.d.ts.map +1 -0
- package/dist/src/model.js +5978 -0
- package/dist/src/parsers/convert-from-number.cjs +34 -0
- package/dist/src/parsers/convert-from-number.d.ts.map +1 -0
- package/dist/src/parsers/convert-from-number.js +11 -0
- package/dist/src/parsers/convert-from-string.cjs +40 -0
- package/dist/src/parsers/convert-from-string.d.ts.map +1 -0
- package/dist/src/parsers/convert-from-string.js +17 -0
- package/dist/src/parsers/index.cjs +50 -0
- package/dist/src/parsers/index.d.ts.map +1 -0
- package/dist/src/parsers/index.js +24 -0
- package/dist/src/schema/array.cjs +1863 -0
- package/dist/src/schema/array.d.ts.map +1 -0
- package/dist/src/schema/array.js +1837 -0
- package/dist/src/schema/boolean.cjs +1800 -0
- package/dist/src/schema/boolean.d.ts.map +1 -0
- package/dist/src/schema/boolean.js +1774 -0
- package/dist/src/schema/datetime.cjs +1789 -0
- package/dist/src/schema/datetime.d.ts.map +1 -0
- package/dist/src/schema/datetime.js +1763 -0
- package/dist/src/schema/index.cjs +2026 -0
- package/dist/src/schema/index.d.ts.map +1 -0
- package/dist/src/schema/index.js +2001 -0
- package/dist/src/schema/number.cjs +2026 -0
- package/dist/src/schema/number.d.ts.map +1 -0
- package/dist/src/schema/number.js +2000 -0
- package/dist/src/schema/object.cjs +1795 -0
- package/dist/src/schema/object.d.ts.map +1 -0
- package/dist/src/schema/object.js +1769 -0
- package/dist/src/schema/schema.cjs +844 -0
- package/dist/src/schema/schema.d.ts.map +1 -0
- package/dist/src/schema/schema.js +818 -0
- package/dist/src/schema/string.cjs +2126 -0
- package/dist/src/schema/string.d.ts.map +1 -0
- package/dist/src/schema/string.js +2100 -0
- package/dist/src/schema/types.cjs +18 -0
- package/dist/src/schema/types.d.ts.map +1 -0
- package/dist/src/schema/types.js +0 -0
- package/dist/src/schema/union.cjs +1737 -0
- package/dist/src/schema/union.d.ts.map +1 -0
- package/dist/src/schema/union.js +1711 -0
- package/dist/src/types.cjs +18 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +0 -0
- package/dist/src/utils.cjs +477 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +445 -0
- package/dist/src/validators/array.cjs +176 -0
- package/dist/src/validators/array.d.ts.map +1 -0
- package/dist/src/validators/array.js +150 -0
- package/dist/src/validators/boolean.cjs +71 -0
- package/dist/src/validators/boolean.d.ts.map +1 -0
- package/dist/src/validators/boolean.js +47 -0
- package/dist/src/validators/datetime.cjs +129 -0
- package/dist/src/validators/datetime.d.ts.map +1 -0
- package/dist/src/validators/datetime.js +103 -0
- package/dist/src/validators/number.cjs +194 -0
- package/dist/src/validators/number.d.ts.map +1 -0
- package/dist/src/validators/number.js +166 -0
- package/dist/src/validators/object.cjs +69 -0
- package/dist/src/validators/object.d.ts.map +1 -0
- package/dist/src/validators/object.js +46 -0
- package/dist/src/validators/schema.cjs +158 -0
- package/dist/src/validators/schema.d.ts.map +1 -0
- package/dist/src/validators/schema.js +132 -0
- package/dist/src/validators/string.cjs +255 -0
- package/dist/src/validators/string.d.ts.map +1 -0
- package/dist/src/validators/string.js +224 -0
- package/dist/src/validators/types.cjs +18 -0
- package/dist/src/validators/types.d.ts.map +1 -0
- package/dist/src/validators/types.js +0 -0
- package/dist/src/validators/union.cjs +69 -0
- package/dist/src/validators/union.d.ts.map +1 -0
- package/dist/src/validators/union.js +46 -0
- package/dist/src/validators/utils.cjs +165 -0
- package/dist/src/validators/utils.d.ts.map +1 -0
- package/dist/src/validators/utils.js +142 -0
- package/dist/tsconfig.types.tsbuildinfo +1 -0
- package/package.json +15 -23
- package/dist/cjs/src/adapter/fields/array.js +0 -152
- package/dist/cjs/src/adapter/fields/boolean.js +0 -162
- package/dist/cjs/src/adapter/fields/datetime.js +0 -162
- package/dist/cjs/src/adapter/fields/index.js +0 -311
- package/dist/cjs/src/adapter/fields/number.js +0 -162
- package/dist/cjs/src/adapter/fields/object.js +0 -171
- package/dist/cjs/src/adapter/fields/string.js +0 -162
- package/dist/cjs/src/adapter/fields/union.js +0 -162
- package/dist/cjs/src/adapter/index.js +0 -201
- package/dist/cjs/src/adapter/types.js +0 -4
- package/dist/cjs/src/compile.js +0 -262
- package/dist/cjs/src/conf.js +0 -26
- package/dist/cjs/src/constants.js +0 -36
- package/dist/cjs/src/domain.js +0 -156
- package/dist/cjs/src/exceptions.js +0 -168
- package/dist/cjs/src/index.js +0 -317
- package/dist/cjs/src/middleware.js +0 -213
- package/dist/cjs/src/model.js +0 -628
- package/dist/cjs/src/parsers/convert-from-number.js +0 -15
- package/dist/cjs/src/parsers/convert-from-string.js +0 -19
- package/dist/cjs/src/parsers/index.js +0 -20
- package/dist/cjs/src/schema/array.js +0 -1014
- package/dist/cjs/src/schema/boolean.js +0 -883
- package/dist/cjs/src/schema/datetime.js +0 -840
- package/dist/cjs/src/schema/index.js +0 -12
- package/dist/cjs/src/schema/number.js +0 -976
- package/dist/cjs/src/schema/object.js +0 -1052
- package/dist/cjs/src/schema/schema.js +0 -1840
- package/dist/cjs/src/schema/string.js +0 -1038
- package/dist/cjs/src/schema/types.js +0 -4
- package/dist/cjs/src/schema/union.js +0 -1070
- package/dist/cjs/src/types.js +0 -4
- package/dist/cjs/src/utils.js +0 -647
- package/dist/cjs/src/validators/array.js +0 -462
- package/dist/cjs/src/validators/boolean.js +0 -201
- package/dist/cjs/src/validators/datetime.js +0 -291
- package/dist/cjs/src/validators/number.js +0 -375
- package/dist/cjs/src/validators/object.js +0 -291
- package/dist/cjs/src/validators/schema.js +0 -322
- package/dist/cjs/src/validators/string.js +0 -467
- package/dist/cjs/src/validators/types.js +0 -4
- package/dist/cjs/src/validators/union.js +0 -233
- package/dist/cjs/src/validators/utils.js +0 -499
- package/dist/cjs/tsconfig.types.tsbuildinfo +0 -1
- package/dist/cjs/types/adapter/fields/array.d.ts.map +0 -1
- package/dist/cjs/types/adapter/fields/boolean.d.ts.map +0 -1
- package/dist/cjs/types/adapter/fields/datetime.d.ts.map +0 -1
- package/dist/cjs/types/adapter/fields/index.d.ts.map +0 -1
- package/dist/cjs/types/adapter/fields/number.d.ts.map +0 -1
- package/dist/cjs/types/adapter/fields/object.d.ts.map +0 -1
- package/dist/cjs/types/adapter/fields/string.d.ts.map +0 -1
- package/dist/cjs/types/adapter/fields/union.d.ts.map +0 -1
- package/dist/cjs/types/adapter/index.d.ts.map +0 -1
- package/dist/cjs/types/adapter/types.d.ts.map +0 -1
- package/dist/cjs/types/compile.d.ts.map +0 -1
- package/dist/cjs/types/conf.d.ts.map +0 -1
- package/dist/cjs/types/constants.d.ts.map +0 -1
- package/dist/cjs/types/domain.d.ts.map +0 -1
- package/dist/cjs/types/exceptions.d.ts.map +0 -1
- package/dist/cjs/types/index.d.ts.map +0 -1
- package/dist/cjs/types/middleware.d.ts.map +0 -1
- package/dist/cjs/types/model.d.ts.map +0 -1
- package/dist/cjs/types/parsers/convert-from-number.d.ts.map +0 -1
- package/dist/cjs/types/parsers/convert-from-string.d.ts.map +0 -1
- package/dist/cjs/types/parsers/index.d.ts.map +0 -1
- package/dist/cjs/types/schema/array.d.ts.map +0 -1
- package/dist/cjs/types/schema/boolean.d.ts.map +0 -1
- package/dist/cjs/types/schema/datetime.d.ts.map +0 -1
- package/dist/cjs/types/schema/index.d.ts.map +0 -1
- package/dist/cjs/types/schema/number.d.ts.map +0 -1
- package/dist/cjs/types/schema/object.d.ts.map +0 -1
- package/dist/cjs/types/schema/schema.d.ts.map +0 -1
- package/dist/cjs/types/schema/string.d.ts.map +0 -1
- package/dist/cjs/types/schema/types.d.ts.map +0 -1
- package/dist/cjs/types/schema/union.d.ts.map +0 -1
- package/dist/cjs/types/types.d.ts.map +0 -1
- package/dist/cjs/types/utils.d.ts.map +0 -1
- package/dist/cjs/types/validators/array.d.ts.map +0 -1
- package/dist/cjs/types/validators/boolean.d.ts.map +0 -1
- package/dist/cjs/types/validators/datetime.d.ts.map +0 -1
- package/dist/cjs/types/validators/number.d.ts.map +0 -1
- package/dist/cjs/types/validators/object.d.ts.map +0 -1
- package/dist/cjs/types/validators/schema.d.ts.map +0 -1
- package/dist/cjs/types/validators/string.d.ts.map +0 -1
- package/dist/cjs/types/validators/types.d.ts.map +0 -1
- package/dist/cjs/types/validators/union.d.ts.map +0 -1
- package/dist/cjs/types/validators/utils.d.ts.map +0 -1
- package/dist/esm/src/adapter/fields/array.js +0 -13
- package/dist/esm/src/adapter/fields/boolean.js +0 -20
- package/dist/esm/src/adapter/fields/datetime.js +0 -20
- package/dist/esm/src/adapter/fields/index.js +0 -37
- package/dist/esm/src/adapter/fields/number.js +0 -20
- package/dist/esm/src/adapter/fields/object.js +0 -26
- package/dist/esm/src/adapter/fields/string.js +0 -20
- package/dist/esm/src/adapter/fields/union.js +0 -20
- package/dist/esm/src/adapter/index.js +0 -21
- package/dist/esm/src/adapter/types.js +0 -1
- package/dist/esm/src/compile.js +0 -10
- package/dist/esm/src/conf.js +0 -18
- package/dist/esm/src/constants.js +0 -4
- package/dist/esm/src/domain.js +0 -11
- package/dist/esm/src/exceptions.js +0 -15
- package/dist/esm/src/index.js +0 -126
- package/dist/esm/src/middleware.js +0 -29
- package/dist/esm/src/model.js +0 -264
- package/dist/esm/src/parsers/convert-from-number.js +0 -8
- package/dist/esm/src/parsers/convert-from-string.js +0 -14
- package/dist/esm/src/parsers/index.js +0 -2
- package/dist/esm/src/schema/array.js +0 -487
- package/dist/esm/src/schema/boolean.js +0 -520
- package/dist/esm/src/schema/datetime.js +0 -477
- package/dist/esm/src/schema/index.js +0 -3
- package/dist/esm/src/schema/number.js +0 -610
- package/dist/esm/src/schema/object.js +0 -521
- package/dist/esm/src/schema/schema.js +0 -767
- package/dist/esm/src/schema/string.js +0 -668
- package/dist/esm/src/schema/types.js +0 -1
- package/dist/esm/src/schema/union.js +0 -468
- package/dist/esm/src/types.js +0 -14
- package/dist/esm/src/utils.js +0 -207
- package/dist/esm/src/validators/array.js +0 -140
- package/dist/esm/src/validators/boolean.js +0 -37
- package/dist/esm/src/validators/datetime.js +0 -89
- package/dist/esm/src/validators/number.js +0 -148
- package/dist/esm/src/validators/object.js +0 -39
- package/dist/esm/src/validators/schema.js +0 -118
- package/dist/esm/src/validators/string.js +0 -202
- package/dist/esm/src/validators/types.js +0 -1
- package/dist/esm/src/validators/union.js +0 -39
- package/dist/esm/src/validators/utils.js +0 -147
- /package/dist/{cjs/types → src}/adapter/fields/array.d.ts +0 -0
- /package/dist/{cjs/types → src}/adapter/fields/boolean.d.ts +0 -0
- /package/dist/{cjs/types → src}/adapter/fields/datetime.d.ts +0 -0
- /package/dist/{cjs/types → src}/adapter/fields/index.d.ts +0 -0
- /package/dist/{cjs/types → src}/adapter/fields/number.d.ts +0 -0
- /package/dist/{cjs/types → src}/adapter/fields/object.d.ts +0 -0
- /package/dist/{cjs/types → src}/adapter/fields/string.d.ts +0 -0
- /package/dist/{cjs/types → src}/adapter/fields/union.d.ts +0 -0
- /package/dist/{cjs/types → src}/adapter/index.d.ts +0 -0
- /package/dist/{cjs/types → src}/adapter/types.d.ts +0 -0
- /package/dist/{cjs/types → src}/compile.d.ts +0 -0
- /package/dist/{cjs/types → src}/conf.d.ts +0 -0
- /package/dist/{cjs/types → src}/constants.d.ts +0 -0
- /package/dist/{cjs/types → src}/domain.d.ts +0 -0
- /package/dist/{cjs/types → src}/exceptions.d.ts +0 -0
- /package/dist/{cjs/types → src}/index.d.ts +0 -0
- /package/dist/{cjs/types → src}/middleware.d.ts +0 -0
- /package/dist/{cjs/types → src}/model.d.ts +0 -0
- /package/dist/{cjs/types → src}/parsers/convert-from-number.d.ts +0 -0
- /package/dist/{cjs/types → src}/parsers/convert-from-string.d.ts +0 -0
- /package/dist/{cjs/types → src}/parsers/index.d.ts +0 -0
- /package/dist/{cjs/types → src}/schema/array.d.ts +0 -0
- /package/dist/{cjs/types → src}/schema/boolean.d.ts +0 -0
- /package/dist/{cjs/types → src}/schema/datetime.d.ts +0 -0
- /package/dist/{cjs/types → src}/schema/index.d.ts +0 -0
- /package/dist/{cjs/types → src}/schema/number.d.ts +0 -0
- /package/dist/{cjs/types → src}/schema/object.d.ts +0 -0
- /package/dist/{cjs/types → src}/schema/schema.d.ts +0 -0
- /package/dist/{cjs/types → src}/schema/string.d.ts +0 -0
- /package/dist/{cjs/types → src}/schema/types.d.ts +0 -0
- /package/dist/{cjs/types → src}/schema/union.d.ts +0 -0
- /package/dist/{cjs/types → src}/types.d.ts +0 -0
- /package/dist/{cjs/types → src}/utils.d.ts +0 -0
- /package/dist/{cjs/types → src}/validators/array.d.ts +0 -0
- /package/dist/{cjs/types → src}/validators/boolean.d.ts +0 -0
- /package/dist/{cjs/types → src}/validators/datetime.d.ts +0 -0
- /package/dist/{cjs/types → src}/validators/number.d.ts +0 -0
- /package/dist/{cjs/types → src}/validators/object.d.ts +0 -0
- /package/dist/{cjs/types → src}/validators/schema.d.ts +0 -0
- /package/dist/{cjs/types → src}/validators/string.d.ts +0 -0
- /package/dist/{cjs/types → src}/validators/types.d.ts +0 -0
- /package/dist/{cjs/types → src}/validators/union.d.ts +0 -0
- /package/dist/{cjs/types → src}/validators/utils.d.ts +0 -0
@@ -1,520 +0,0 @@
|
|
1
|
-
import { Schema } from './schema';
|
2
|
-
import { convertFromStringBuilder } from '../parsers/convert-from-string';
|
3
|
-
import { defaultTransform, defaultTransformToAdapter } from '../utils';
|
4
|
-
import { booleanValidation } from '../validators/boolean';
|
5
|
-
import { is, nullable, optional } from '../validators/schema';
|
6
|
-
export class BooleanSchema extends Schema {
|
7
|
-
fieldType = 'boolean';
|
8
|
-
__allowString;
|
9
|
-
__allowNumber;
|
10
|
-
__trueValues;
|
11
|
-
__falseValues;
|
12
|
-
__is;
|
13
|
-
__type = {
|
14
|
-
message: 'Invalid type',
|
15
|
-
check: (value)=>{
|
16
|
-
if (typeof value === 'string' && this.__allowString) return true;
|
17
|
-
if (typeof value === 'number' && this.__allowNumber) return true;
|
18
|
-
if (Array.isArray(this.__trueValues) && this.__trueValues.includes(value)) return true;
|
19
|
-
if (Array.isArray(this.__falseValues) && this.__falseValues.includes(value)) return true;
|
20
|
-
return typeof value === 'boolean';
|
21
|
-
}
|
22
|
-
};
|
23
|
-
async __transformToAdapter(options) {
|
24
|
-
return defaultTransformToAdapter(async (adapter)=>{
|
25
|
-
return defaultTransform('boolean', this, adapter, adapter.boolean, ()=>({
|
26
|
-
parsers: {
|
27
|
-
allowString: this.__allowString,
|
28
|
-
allowNumber: this.__allowNumber,
|
29
|
-
// eslint-disable-next-line ts/no-unnecessary-condition
|
30
|
-
is: this.__is?.value,
|
31
|
-
trueValues: this.__trueValues,
|
32
|
-
falseValues: this.__falseValues,
|
33
|
-
nullable: this.__nullable.allow,
|
34
|
-
optional: this.__optional.allow
|
35
|
-
},
|
36
|
-
type: this.__type,
|
37
|
-
is: this.__is,
|
38
|
-
nullable: this.__nullable,
|
39
|
-
optional: this.__optional
|
40
|
-
}), {
|
41
|
-
optional,
|
42
|
-
nullable,
|
43
|
-
is
|
44
|
-
}, {
|
45
|
-
validatorsIfFallbackOrNotSupported: booleanValidation(),
|
46
|
-
shouldAddStringVersion: options.shouldAddStringVersion,
|
47
|
-
// eslint-disable-next-line ts/require-await
|
48
|
-
fallbackIfNotSupported: async ()=>[]
|
49
|
-
});
|
50
|
-
}, this, this.__transformedSchemas, options, 'boolean');
|
51
|
-
}
|
52
|
-
/**
|
53
|
-
* This let's you refine the schema with custom validations. This is useful when you want to validate something that
|
54
|
-
* is not supported by default by the schema adapter.
|
55
|
-
*
|
56
|
-
* @example
|
57
|
-
* ```typescript
|
58
|
-
* import * as p from '@palmares/schemas';
|
59
|
-
*
|
60
|
-
* const numberSchema = p.number().refine((value) => {
|
61
|
-
* if (value < 0) return { code: 'invalid_number', message: 'The number should be greater than 0' };
|
62
|
-
* });
|
63
|
-
*
|
64
|
-
* const { errors, parsed } = await numberSchema.parse(-1);
|
65
|
-
*
|
66
|
-
* // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
|
67
|
-
* console.log(errors);
|
68
|
-
* ```
|
69
|
-
*
|
70
|
-
* @param refinementCallback - The callback that will be called to validate the value.
|
71
|
-
* @param options - Options for the refinement.
|
72
|
-
* @param options.isAsync - Whether the callback is async or not. Defaults to true.
|
73
|
-
*
|
74
|
-
* @returns The schema.
|
75
|
-
*/ refine(refinementCallback) {
|
76
|
-
return super.refine(refinementCallback);
|
77
|
-
}
|
78
|
-
/**
|
79
|
-
* Allows the value to be either undefined or null.
|
80
|
-
*
|
81
|
-
* @example
|
82
|
-
* ```typescript
|
83
|
-
* import * as p from '@palmares/schemas';
|
84
|
-
*
|
85
|
-
* const numberSchema = p.number().optional();
|
86
|
-
*
|
87
|
-
* const { errors, parsed } = await numberSchema.parse(undefined);
|
88
|
-
*
|
89
|
-
* console.log(parsed); // undefined
|
90
|
-
*
|
91
|
-
* const { errors, parsed } = await numberSchema.parse(null);
|
92
|
-
*
|
93
|
-
* console.log(parsed); // null
|
94
|
-
*
|
95
|
-
* const { errors, parsed } = await numberSchema.parse(1);
|
96
|
-
*
|
97
|
-
* console.log(parsed); // 1
|
98
|
-
* ```
|
99
|
-
*
|
100
|
-
* @returns - The schema we are working with.
|
101
|
-
*/ optional(options) {
|
102
|
-
return super.optional(options);
|
103
|
-
}
|
104
|
-
/**
|
105
|
-
* Just adds a message when the value is undefined. It's just a syntax sugar for
|
106
|
-
*
|
107
|
-
* ```typescript
|
108
|
-
* p.string().optional({ message: 'This value cannot be null', allow: false })
|
109
|
-
* ```
|
110
|
-
*
|
111
|
-
* @param options - The options of nonOptional function
|
112
|
-
* @param options.message - A custom message if the value is undefined.
|
113
|
-
*
|
114
|
-
* @returns - The schema.
|
115
|
-
*/ nonOptional(options) {
|
116
|
-
return super.optional({
|
117
|
-
message: options?.message,
|
118
|
-
allow: false
|
119
|
-
});
|
120
|
-
}
|
121
|
-
/**
|
122
|
-
* Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value is
|
123
|
-
* NULL by setting the { message: 'Your custom message', allow: false } on the options.
|
124
|
-
*
|
125
|
-
* @example
|
126
|
-
* ```typescript
|
127
|
-
* import * as p from '@palmares/schemas';
|
128
|
-
*
|
129
|
-
* const numberSchema = p.number().nullable();
|
130
|
-
*
|
131
|
-
* const { errors, parsed } = await numberSchema.parse(null);
|
132
|
-
*
|
133
|
-
* console.log(parsed); // null
|
134
|
-
*
|
135
|
-
* const { errors, parsed } = await numberSchema.parse(undefined);
|
136
|
-
*
|
137
|
-
* console.log(errors); // [{ isValid: false, code: 'invalid_type', message: 'Invalid type', path: [] }]
|
138
|
-
* ```
|
139
|
-
*
|
140
|
-
* @param options - The options for the nullable function.
|
141
|
-
* @param options.message - The message to be shown when the value is not null. Defaults to 'Cannot be null'.
|
142
|
-
* @param options.allow - Whether the value can be null or not. Defaults to true.
|
143
|
-
*
|
144
|
-
* @returns The schema.
|
145
|
-
*/ nullable(options) {
|
146
|
-
return super.nullable(options);
|
147
|
-
}
|
148
|
-
/**
|
149
|
-
* Just adds a message when the value is null. It's just a syntax sugar for
|
150
|
-
*
|
151
|
-
* ```typescript
|
152
|
-
* p.string().nullable({ message: 'This value cannot be null', allow: false })
|
153
|
-
* ```
|
154
|
-
*
|
155
|
-
* @param options - The options of nonNullable function
|
156
|
-
* @param options.message - A custom message if the value is null.
|
157
|
-
*
|
158
|
-
* @returns - The schema.
|
159
|
-
*/ nonNullable(options) {
|
160
|
-
return super.nullable({
|
161
|
-
message: options?.message || '',
|
162
|
-
allow: false
|
163
|
-
});
|
164
|
-
}
|
165
|
-
/**
|
166
|
-
* This method will remove the value from the representation of the schema. If the value is undefined it will keep
|
167
|
-
* that way otherwise it will set the value to undefined after it's validated.
|
168
|
-
* This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
|
169
|
-
* function. This will remove the value from the representation of the schema.
|
170
|
-
*
|
171
|
-
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
172
|
-
* function.But if you want to remove the value from the internal representation, you can pass the argument
|
173
|
-
* `toInternal` as true. Then if you still want to remove the value from the representation, you will need to pass
|
174
|
-
* the argument `toRepresentation` as true as well.
|
175
|
-
*
|
176
|
-
* @example
|
177
|
-
* ```typescript
|
178
|
-
* import * as p from '@palmares/schemas';
|
179
|
-
*
|
180
|
-
* const userSchema = p.object({
|
181
|
-
* id: p.number().optional(),
|
182
|
-
* name: p.string(),
|
183
|
-
* password: p.string().omit()
|
184
|
-
* });
|
185
|
-
*
|
186
|
-
* const user = await userSchema.data({
|
187
|
-
* id: 1,
|
188
|
-
* name: 'John Doe',
|
189
|
-
* password: '123456'
|
190
|
-
* });
|
191
|
-
*
|
192
|
-
* console.log(user); // { id: 1, name: 'John Doe' }
|
193
|
-
* ```
|
194
|
-
*
|
195
|
-
*
|
196
|
-
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
197
|
-
* the {@link data} function. But if you want to remove the value from the internal representation, you can pass the
|
198
|
-
* argument `toInternal` as true.
|
199
|
-
* Then if you still want to remove the value from the representation, you will need to pass the argument
|
200
|
-
* `toRepresentation` as true as well.
|
201
|
-
*
|
202
|
-
* @returns The schema.
|
203
|
-
*/ omit(args) {
|
204
|
-
return super.omit(args);
|
205
|
-
}
|
206
|
-
/**
|
207
|
-
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
|
208
|
-
* source like a database. You should always return the schema after you save the value, that way we will always have
|
209
|
-
* the correct type of the schema after the save operation.
|
210
|
-
*
|
211
|
-
* You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
|
212
|
-
*
|
213
|
-
* @example
|
214
|
-
* ```typescript
|
215
|
-
* import * as p from '@palmares/schemas';
|
216
|
-
*
|
217
|
-
* import { User } from './models';
|
218
|
-
*
|
219
|
-
* const userSchema = p.object({
|
220
|
-
* id: p.number().optional(),
|
221
|
-
* name: p.string(),
|
222
|
-
* email: p.string().email(),
|
223
|
-
* }).onSave(async (value) => {
|
224
|
-
* // Create or update the user on the database using palmares models or any other library of your choice.
|
225
|
-
* if (value.id)
|
226
|
-
* await User.default.set(value, { search: { id: value.id } });
|
227
|
-
* else
|
228
|
-
* await User.default.set(value);
|
229
|
-
*
|
230
|
-
* return value;
|
231
|
-
* });
|
232
|
-
*
|
233
|
-
*
|
234
|
-
* // Then, on your controller, do something like this:
|
235
|
-
* const { isValid, save, errors } = await userSchema.validate(req.body);
|
236
|
-
* if (isValid) {
|
237
|
-
* const savedValue = await save();
|
238
|
-
* return Response.json(savedValue, { status: 201 });
|
239
|
-
* }
|
240
|
-
*
|
241
|
-
* return Response.json({ errors }, { status: 400 });
|
242
|
-
* ```
|
243
|
-
*
|
244
|
-
* @param callback - The callback that will be called to save the value on an external source.
|
245
|
-
*
|
246
|
-
* @returns The schema.
|
247
|
-
*/ onSave(callback) {
|
248
|
-
return super.onSave(callback);
|
249
|
-
}
|
250
|
-
/**
|
251
|
-
* This function is used to add a default value to the schema. If the value is either undefined or null, the default
|
252
|
-
* value will be used.
|
253
|
-
*
|
254
|
-
* @example
|
255
|
-
* ```typescript
|
256
|
-
* import * as p from '@palmares/schemas';
|
257
|
-
*
|
258
|
-
* const numberSchema = p.number().default(0);
|
259
|
-
*
|
260
|
-
* const { errors, parsed } = await numberSchema.parse(undefined);
|
261
|
-
*
|
262
|
-
* console.log(parsed); // 0
|
263
|
-
* ```
|
264
|
-
*/ default(defaultValueOrFunction) {
|
265
|
-
return super.default(defaultValueOrFunction);
|
266
|
-
}
|
267
|
-
/**
|
268
|
-
* This function let's you customize the schema your own way. After we translate the schema on the adapter we call
|
269
|
-
* this function to let you customize the custom schema your own way. Our API does not support passthrough?
|
270
|
-
* No problem, you can use this function to customize the zod schema.
|
271
|
-
*
|
272
|
-
* @example
|
273
|
-
* ```typescript
|
274
|
-
* import * as p from '@palmares/schemas';
|
275
|
-
*
|
276
|
-
* const numberSchema = p.number().extends((schema) => {
|
277
|
-
* return schema.nonnegative();
|
278
|
-
* });
|
279
|
-
*
|
280
|
-
* const { errors, parsed } = await numberSchema.parse(-1);
|
281
|
-
* // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
|
282
|
-
* console.log(errors);
|
283
|
-
* ```
|
284
|
-
*
|
285
|
-
* @param callback - The callback that will be called to customize the schema.
|
286
|
-
* @param toStringCallback - The callback that will be called to transform the schema to a string when you want to
|
287
|
-
* compile the underlying schema to a string so you can save it for future runs.
|
288
|
-
*
|
289
|
-
* @returns The schema.
|
290
|
-
*/ extends(callback, toStringCallback) {
|
291
|
-
return super.extends(callback, toStringCallback);
|
292
|
-
}
|
293
|
-
/**
|
294
|
-
* This function is used to transform the value to the representation of the schema. When using the {@link data}
|
295
|
-
* function. With this function you have full control to add data cleaning for example, transforming the data and
|
296
|
-
* whatever. Another use case is when you want to return deeply nested recursive data. The schema maps to itself.
|
297
|
-
*
|
298
|
-
* @example
|
299
|
-
* ```typescript
|
300
|
-
* import * as p from '@palmares/schemas';
|
301
|
-
*
|
302
|
-
* const recursiveSchema = p.object({
|
303
|
-
* id: p.number().optional(),
|
304
|
-
* name: p.string(),
|
305
|
-
* }).toRepresentation(async (value) => {
|
306
|
-
* return {
|
307
|
-
* id: value.id,
|
308
|
-
* name: value.name,
|
309
|
-
* children: await Promise.all(value.children.map(async (child) => await recursiveSchema.data(child)))
|
310
|
-
* }
|
311
|
-
* });
|
312
|
-
*
|
313
|
-
* const data = await recursiveSchema.data({
|
314
|
-
* id: 1,
|
315
|
-
* name: 'John Doe',
|
316
|
-
* });
|
317
|
-
* ```
|
318
|
-
*
|
319
|
-
* @example
|
320
|
-
* ```
|
321
|
-
* import * as p from '@palmares/schemas';
|
322
|
-
*
|
323
|
-
* const colorToRGBSchema = p.string().toRepresentation(async (value) => {
|
324
|
-
* switch (value) {
|
325
|
-
* case 'red': return { r: 255, g: 0, b: 0 };
|
326
|
-
* case 'green': return { r: 0, g: 255, b: 0 };
|
327
|
-
* case 'blue': return { r: 0, g: 0, b: 255 };
|
328
|
-
* default: return { r: 0, g: 0, b: 0 };
|
329
|
-
* }
|
330
|
-
* });
|
331
|
-
* ```
|
332
|
-
* @param toRepresentationCallback - The callback that will be called to transform the value to the representation.
|
333
|
-
*
|
334
|
-
* @returns The schema with a new return type
|
335
|
-
*/ toRepresentation(toRepresentationCallback) {
|
336
|
-
return super.toRepresentation(toRepresentationCallback);
|
337
|
-
}
|
338
|
-
/**
|
339
|
-
* This function is used to transform the value to the internal representation of the schema. This is useful when
|
340
|
-
* you want to transform the value to a type that the schema adapter can understand. For example, you might want
|
341
|
-
* to transform a string to a date. This is the function you use.
|
342
|
-
*
|
343
|
-
* @example
|
344
|
-
* ```typescript
|
345
|
-
* import * as p from '@palmares/schemas';
|
346
|
-
*
|
347
|
-
* const dateSchema = p.string().toInternal((value) => {
|
348
|
-
* return new Date(value);
|
349
|
-
* });
|
350
|
-
*
|
351
|
-
* const date = await dateSchema.parse('2021-01-01');
|
352
|
-
*
|
353
|
-
* console.log(date); // Date object
|
354
|
-
*
|
355
|
-
* const rgbToColorSchema = p.object({
|
356
|
-
* r: p.number().min(0).max(255),
|
357
|
-
* g: p.number().min(0).max(255),
|
358
|
-
* b: p.number().min(0).max(255),
|
359
|
-
* }).toInternal(async (value) => {
|
360
|
-
* if (value.r === 255 && value.g === 0 && value.b === 0) return 'red';
|
361
|
-
* if (value.r === 0 && value.g === 255 && value.b === 0) return 'green';
|
362
|
-
* if (value.r === 0 && value.g === 0 && value.b === 255) return 'blue';
|
363
|
-
* return `rgb(${value.r}, ${value.g}, ${value.b})`;
|
364
|
-
* });
|
365
|
-
* ```
|
366
|
-
*
|
367
|
-
* @param toInternalCallback - The callback that will be called to transform the value to the internal representation.
|
368
|
-
*
|
369
|
-
* @returns The schema with a new return type.
|
370
|
-
*/ toInternal(toInternalCallback) {
|
371
|
-
return super.toInternal(toInternalCallback);
|
372
|
-
}
|
373
|
-
/**
|
374
|
-
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
|
375
|
-
* you can convert that string to a date here BEFORE the validation. This pretty much transforms the value to a type
|
376
|
-
* that the schema adapter can understand.
|
377
|
-
*
|
378
|
-
* @example
|
379
|
-
* ```
|
380
|
-
* import * as p from '@palmares/schemas';
|
381
|
-
* import * as z from 'zod';
|
382
|
-
*
|
383
|
-
* const customRecordToMapSchema = p.schema().appendSchema(z.map()).toValidate(async (value) => {
|
384
|
-
* return new Map(value); // Before validating we transform the value to a map.
|
385
|
-
* });
|
386
|
-
*
|
387
|
-
* const { errors, parsed } = await customRecordToMapSchema.parse({ key: 'value' });
|
388
|
-
* ```
|
389
|
-
*
|
390
|
-
* @param toValidateCallback - The callback that will be called to validate the value.
|
391
|
-
*
|
392
|
-
* @returns The schema with a new return type.
|
393
|
-
*/ toValidate(toValidateCallback) {
|
394
|
-
return super.toValidate(toValidateCallback);
|
395
|
-
}
|
396
|
-
/**
|
397
|
-
* This will allow the value to be a string, it does not validate, it just parses inputs as strings and allows the
|
398
|
-
* result to be a string as well.
|
399
|
-
*
|
400
|
-
* @example
|
401
|
-
* ```ts
|
402
|
-
* boolean().allowString().parse('true') // true
|
403
|
-
* ```
|
404
|
-
*
|
405
|
-
* @returns - The schema instance
|
406
|
-
*/ allowString() {
|
407
|
-
this.__allowString = true;
|
408
|
-
this.__parsers.low.set('allowString', convertFromStringBuilder((value)=>{
|
409
|
-
return {
|
410
|
-
value: Boolean(value),
|
411
|
-
preventNextParsers: false
|
412
|
-
};
|
413
|
-
}));
|
414
|
-
return this;
|
415
|
-
}
|
416
|
-
/**
|
417
|
-
* Allows you to set the values that will be considered as true. This is useful when you have a string that can be
|
418
|
-
* 'T' or 'F' for example.
|
419
|
-
*
|
420
|
-
* @example
|
421
|
-
* ```ts
|
422
|
-
* boolean().trueValues(['T', 'Y', 1]).parse('T') // works and parses to true
|
423
|
-
* boolean().trueValues(['T', 'Y', 1]).parse('N') // fails
|
424
|
-
* ```
|
425
|
-
*
|
426
|
-
* @param values - The values on an array that will be considered as true.
|
427
|
-
*
|
428
|
-
* @returns - The schema instance
|
429
|
-
*/ trueValues(values) {
|
430
|
-
this.__trueValues = values;
|
431
|
-
this.__parsers.medium.set('trueValues', (value)=>{
|
432
|
-
const valueExistsInList = values.includes(value);
|
433
|
-
console.log('trueValues', valueExistsInList);
|
434
|
-
return {
|
435
|
-
preventNextParsers: valueExistsInList,
|
436
|
-
value: valueExistsInList ? true : value
|
437
|
-
};
|
438
|
-
});
|
439
|
-
return this;
|
440
|
-
}
|
441
|
-
/**
|
442
|
-
* Allows you to set the values that will be considered as false. This is useful when you have a string that can
|
443
|
-
* be 'N' or 0 for example.
|
444
|
-
*
|
445
|
-
* @example
|
446
|
-
* ```ts
|
447
|
-
* boolean().falseValues(['F', 'N', 0]).parse('F') // works and parses to false
|
448
|
-
* boolean().falseValues(['F', 'N', 0]).parse('Y') // fails and returns the value as is
|
449
|
-
* ```
|
450
|
-
*
|
451
|
-
* @param values - The values on an array that will be considered as true.
|
452
|
-
*
|
453
|
-
* @returns - The schema instance
|
454
|
-
*/ falseValues(values) {
|
455
|
-
this.__falseValues = values;
|
456
|
-
this.__parsers.medium.set('falseValues', (value)=>{
|
457
|
-
const valueExistsInList = values.includes(value);
|
458
|
-
console.log('falseValues', valueExistsInList);
|
459
|
-
return {
|
460
|
-
preventNextParsers: valueExistsInList,
|
461
|
-
value: valueExistsInList ? false : value
|
462
|
-
};
|
463
|
-
});
|
464
|
-
return this;
|
465
|
-
}
|
466
|
-
/**
|
467
|
-
* This will allow the value to be a number, it does not validate, it just parses inputs as number and allows the
|
468
|
-
* result to be a string as well.
|
469
|
-
*
|
470
|
-
* @example
|
471
|
-
* ```ts
|
472
|
-
* boolean().allowNumber().parse(1) // true
|
473
|
-
* ```
|
474
|
-
*
|
475
|
-
* @returns - The schema instance
|
476
|
-
*/ allowNumber() {
|
477
|
-
this.__allowNumber = true;
|
478
|
-
this.__parsers.low.set('allowNumber', (value)=>{
|
479
|
-
return {
|
480
|
-
value: typeof value === 'number' ? Boolean(value) : value,
|
481
|
-
preventNextParsers: typeof value === 'number'
|
482
|
-
};
|
483
|
-
});
|
484
|
-
return this;
|
485
|
-
}
|
486
|
-
/**
|
487
|
-
* This will validate if the value is equal to the value passed as argument. This way you can guarantee that the value
|
488
|
-
* is exactly what you want.
|
489
|
-
*
|
490
|
-
* @example
|
491
|
-
* ```ts
|
492
|
-
* boolean().is(true).parse(true) // true
|
493
|
-
* boolean().is(true).parse(false) // doesn't allow
|
494
|
-
* ```
|
495
|
-
*
|
496
|
-
* @param value - The value to compare with
|
497
|
-
* @param options - The options to be passed to the validation
|
498
|
-
* @param options.message - The message to be returned if the validation fails
|
499
|
-
*
|
500
|
-
* @returns - The schema instance
|
501
|
-
*/ is(value, options) {
|
502
|
-
this.__is = {
|
503
|
-
value,
|
504
|
-
message: typeof options?.message === 'string' ? options.message : `The value should be equal to ${value}`
|
505
|
-
};
|
506
|
-
this.__parsers.high.set('is', (valueFromParser)=>{
|
507
|
-
const isSetValue = value === valueFromParser;
|
508
|
-
return {
|
509
|
-
value: isSetValue ? valueFromParser : undefined,
|
510
|
-
preventNextParsers: true
|
511
|
-
};
|
512
|
-
});
|
513
|
-
return this;
|
514
|
-
}
|
515
|
-
static new() {
|
516
|
-
const returnValue = new BooleanSchema();
|
517
|
-
return returnValue;
|
518
|
-
}
|
519
|
-
}
|
520
|
-
export const boolean = ()=>BooleanSchema.new();
|