@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,633 @@
|
|
1
|
+
import Schema from './schema';
|
2
|
+
import { getDefaultAdapter } from '../conf';
|
3
|
+
import {
|
4
|
+
defaultTransform,
|
5
|
+
defaultTransformToAdapter,
|
6
|
+
transformSchemaAndCheckIfShouldBeHandledByFallbackOnComplexSchemas,
|
7
|
+
} from '../utils';
|
8
|
+
import { arrayValidation, maxLength, minLength, nonEmpty } from '../validators/array';
|
9
|
+
import { nullable, optional } from '../validators/schema';
|
10
|
+
import Validator from '../validators/utils';
|
11
|
+
|
12
|
+
import type { DefinitionsOfSchemaType, ExtractTypeFromArrayOfSchemas } from './types';
|
13
|
+
|
14
|
+
export default class ArraySchema<
|
15
|
+
TType extends {
|
16
|
+
input: any;
|
17
|
+
validate: any;
|
18
|
+
internal: any;
|
19
|
+
output: any;
|
20
|
+
representation: any;
|
21
|
+
} = {
|
22
|
+
input: any[];
|
23
|
+
output: any[];
|
24
|
+
internal: any[];
|
25
|
+
representation: any[];
|
26
|
+
validate: any[];
|
27
|
+
},
|
28
|
+
TDefinitions extends DefinitionsOfSchemaType = DefinitionsOfSchemaType,
|
29
|
+
TSchemas extends readonly [Schema, ...Schema[]] | [Schema[]] = [Schema[]],
|
30
|
+
> extends Schema<TType, TDefinitions> {
|
31
|
+
protected __schemas: readonly [Schema, ...Schema[]] | [Schema[]];
|
32
|
+
|
33
|
+
protected __minLength!: {
|
34
|
+
value: number;
|
35
|
+
inclusive: boolean;
|
36
|
+
message: string;
|
37
|
+
};
|
38
|
+
|
39
|
+
protected __maxLength!: {
|
40
|
+
value: number;
|
41
|
+
inclusive: boolean;
|
42
|
+
message: string;
|
43
|
+
};
|
44
|
+
|
45
|
+
protected __nonEmpty!: {
|
46
|
+
message: string;
|
47
|
+
};
|
48
|
+
|
49
|
+
constructor(...schemas: TSchemas) {
|
50
|
+
super();
|
51
|
+
this.__schemas = schemas;
|
52
|
+
}
|
53
|
+
|
54
|
+
protected async __transformToAdapter(options: Parameters<Schema['__transformToAdapter']>[0]): Promise<any> {
|
55
|
+
return defaultTransformToAdapter(
|
56
|
+
async (adapter) => {
|
57
|
+
const schemas = Array.isArray(this.__schemas[0]) ? this.__schemas[0] : this.__schemas;
|
58
|
+
const transformedSchemasAsString: string[] = [];
|
59
|
+
const transformedSchemas: any[] = [];
|
60
|
+
let shouldBeHandledByFallback = false;
|
61
|
+
|
62
|
+
await Promise.all(
|
63
|
+
(schemas as Schema[]).map(async (schema) => {
|
64
|
+
const [transformedData, shouldAddFallbackValidationForThisSchema] =
|
65
|
+
await transformSchemaAndCheckIfShouldBeHandledByFallbackOnComplexSchemas(schema, options);
|
66
|
+
|
67
|
+
if (shouldAddFallbackValidationForThisSchema) shouldBeHandledByFallback = true;
|
68
|
+
|
69
|
+
for (const transformedSchema of transformedData) {
|
70
|
+
transformedSchemasAsString.push(transformedSchema.asString);
|
71
|
+
transformedSchemas.push(transformedSchema.transformed);
|
72
|
+
}
|
73
|
+
})
|
74
|
+
);
|
75
|
+
|
76
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
77
|
+
if (shouldBeHandledByFallback)
|
78
|
+
Validator.createAndAppendFallback(
|
79
|
+
this,
|
80
|
+
arrayValidation(
|
81
|
+
Array.isArray(this.__schemas[0]) === false,
|
82
|
+
(Array.isArray(this.__schemas[0]) ? this.__schemas[0] : this.__schemas) as Schema<any, any>[]
|
83
|
+
)
|
84
|
+
);
|
85
|
+
|
86
|
+
return defaultTransform(
|
87
|
+
'array',
|
88
|
+
this,
|
89
|
+
adapter,
|
90
|
+
adapter.array,
|
91
|
+
() => ({
|
92
|
+
isTuple: Array.isArray(this.__schemas[0]) === false,
|
93
|
+
nullable: this.__nullable,
|
94
|
+
optional: this.__optional,
|
95
|
+
maxLength: this.__maxLength,
|
96
|
+
minLength: this.__minLength,
|
97
|
+
nonEmpty: this.__nonEmpty,
|
98
|
+
parsers: {
|
99
|
+
nullable: this.__nullable.allow,
|
100
|
+
optional: this.__optional.allow,
|
101
|
+
}
|
102
|
+
}),
|
103
|
+
{
|
104
|
+
optional,
|
105
|
+
nullable,
|
106
|
+
minLength,
|
107
|
+
maxLength,
|
108
|
+
nonEmpty,
|
109
|
+
},
|
110
|
+
{
|
111
|
+
shouldAddStringVersion: options.shouldAddStringVersion,
|
112
|
+
// eslint-disable-next-line ts/require-await
|
113
|
+
fallbackIfNotSupported: async () => [],
|
114
|
+
}
|
115
|
+
);
|
116
|
+
},
|
117
|
+
this.__transformedSchemas,
|
118
|
+
options,
|
119
|
+
'array'
|
120
|
+
);
|
121
|
+
}
|
122
|
+
|
123
|
+
/**
|
124
|
+
* This let's you refine the schema with custom validations. This is useful when you want to validate something that is not supported by default by the schema adapter.
|
125
|
+
*
|
126
|
+
* @example
|
127
|
+
* ```typescript
|
128
|
+
* import * as p from '@palmares/schemas';
|
129
|
+
*
|
130
|
+
* const numberSchema = p.number().refine((value) => {
|
131
|
+
* if (value < 0) return { code: 'invalid_number', message: 'The number should be greater than 0' };
|
132
|
+
* });
|
133
|
+
*
|
134
|
+
* const { errors, parsed } = await numberSchema.parse(-1);
|
135
|
+
*
|
136
|
+
* console.log(errors); // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
|
137
|
+
* ```
|
138
|
+
*
|
139
|
+
* @param refinementCallback - The callback that will be called to validate the value.
|
140
|
+
* @param options - Options for the refinement.
|
141
|
+
* @param options.isAsync - Whether the callback is async or not. Defaults to true.
|
142
|
+
*/
|
143
|
+
refine(
|
144
|
+
refinementCallback: (value: TType['input']) => Promise<void | undefined | { code: string; message: string }> | void | undefined | { code: string; message: string }
|
145
|
+
) {
|
146
|
+
return super.refine(refinementCallback) as unknown as ArraySchema<
|
147
|
+
{
|
148
|
+
input: TType['input'];
|
149
|
+
validate: TType['validate'];
|
150
|
+
internal: TType['internal'];
|
151
|
+
output: TType['output'];
|
152
|
+
representation: TType['representation'];
|
153
|
+
},
|
154
|
+
TDefinitions,
|
155
|
+
TSchemas
|
156
|
+
>;
|
157
|
+
}
|
158
|
+
|
159
|
+
/**
|
160
|
+
* Allows the value to be either undefined or null. Different from the `optional` method on other schemas, You can pass `outputOnly` as `true` to this method.
|
161
|
+
* This will allow you to pass `null` or `undefined` as a value on the {@link Schema.data} method, but it will not allow the value to be `null` or `undefined`.
|
162
|
+
* This is useful for typing purposes.
|
163
|
+
*
|
164
|
+
* @example
|
165
|
+
* ```typescript
|
166
|
+
* import * as p from '@palmares/schemas';
|
167
|
+
*
|
168
|
+
* const numberSchema = p.number().optional();
|
169
|
+
*
|
170
|
+
* const { errors, parsed } = await numberSchema.parse(undefined);
|
171
|
+
*
|
172
|
+
* console.log(parsed); // undefined
|
173
|
+
*
|
174
|
+
* const { errors, parsed } = await numberSchema.parse(null);
|
175
|
+
*
|
176
|
+
* console.log(parsed); // null
|
177
|
+
*
|
178
|
+
* const { errors, parsed } = await numberSchema.parse(1);
|
179
|
+
*
|
180
|
+
* console.log(parsed); // 1
|
181
|
+
*
|
182
|
+
* const companySchema = p.object({ id: p.number(), name: p.string() });
|
183
|
+
* const userSchema = p.object({
|
184
|
+
* id: p.number(),
|
185
|
+
* name: p.string(),
|
186
|
+
* company: companySchema.optional({ outputOnly: true })
|
187
|
+
* });
|
188
|
+
*
|
189
|
+
* const { errors, parsed } = await userSchema.data({ id: 1, name: 'John Doe' }); // Will not allow the company to be null or undefined on a typing level.
|
190
|
+
* const value = await userSchema.data({ id: 1, name: 'John Doe' }); // Will allow the company to be null or undefined on a typing level
|
191
|
+
* ```
|
192
|
+
*
|
193
|
+
* @returns - The schema we are working with.
|
194
|
+
*/
|
195
|
+
optional<TOutputOnly extends boolean = false>(options?: { message?: string; allow?: false, outputOnly?: TOutputOnly}) {
|
196
|
+
return (options?.outputOnly ? this : super.optional(options)) as unknown as ArraySchema<(TOutputOnly extends true ?
|
197
|
+
{
|
198
|
+
input: TType['input'];
|
199
|
+
validate: TType['validate'];
|
200
|
+
internal: TType['internal'];
|
201
|
+
output: TType['output'] | undefined | null;
|
202
|
+
representation: TType['representation'];
|
203
|
+
} :
|
204
|
+
{
|
205
|
+
input: TType['input'] | undefined | null;
|
206
|
+
validate: TType['validate'] | undefined | null;
|
207
|
+
internal: TType['internal'] | undefined | null;
|
208
|
+
output: TType['output'] | undefined | null;
|
209
|
+
representation: TType['representation'] | undefined | null;
|
210
|
+
}),
|
211
|
+
TDefinitions,
|
212
|
+
TSchemas
|
213
|
+
>;
|
214
|
+
}
|
215
|
+
|
216
|
+
/**
|
217
|
+
* Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value is NULL by setting
|
218
|
+
* the { message: 'Your custom message', allow: false } on the options.
|
219
|
+
*
|
220
|
+
* @example
|
221
|
+
* ```typescript
|
222
|
+
* import * as p from '@palmares/schemas';
|
223
|
+
*
|
224
|
+
* const numberSchema = p.number().nullable();
|
225
|
+
*
|
226
|
+
* const { errors, parsed } = await numberSchema.parse(null);
|
227
|
+
*
|
228
|
+
* console.log(parsed); // null
|
229
|
+
*
|
230
|
+
* const { errors, parsed } = await numberSchema.parse(undefined);
|
231
|
+
*
|
232
|
+
* console.log(errors); // [{ isValid: false, code: 'invalid_type', message: 'Invalid type', path: [] }]
|
233
|
+
* ```
|
234
|
+
*
|
235
|
+
* @param options - The options for the nullable function.
|
236
|
+
* @param options.message - The message to be shown when the value is not null. Defaults to 'Cannot be null'.
|
237
|
+
* @param options.allow - Whether the value can be null or not. Defaults to true.
|
238
|
+
*
|
239
|
+
* @returns The schema.
|
240
|
+
*/
|
241
|
+
nullable(options?: { message: string; allow: false }) {
|
242
|
+
return super.nullable(options) as unknown as ArraySchema<
|
243
|
+
{
|
244
|
+
input: TType['input'] | null;
|
245
|
+
validate: TType['validate'] | null;
|
246
|
+
internal: TType['internal'] | null;
|
247
|
+
output: TType['output'] | null;
|
248
|
+
representation: TType['representation'] | null;
|
249
|
+
},
|
250
|
+
TDefinitions,
|
251
|
+
TSchemas
|
252
|
+
>;
|
253
|
+
}
|
254
|
+
|
255
|
+
|
256
|
+
/**
|
257
|
+
* This method will remove the value from the representation of the schema. If the value is undefined it will keep that way
|
258
|
+
* otherwise it will set the value to undefined after it's validated.
|
259
|
+
* This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
|
260
|
+
* function. This will remove the value from the representation of the schema.
|
261
|
+
*
|
262
|
+
* By default, the value will be removed just from the representation, in other words, when you call the {@link data} function.
|
263
|
+
* But if you want to remove the value from the internal representation, you can pass the argument `toInternal` as true.
|
264
|
+
* Then if you still want to remove the value from the representation, you will need to pass the argument `toRepresentation` as true as well.
|
265
|
+
*
|
266
|
+
* @example
|
267
|
+
* ```typescript
|
268
|
+
* import * as p from '@palmares/schemas';
|
269
|
+
*
|
270
|
+
* const userSchema = p.object({
|
271
|
+
* id: p.number().optional(),
|
272
|
+
* name: p.string(),
|
273
|
+
* password: p.string().omit()
|
274
|
+
* });
|
275
|
+
*
|
276
|
+
* const user = await userSchema.data({
|
277
|
+
* id: 1,
|
278
|
+
* name: 'John Doe',
|
279
|
+
* password: '123456'
|
280
|
+
* });
|
281
|
+
*
|
282
|
+
* console.log(user); // { id: 1, name: 'John Doe' }
|
283
|
+
* ```
|
284
|
+
*
|
285
|
+
*
|
286
|
+
* @param args - By default, the value will be removed just from the representation, in other words, when you call the {@link data} function.
|
287
|
+
* But if you want to remove the value from the internal representation, you can pass the argument `toInternal` as true.
|
288
|
+
* Then if you still want to remove the value from the representation, you will need to pass the argument `toRepresentation` as true as well.
|
289
|
+
*
|
290
|
+
* @returns The schema.
|
291
|
+
*/
|
292
|
+
omit<
|
293
|
+
TToInternal extends boolean,
|
294
|
+
TToRepresentation extends boolean = boolean extends TToInternal ? true : false
|
295
|
+
>(args?: { toInternal?: TToInternal, toRepresentation?: TToRepresentation }) {
|
296
|
+
return super.omit(args) as unknown as ArraySchema<
|
297
|
+
{
|
298
|
+
input: TToInternal extends true ? TType['input'] | undefined : TType['input'];
|
299
|
+
validate: TToInternal extends true ? TType['validate'] | undefined : TType['validate'];
|
300
|
+
internal: TToInternal extends true ? undefined : TType['internal'];
|
301
|
+
output: TToRepresentation extends true ? TType['output'] | undefined : TType['output'];
|
302
|
+
representation: TToRepresentation extends true ? undefined : TType['representation'];
|
303
|
+
},
|
304
|
+
TDefinitions,
|
305
|
+
TSchemas
|
306
|
+
>;
|
307
|
+
}
|
308
|
+
|
309
|
+
/**
|
310
|
+
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external source
|
311
|
+
* like a database. You should always return the schema after you save the value, that way we will always have the correct type
|
312
|
+
* of the schema after the save operation.
|
313
|
+
*
|
314
|
+
* You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
|
315
|
+
*
|
316
|
+
* @example
|
317
|
+
* ```typescript
|
318
|
+
* import * as p from '@palmares/schemas';
|
319
|
+
*
|
320
|
+
* import { User } from './models';
|
321
|
+
*
|
322
|
+
* const userSchema = p.object({
|
323
|
+
* id: p.number().optional(),
|
324
|
+
* name: p.string(),
|
325
|
+
* email: p.string().email(),
|
326
|
+
* }).onSave(async (value) => {
|
327
|
+
* // Create or update the user on the database using palmares models or any other library of your choice.
|
328
|
+
* if (value.id)
|
329
|
+
* await User.default.set(value, { search: { id: value.id } });
|
330
|
+
* else
|
331
|
+
* await User.default.set(value);
|
332
|
+
*
|
333
|
+
* return value;
|
334
|
+
* });
|
335
|
+
*
|
336
|
+
*
|
337
|
+
* // Then, on your controller, do something like this:
|
338
|
+
* const { isValid, save, errors } = await userSchema.validate(req.body);
|
339
|
+
* if (isValid) {
|
340
|
+
* const savedValue = await save();
|
341
|
+
* return Response.json(savedValue, { status: 201 });
|
342
|
+
* }
|
343
|
+
*
|
344
|
+
* return Response.json({ errors }, { status: 400 });
|
345
|
+
* ```
|
346
|
+
*
|
347
|
+
* @param callback - The callback that will be called to save the value on an external source.
|
348
|
+
*
|
349
|
+
* @returns The schema.
|
350
|
+
*/
|
351
|
+
onSave(callback: (value: TType['internal']) => Promise<TType['output']> | TType['output']) {
|
352
|
+
return super.onSave(callback) as unknown as ArraySchema<
|
353
|
+
{
|
354
|
+
input: TType['input'];
|
355
|
+
validate: TType['validate'];
|
356
|
+
internal: TType['internal'];
|
357
|
+
output: TType['output'];
|
358
|
+
representation: TType['representation'];
|
359
|
+
},
|
360
|
+
TDefinitions & {
|
361
|
+
hasSave: true;
|
362
|
+
},
|
363
|
+
TSchemas
|
364
|
+
>;
|
365
|
+
}
|
366
|
+
|
367
|
+
|
368
|
+
/**
|
369
|
+
* This function is used to add a default value to the schema. If the value is either undefined or null, the default value will be used.
|
370
|
+
*
|
371
|
+
* @example
|
372
|
+
* ```typescript
|
373
|
+
* import * as p from '@palmares/schemas';
|
374
|
+
*
|
375
|
+
* const numberSchema = p.number().default(0);
|
376
|
+
*
|
377
|
+
* const { errors, parsed } = await numberSchema.parse(undefined);
|
378
|
+
*
|
379
|
+
* console.log(parsed); // 0
|
380
|
+
* ```
|
381
|
+
*/
|
382
|
+
default<TDefaultValue extends TType['input'] | (() => Promise<TType['input']>)>(
|
383
|
+
defaultValueOrFunction: TDefaultValue
|
384
|
+
) {
|
385
|
+
return super.default(defaultValueOrFunction) as unknown as ArraySchema<
|
386
|
+
{
|
387
|
+
input: TType['input'] | undefined | null;
|
388
|
+
validate: TType['validate'];
|
389
|
+
internal: TType['internal'];
|
390
|
+
output: TType['output'] | undefined | null;
|
391
|
+
representation: TType['representation'];
|
392
|
+
},
|
393
|
+
TDefinitions,
|
394
|
+
TSchemas
|
395
|
+
>;
|
396
|
+
}
|
397
|
+
|
398
|
+
/**
|
399
|
+
* This function let's you customize the schema your own way. After we translate the schema on the adapter we call this function to let you customize
|
400
|
+
* the custom schema your own way. Our API does not support passthrough? No problem, you can use this function to customize the zod schema.
|
401
|
+
*
|
402
|
+
* @example
|
403
|
+
* ```typescript
|
404
|
+
* import * as p from '@palmares/schemas';
|
405
|
+
*
|
406
|
+
* const numberSchema = p.number().extends((schema) => {
|
407
|
+
* return schema.nonnegative();
|
408
|
+
* });
|
409
|
+
*
|
410
|
+
* const { errors, parsed } = await numberSchema.parse(-1);
|
411
|
+
*
|
412
|
+
* console.log(errors); // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
|
413
|
+
* ```
|
414
|
+
*
|
415
|
+
* @param callback - The callback that will be called to customize the schema.
|
416
|
+
* @param toStringCallback - The callback that will be called to transform the schema to a string when you want to compile the underlying schema
|
417
|
+
* to a string so you can save it for future runs.
|
418
|
+
*
|
419
|
+
* @returns The schema.
|
420
|
+
*/
|
421
|
+
extends(
|
422
|
+
callback: (
|
423
|
+
schema: Awaited<ReturnType<NonNullable<TDefinitions['schemaAdapter']['object']>['translate']>>
|
424
|
+
) => Awaited<ReturnType<NonNullable<TDefinitions['schemaAdapter']['field']>['translate']>> | any,
|
425
|
+
toStringCallback?: (schemaAsString: string) => string
|
426
|
+
) {
|
427
|
+
return super.extends(callback, toStringCallback);
|
428
|
+
}
|
429
|
+
|
430
|
+
/**
|
431
|
+
* This function is used to transform the value to the representation of the schema. When using the {@link data} function. With this function you have full
|
432
|
+
* control to add data cleaning for example, transforming the data and whatever. Another use case is when you want to return deeply nested recursive data.
|
433
|
+
* The schema maps to itself.
|
434
|
+
*
|
435
|
+
* @example
|
436
|
+
* ```typescript
|
437
|
+
* import * as p from '@palmares/schemas';
|
438
|
+
*
|
439
|
+
* const recursiveSchema = p.object({
|
440
|
+
* id: p.number().optional(),
|
441
|
+
* name: p.string(),
|
442
|
+
* }).toRepresentation(async (value) => {
|
443
|
+
* return {
|
444
|
+
* id: value.id,
|
445
|
+
* name: value.name,
|
446
|
+
* children: await Promise.all(value.children.map(async (child) => await recursiveSchema.data(child)))
|
447
|
+
* }
|
448
|
+
* });
|
449
|
+
*
|
450
|
+
* const data = await recursiveSchema.data({
|
451
|
+
* id: 1,
|
452
|
+
* name: 'John Doe',
|
453
|
+
* });
|
454
|
+
* ```
|
455
|
+
*
|
456
|
+
* @example
|
457
|
+
* ```
|
458
|
+
* import * as p from '@palmares/schemas';
|
459
|
+
*
|
460
|
+
* const colorToRGBSchema = p.string().toRepresentation(async (value) => {
|
461
|
+
* switch (value) {
|
462
|
+
* case 'red': return { r: 255, g: 0, b: 0 };
|
463
|
+
* case 'green': return { r: 0, g: 255, b: 0 };
|
464
|
+
* case 'blue': return { r: 0, g: 0, b: 255 };
|
465
|
+
* default: return { r: 0, g: 0, b: 0 };
|
466
|
+
* }
|
467
|
+
* });
|
468
|
+
* ```
|
469
|
+
* @param toRepresentationCallback - The callback that will be called to transform the value to the representation.
|
470
|
+
*
|
471
|
+
* @returns The schema with a new return type
|
472
|
+
*/
|
473
|
+
toRepresentation<TRepresentation>(
|
474
|
+
toRepresentationCallback: (value: TType['representation']) => Promise<TRepresentation>
|
475
|
+
) {
|
476
|
+
return super.toRepresentation(toRepresentationCallback) as unknown as ArraySchema<
|
477
|
+
{
|
478
|
+
input: TType['input'];
|
479
|
+
validate: TType['validate'];
|
480
|
+
internal: TType['internal'];
|
481
|
+
output: TType['output'];
|
482
|
+
representation: TRepresentation;
|
483
|
+
},
|
484
|
+
TDefinitions,
|
485
|
+
TSchemas
|
486
|
+
>;
|
487
|
+
}
|
488
|
+
|
489
|
+
/**
|
490
|
+
* This function is used to transform the value to the internal representation of the schema. This is useful when you want to transform the value
|
491
|
+
* to a type that the schema adapter can understand. For example, you might want to transform a string to a date. This is the function you use.
|
492
|
+
*
|
493
|
+
* @example
|
494
|
+
* ```typescript
|
495
|
+
* import * as p from '@palmares/schemas';
|
496
|
+
*
|
497
|
+
* const dateSchema = p.string().toInternal((value) => {
|
498
|
+
* return new Date(value);
|
499
|
+
* });
|
500
|
+
*
|
501
|
+
* const date = await dateSchema.parse('2021-01-01');
|
502
|
+
*
|
503
|
+
* console.log(date); // Date object
|
504
|
+
*
|
505
|
+
* const rgbToColorSchema = p.object({
|
506
|
+
* r: p.number().min(0).max(255),
|
507
|
+
* g: p.number().min(0).max(255),
|
508
|
+
* b: p.number().min(0).max(255),
|
509
|
+
* }).toInternal(async (value) => {
|
510
|
+
* if (value.r === 255 && value.g === 0 && value.b === 0) return 'red';
|
511
|
+
* if (value.r === 0 && value.g === 255 && value.b === 0) return 'green';
|
512
|
+
* if (value.r === 0 && value.g === 0 && value.b === 255) return 'blue';
|
513
|
+
* return `rgb(${value.r}, ${value.g}, ${value.b})`;
|
514
|
+
* });
|
515
|
+
* ```
|
516
|
+
*
|
517
|
+
* @param toInternalCallback - The callback that will be called to transform the value to the internal representation.
|
518
|
+
*
|
519
|
+
* @returns The schema with a new return type.
|
520
|
+
*/
|
521
|
+
toInternal<TInternal>(toInternalCallback: (value: TType['validate']) => Promise<TInternal>) {
|
522
|
+
return super.toInternal(toInternalCallback) as unknown as ArraySchema<
|
523
|
+
{
|
524
|
+
input: TType['input'];
|
525
|
+
validate: TType['validate'];
|
526
|
+
internal: TInternal;
|
527
|
+
output: TType['output'];
|
528
|
+
representation: TType['representation'];
|
529
|
+
},
|
530
|
+
TDefinitions,
|
531
|
+
TSchemas
|
532
|
+
>;
|
533
|
+
}
|
534
|
+
|
535
|
+
/**
|
536
|
+
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a string, you can convert that string to a date
|
537
|
+
* here BEFORE the validation. This pretty much transforms the value to a type that the schema adapter can understand.
|
538
|
+
*
|
539
|
+
* @example
|
540
|
+
* ```
|
541
|
+
* import * as p from '@palmares/schemas';
|
542
|
+
* import * as z from 'zod';
|
543
|
+
*
|
544
|
+
* const customRecordToMapSchema = p.schema().appendSchema(z.map()).toValidate(async (value) => {
|
545
|
+
* return new Map(value); // Before validating we transform the value to a map.
|
546
|
+
* });
|
547
|
+
*
|
548
|
+
* const { errors, parsed } = await customRecordToMapSchema.parse({ key: 'value' });
|
549
|
+
* ```
|
550
|
+
*
|
551
|
+
* @param toValidateCallback - The callback that will be called to validate the value.
|
552
|
+
*
|
553
|
+
* @returns The schema with a new return type.
|
554
|
+
*/
|
555
|
+
toValidate<TValidate>(toValidateCallback: (value: TType['input']) => Promise<TValidate> | TValidate) {
|
556
|
+
return super.toValidate(toValidateCallback) as unknown as ArraySchema<
|
557
|
+
{
|
558
|
+
input: TType['input'];
|
559
|
+
validate: TValidate;
|
560
|
+
internal: TType['internal'];
|
561
|
+
output: TType['output'];
|
562
|
+
representation: TType['representation'];
|
563
|
+
},
|
564
|
+
TDefinitions,
|
565
|
+
TSchemas
|
566
|
+
>;
|
567
|
+
}
|
568
|
+
|
569
|
+
minLength(value: number, inclusive = true, message?: string) {
|
570
|
+
message = message || `The array must have a minimum length of ${value}`;
|
571
|
+
this.__minLength = {
|
572
|
+
value: value,
|
573
|
+
inclusive: inclusive,
|
574
|
+
message: message,
|
575
|
+
};
|
576
|
+
|
577
|
+
return this;
|
578
|
+
}
|
579
|
+
|
580
|
+
maxLength(value: number, inclusive = true, message?: string) {
|
581
|
+
message = message || `The array must have a maximum length of ${value}`;
|
582
|
+
this.__maxLength = {
|
583
|
+
value: value,
|
584
|
+
inclusive: inclusive,
|
585
|
+
message: message,
|
586
|
+
};
|
587
|
+
|
588
|
+
return this;
|
589
|
+
}
|
590
|
+
|
591
|
+
nonEmpty(message?: string) {
|
592
|
+
message = message || 'The array must not be empty';
|
593
|
+
this.__nonEmpty = {
|
594
|
+
message: message,
|
595
|
+
};
|
596
|
+
|
597
|
+
return this;
|
598
|
+
}
|
599
|
+
|
600
|
+
static new<
|
601
|
+
TSchemas extends readonly [Schema, ...Schema[]] | [Schema[]],
|
602
|
+
TDefinitions extends DefinitionsOfSchemaType = DefinitionsOfSchemaType,
|
603
|
+
>(...schemas: TSchemas) {
|
604
|
+
const returnValue = new ArraySchema<
|
605
|
+
{
|
606
|
+
input: ExtractTypeFromArrayOfSchemas<TSchemas, 'input'>;
|
607
|
+
validate: ExtractTypeFromArrayOfSchemas<TSchemas, 'validate'>;
|
608
|
+
internal: ExtractTypeFromArrayOfSchemas<TSchemas, 'internal'>;
|
609
|
+
output: ExtractTypeFromArrayOfSchemas<TSchemas, 'output'>;
|
610
|
+
representation: ExtractTypeFromArrayOfSchemas<TSchemas, 'representation'>;
|
611
|
+
},
|
612
|
+
TDefinitions,
|
613
|
+
TSchemas
|
614
|
+
>(...schemas);
|
615
|
+
|
616
|
+
const adapterInstance = getDefaultAdapter();
|
617
|
+
|
618
|
+
returnValue.__transformedSchemas[adapterInstance.constructor.name] = {
|
619
|
+
transformed: false,
|
620
|
+
adapter: adapterInstance,
|
621
|
+
schemas: [],
|
622
|
+
};
|
623
|
+
|
624
|
+
return returnValue;
|
625
|
+
}
|
626
|
+
}
|
627
|
+
|
628
|
+
export const array = <
|
629
|
+
TSchemas extends readonly [Schema, ...Schema[]] | [Schema[]],
|
630
|
+
TDefinitions extends DefinitionsOfSchemaType,
|
631
|
+
>(
|
632
|
+
...schemas: TSchemas
|
633
|
+
) => ArraySchema.new<TSchemas, TDefinitions>(...schemas);
|