@palmares/schemas 0.0.1 → 0.1.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 +12 -410
- package/.turbo/turbo-build.log +13 -4
- package/CHANGELOG.md +26 -0
- package/__tests__/.drizzle/migrations/0000_skinny_harrier.sql +22 -0
- package/__tests__/.drizzle/migrations/meta/0000_snapshot.json +156 -0
- package/__tests__/.drizzle/migrations/meta/_journal.json +13 -0
- package/__tests__/.drizzle/schema.ts +35 -0
- package/__tests__/drizzle.config.ts +11 -0
- package/__tests__/eslint.config.js +10 -0
- package/__tests__/manage.ts +5 -0
- package/__tests__/node_modules/.bin/drizzle-kit +17 -0
- package/__tests__/node_modules/.bin/esbuild +14 -0
- package/__tests__/node_modules/.bin/tsc +17 -0
- package/__tests__/node_modules/.bin/tsserver +17 -0
- package/__tests__/node_modules/.bin/tsx +17 -0
- package/__tests__/package.json +36 -0
- package/__tests__/sqlite.db +0 -0
- package/__tests__/src/core/array.test.ts +130 -0
- package/__tests__/src/core/boolean.test.ts +66 -0
- package/__tests__/src/core/datetime.test.ts +102 -0
- package/__tests__/src/core/index.ts +35 -0
- package/__tests__/src/core/model.test.ts +260 -0
- package/__tests__/src/core/models.ts +50 -0
- package/__tests__/src/core/numbers.test.ts +177 -0
- package/__tests__/src/core/object.test.ts +198 -0
- package/__tests__/src/core/string.test.ts +222 -0
- package/__tests__/src/core/test.test.ts +59 -0
- package/__tests__/src/core/types.test.ts +97 -0
- package/__tests__/src/core/union.test.ts +99 -0
- package/__tests__/src/settings.ts +71 -0
- package/__tests__/tsconfig.json +11 -0
- package/dist/cjs/src/adapter/fields/index.js +2 -2
- package/dist/cjs/src/adapter/fields/object.js +9 -0
- package/dist/cjs/src/adapter/index.js +1 -0
- package/dist/cjs/src/constants.js +1 -7
- package/dist/cjs/src/domain.js +146 -1
- package/dist/cjs/src/index.js +69 -74
- package/dist/cjs/src/model.js +206 -206
- package/dist/cjs/src/schema/array.js +185 -58
- package/dist/cjs/src/schema/boolean.js +105 -44
- package/dist/cjs/src/schema/datetime.js +104 -38
- package/dist/cjs/src/schema/number.js +134 -114
- package/dist/cjs/src/schema/object.js +106 -43
- package/dist/cjs/src/schema/schema.js +123 -75
- package/dist/cjs/src/schema/string.js +152 -58
- package/dist/cjs/src/schema/union.js +412 -290
- package/dist/cjs/src/utils.js +42 -15
- package/dist/cjs/src/validators/array.js +6 -1
- package/dist/cjs/src/validators/boolean.js +2 -0
- package/dist/cjs/src/validators/datetime.js +4 -0
- package/dist/cjs/src/validators/number.js +12 -40
- package/dist/cjs/src/validators/object.js +1 -0
- package/dist/cjs/src/validators/schema.js +5 -1
- package/dist/cjs/src/validators/string.js +30 -2
- package/dist/cjs/src/validators/union.js +5 -4
- package/dist/cjs/src/validators/utils.js +99 -27
- package/dist/cjs/tsconfig.types.tsbuildinfo +1 -1
- package/dist/cjs/types/adapter/fields/array.d.ts +2 -2
- package/dist/cjs/types/adapter/fields/array.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/boolean.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/datetime.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/index.d.ts +2 -2
- package/dist/cjs/types/adapter/fields/index.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/number.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/object.d.ts +2 -1
- package/dist/cjs/types/adapter/fields/object.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/string.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/union.d.ts.map +1 -1
- package/dist/cjs/types/adapter/index.d.ts +1 -0
- package/dist/cjs/types/adapter/index.d.ts.map +1 -1
- package/dist/cjs/types/adapter/types.d.ts +28 -18
- package/dist/cjs/types/adapter/types.d.ts.map +1 -1
- package/dist/cjs/types/constants.d.ts +0 -1
- package/dist/cjs/types/constants.d.ts.map +1 -1
- package/dist/cjs/types/domain.d.ts +5 -4
- package/dist/cjs/types/domain.d.ts.map +1 -1
- package/dist/cjs/types/index.d.ts +78 -55
- package/dist/cjs/types/index.d.ts.map +1 -1
- package/dist/cjs/types/model.d.ts +17 -17
- package/dist/cjs/types/model.d.ts.map +1 -1
- package/dist/cjs/types/schema/array.d.ts +168 -47
- package/dist/cjs/types/schema/array.d.ts.map +1 -1
- package/dist/cjs/types/schema/boolean.d.ts +103 -44
- package/dist/cjs/types/schema/boolean.d.ts.map +1 -1
- package/dist/cjs/types/schema/datetime.d.ts +90 -30
- package/dist/cjs/types/schema/datetime.d.ts.map +1 -1
- package/dist/cjs/types/schema/number.d.ts +133 -125
- package/dist/cjs/types/schema/number.d.ts.map +1 -1
- package/dist/cjs/types/schema/object.d.ts +104 -35
- package/dist/cjs/types/schema/object.d.ts.map +1 -1
- package/dist/cjs/types/schema/schema.d.ts +62 -44
- package/dist/cjs/types/schema/schema.d.ts.map +1 -1
- package/dist/cjs/types/schema/string.d.ts +152 -65
- package/dist/cjs/types/schema/string.d.ts.map +1 -1
- package/dist/cjs/types/schema/types.d.ts +11 -2
- package/dist/cjs/types/schema/types.d.ts.map +1 -1
- package/dist/cjs/types/schema/union.d.ts +133 -40
- package/dist/cjs/types/schema/union.d.ts.map +1 -1
- package/dist/cjs/types/types.d.ts +35 -0
- package/dist/cjs/types/types.d.ts.map +1 -1
- package/dist/cjs/types/utils.d.ts +41 -27
- package/dist/cjs/types/utils.d.ts.map +1 -1
- package/dist/cjs/types/validators/array.d.ts.map +1 -1
- package/dist/cjs/types/validators/boolean.d.ts.map +1 -1
- package/dist/cjs/types/validators/datetime.d.ts.map +1 -1
- package/dist/cjs/types/validators/number.d.ts +5 -6
- package/dist/cjs/types/validators/number.d.ts.map +1 -1
- package/dist/cjs/types/validators/object.d.ts.map +1 -1
- package/dist/cjs/types/validators/schema.d.ts +2 -2
- package/dist/cjs/types/validators/schema.d.ts.map +1 -1
- package/dist/cjs/types/validators/string.d.ts +9 -9
- package/dist/cjs/types/validators/string.d.ts.map +1 -1
- package/dist/cjs/types/validators/utils.d.ts +44 -27
- package/dist/cjs/types/validators/utils.d.ts.map +1 -1
- package/dist/esm/src/adapter/fields/index.js +2 -2
- package/dist/esm/src/adapter/fields/object.js +6 -0
- package/dist/esm/src/adapter/index.js +1 -0
- package/dist/esm/src/constants.js +1 -2
- package/dist/esm/src/domain.js +11 -1
- package/dist/esm/src/index.js +38 -73
- package/dist/esm/src/model.js +83 -78
- package/dist/esm/src/schema/array.js +136 -54
- package/dist/esm/src/schema/boolean.js +98 -44
- package/dist/esm/src/schema/datetime.js +91 -38
- package/dist/esm/src/schema/number.js +127 -110
- package/dist/esm/src/schema/object.js +98 -43
- package/dist/esm/src/schema/schema.js +102 -67
- package/dist/esm/src/schema/string.js +147 -59
- package/dist/esm/src/schema/union.js +119 -40
- package/dist/esm/src/types.js +14 -1
- package/dist/esm/src/utils.js +56 -27
- package/dist/esm/src/validators/array.js +6 -1
- package/dist/esm/src/validators/boolean.js +2 -0
- package/dist/esm/src/validators/datetime.js +4 -0
- package/dist/esm/src/validators/number.js +9 -23
- package/dist/esm/src/validators/object.js +1 -0
- package/dist/esm/src/validators/schema.js +5 -1
- package/dist/esm/src/validators/string.js +30 -2
- package/dist/esm/src/validators/union.js +5 -4
- package/dist/esm/src/validators/utils.js +62 -36
- package/package.json +3 -3
- package/src/adapter/fields/array.ts +2 -2
- package/src/adapter/fields/boolean.ts +3 -8
- package/src/adapter/fields/datetime.ts +3 -9
- package/src/adapter/fields/index.ts +11 -11
- package/src/adapter/fields/number.ts +3 -9
- package/src/adapter/fields/object.ts +13 -10
- package/src/adapter/fields/string.ts +3 -9
- package/src/adapter/fields/union.ts +3 -9
- package/src/adapter/index.ts +1 -0
- package/src/adapter/types.ts +60 -45
- package/src/constants.ts +1 -3
- package/src/domain.ts +15 -1
- package/src/index.ts +189 -211
- package/src/model.ts +119 -115
- package/src/schema/array.ts +274 -90
- package/src/schema/boolean.ts +145 -60
- package/src/schema/datetime.ts +133 -49
- package/src/schema/number.ts +210 -173
- package/src/schema/object.ts +167 -74
- package/src/schema/schema.ts +205 -126
- package/src/schema/string.ts +221 -94
- package/src/schema/types.ts +44 -16
- package/src/schema/union.ts +193 -68
- package/src/types.ts +53 -0
- package/src/utils.ts +115 -57
- package/src/validators/array.ts +46 -27
- package/src/validators/boolean.ts +13 -7
- package/src/validators/datetime.ts +24 -16
- package/src/validators/number.ts +53 -63
- package/src/validators/object.ts +6 -5
- package/src/validators/schema.ts +33 -25
- package/src/validators/string.ts +122 -59
- package/src/validators/union.ts +8 -8
- package/src/validators/utils.ts +67 -42
package/src/schema/types.ts
CHANGED
@@ -13,7 +13,7 @@ export type OnlyFieldAdaptersFromSchemaAdapter = keyof {
|
|
13
13
|
};
|
14
14
|
|
15
15
|
export type DefinitionsOfSchemaType = {
|
16
|
-
schemaType: 'array' | 'object' | 'string' | 'number' | 'boolean' | 'union' | 'datetime' | 'field' |'datetime';
|
16
|
+
schemaType: 'array' | 'object' | 'string' | 'number' | 'boolean' | 'union' | 'datetime' | 'field' | 'datetime';
|
17
17
|
schemaAdapter: SchemaAdapter;
|
18
18
|
hasSave?: boolean;
|
19
19
|
};
|
@@ -22,6 +22,7 @@ export type ValidationFallbackCallbackType = Validator['fallbacks'][number];
|
|
22
22
|
export type ValidationFallbackCallbackReturnType = {
|
23
23
|
parsed: any;
|
24
24
|
errors: {
|
25
|
+
received: any;
|
25
26
|
isValid: boolean;
|
26
27
|
code: ErrorCodes;
|
27
28
|
message: string;
|
@@ -31,13 +32,14 @@ export type ValidationFallbackCallbackReturnType = {
|
|
31
32
|
};
|
32
33
|
export type ValidationFallbackReturnType = {
|
33
34
|
type: ValidatorTypes;
|
35
|
+
name: string;
|
34
36
|
callback: ValidationFallbackCallbackType;
|
35
37
|
};
|
36
38
|
|
37
39
|
type TypesOfSchema = Schema extends Schema<infer TType, any> ? TType : never;
|
38
40
|
type ExtractTypeFromSchemaByTypeOfSchema<
|
39
41
|
TSchema extends Schema,
|
40
|
-
TTypeToExtract extends keyof TypesOfSchema = 'input'
|
42
|
+
TTypeToExtract extends keyof TypesOfSchema = 'input'
|
41
43
|
> = TSchema extends
|
42
44
|
| Schema<
|
43
45
|
{
|
@@ -83,16 +85,16 @@ type ExtractTypeFromSchemaByTypeOfSchema<
|
|
83
85
|
? TTypeToExtract extends 'input'
|
84
86
|
? TInputType
|
85
87
|
: TTypeToExtract extends 'validate'
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
88
|
+
? TValidateType
|
89
|
+
: TTypeToExtract extends 'internal'
|
90
|
+
? TInternalType
|
91
|
+
: TTypeToExtract extends 'output'
|
92
|
+
? TOutputType
|
93
|
+
: TRepresentationType
|
92
94
|
: never;
|
93
95
|
export type ExtractTypeFromObjectOfSchemas<
|
94
96
|
TData extends Record<string, Schema>,
|
95
|
-
TTypeToExtract extends keyof TypesOfSchema = 'input'
|
97
|
+
TTypeToExtract extends keyof TypesOfSchema = 'input'
|
96
98
|
> = {
|
97
99
|
[key in keyof TData as undefined extends ExtractTypeFromSchemaByTypeOfSchema<TData[key], TTypeToExtract>
|
98
100
|
? never
|
@@ -103,10 +105,35 @@ export type ExtractTypeFromObjectOfSchemas<
|
|
103
105
|
: never]?: ExtractTypeFromSchemaByTypeOfSchema<TData[key], TTypeToExtract>;
|
104
106
|
};
|
105
107
|
|
108
|
+
export type ExtractTypeFromUnionOfSchemas<
|
109
|
+
TSchemas extends readonly Schema[] = [],
|
110
|
+
TType extends 'input' | 'output' | 'representation' | 'internal' | 'validate' = 'input'
|
111
|
+
> = TSchemas extends readonly [infer TFirstSchema, ...infer TRestOfSchemas]
|
112
|
+
? TFirstSchema extends Schema<{
|
113
|
+
input: infer TInput;
|
114
|
+
internal: infer TInternal;
|
115
|
+
output: infer TOutput;
|
116
|
+
representation: infer TRepresentation;
|
117
|
+
validate: infer TValidate;
|
118
|
+
}>
|
119
|
+
?
|
120
|
+
| (TType extends 'output'
|
121
|
+
? TOutput
|
122
|
+
: TType extends 'representation'
|
123
|
+
? TRepresentation
|
124
|
+
: TType extends 'internal'
|
125
|
+
? TInternal
|
126
|
+
: TType extends 'validate'
|
127
|
+
? TValidate
|
128
|
+
: TInput)
|
129
|
+
| ExtractTypeFromUnionOfSchemas<TRestOfSchemas extends readonly Schema[] ? TRestOfSchemas : [], TType>
|
130
|
+
: unknown
|
131
|
+
: never;
|
132
|
+
|
106
133
|
export type ExtractTypeFromArrayOfSchemas<
|
107
|
-
TSchemas extends readonly [Schema, ...Schema[]] | [Schema
|
134
|
+
TSchemas extends readonly [Schema, ...Schema[]] | [[Schema]],
|
108
135
|
TTypeToExtract extends keyof TypesOfSchema = 'input',
|
109
|
-
TResult extends any[] = []
|
136
|
+
TResult extends any[] = []
|
110
137
|
> = TSchemas extends readonly [infer TSchema, ...infer TRestSchemas]
|
111
138
|
? TSchema extends Schema
|
112
139
|
? TRestSchemas extends readonly [Schema, ...Schema[]]
|
@@ -116,11 +143,12 @@ export type ExtractTypeFromArrayOfSchemas<
|
|
116
143
|
[...TResult, ExtractTypeFromSchemaByTypeOfSchema<TSchema, TTypeToExtract>]
|
117
144
|
>
|
118
145
|
: [...TResult, ExtractTypeFromSchemaByTypeOfSchema<TSchema, TTypeToExtract>]
|
119
|
-
: TSchemas extends [infer
|
120
|
-
|
121
|
-
|
146
|
+
: TSchemas extends [[infer TSchema]]
|
147
|
+
? TSchema extends Schema
|
148
|
+
? ExtractTypeFromSchemaByTypeOfSchema<TSchema, TTypeToExtract>[]
|
149
|
+
: never
|
122
150
|
: never
|
123
|
-
: never
|
124
151
|
: never;
|
125
152
|
|
126
|
-
export type ExtractUnionTypesFromSchemas<TSchemas extends readonly Schema<any, any>[]> =
|
153
|
+
export type ExtractUnionTypesFromSchemas<TSchemas extends readonly Schema<any, any>[]> =
|
154
|
+
TSchemas[number] extends Schema<infer TType, any> ? TType : never;
|
package/src/schema/union.ts
CHANGED
@@ -1,39 +1,49 @@
|
|
1
|
-
|
1
|
+
import { number } from '.';
|
2
2
|
import Schema from './schema';
|
3
|
+
import { string } from './string';
|
3
4
|
import { getDefaultAdapter } from '../conf';
|
4
5
|
import {
|
5
6
|
defaultTransform,
|
6
7
|
defaultTransformToAdapter,
|
7
|
-
transformSchemaAndCheckIfShouldBeHandledByFallbackOnComplexSchemas
|
8
|
+
transformSchemaAndCheckIfShouldBeHandledByFallbackOnComplexSchemas
|
8
9
|
} from '../utils';
|
9
10
|
import { unionValidation } from '../validators/union';
|
10
11
|
import Validator from '../validators/utils';
|
11
12
|
|
12
|
-
import type { DefinitionsOfSchemaType } from './types';
|
13
|
+
import type { DefinitionsOfSchemaType, ExtractTypeFromUnionOfSchemas } from './types';
|
14
|
+
import type SchemaAdapter from '../adapter';
|
13
15
|
import type FieldAdapter from '../adapter/fields';
|
14
16
|
import type { Narrow } from '@palmares/core';
|
15
17
|
|
16
18
|
export default class UnionSchema<
|
17
19
|
TType extends {
|
18
|
-
input:
|
19
|
-
validate:
|
20
|
-
internal:
|
21
|
-
representation:
|
22
|
-
output:
|
20
|
+
input: unknown;
|
21
|
+
validate: unknown;
|
22
|
+
internal: unknown;
|
23
|
+
representation: unknown;
|
24
|
+
output: unknown;
|
23
25
|
} = {
|
24
|
-
input:
|
25
|
-
output:
|
26
|
-
validate:
|
27
|
-
internal:
|
28
|
-
representation:
|
26
|
+
input: unknown;
|
27
|
+
output: unknown;
|
28
|
+
validate: unknown;
|
29
|
+
internal: unknown;
|
30
|
+
representation: unknown;
|
29
31
|
},
|
30
32
|
TDefinitions extends DefinitionsOfSchemaType = DefinitionsOfSchemaType,
|
31
33
|
TSchemas extends readonly [Schema<any, any>, Schema<any, any>, ...Schema<any, any>[]] = [
|
32
34
|
Schema<any, any>,
|
33
35
|
Schema<any, any>,
|
34
|
-
...Schema<any, any>[]
|
35
|
-
]
|
36
|
+
...Schema<any, any>[]
|
37
|
+
]
|
36
38
|
> extends Schema<TType, TDefinitions> {
|
39
|
+
protected __type: {
|
40
|
+
message: string;
|
41
|
+
check: (value: TType['input']) => boolean;
|
42
|
+
} = {
|
43
|
+
message: 'Invalid type',
|
44
|
+
check: (value) => Array.from(this.__schemas).some((schema) => schema['__type'].check(value))
|
45
|
+
};
|
46
|
+
|
37
47
|
protected __schemas = new Set<Schema<any>>();
|
38
48
|
|
39
49
|
constructor(schemas: TSchemas) {
|
@@ -41,10 +51,10 @@ export default class UnionSchema<
|
|
41
51
|
this.__schemas = new Set(schemas);
|
42
52
|
}
|
43
53
|
|
44
|
-
protected async
|
54
|
+
protected async __transformToAdapter(
|
45
55
|
options: Parameters<Schema['__transformToAdapter']>[0]
|
46
56
|
): Promise<ReturnType<FieldAdapter['translate']>> {
|
47
|
-
return
|
57
|
+
return defaultTransformToAdapter(
|
48
58
|
async (adapter) => {
|
49
59
|
const promises: Promise<any>[] = [];
|
50
60
|
const shouldBeHighPriorityFallback = adapter.union === undefined;
|
@@ -73,12 +83,12 @@ export default class UnionSchema<
|
|
73
83
|
Array.from(this.__schemas) as unknown as readonly [
|
74
84
|
Schema<any, any>,
|
75
85
|
Schema<any, any>,
|
76
|
-
...Schema<any, any>[]
|
86
|
+
...Schema<any, any>[]
|
77
87
|
]
|
78
88
|
),
|
79
89
|
{
|
80
90
|
at: 0,
|
81
|
-
removeCurrent: true
|
91
|
+
removeCurrent: true
|
82
92
|
}
|
83
93
|
);
|
84
94
|
}
|
@@ -91,12 +101,13 @@ export default class UnionSchema<
|
|
91
101
|
adapter.union,
|
92
102
|
(isStringVersion) => ({
|
93
103
|
nullable: this.__nullable,
|
104
|
+
type: this.__type,
|
94
105
|
optional: this.__optional,
|
95
106
|
schemas: isStringVersion ? transformedSchemasAsString : transformedSchemas,
|
96
107
|
parsers: {
|
97
108
|
nullable: this.__nullable.allow,
|
98
|
-
optional: this.__optional.allow
|
99
|
-
}
|
109
|
+
optional: this.__optional.allow
|
110
|
+
}
|
100
111
|
}),
|
101
112
|
{},
|
102
113
|
{
|
@@ -104,13 +115,14 @@ export default class UnionSchema<
|
|
104
115
|
fallbackIfNotSupported: async () => {
|
105
116
|
if (options.appendFallbacksBeforeAdapterValidation)
|
106
117
|
options.appendFallbacksBeforeAdapterValidation(
|
118
|
+
this,
|
107
119
|
'union',
|
108
120
|
async (adapter, fieldAdapter, schema, translatedSchemas, value, path, options) => {
|
109
121
|
const parsedValues = {
|
110
122
|
parsed: value,
|
111
|
-
errors: []
|
123
|
+
errors: []
|
112
124
|
} as { parsed: any; errors: any[] };
|
113
|
-
//
|
125
|
+
// const initialErrorsAsHashedSet = new Set(Array.from(options.errorsAsHashedSet || []));
|
114
126
|
for (const translatedSchema of translatedSchemas) {
|
115
127
|
//options.errorsAsHashedSet = initialErrorsAsHashedSet;
|
116
128
|
const { parsed, errors } = await schema.__validateByAdapter(
|
@@ -121,7 +133,6 @@ export default class UnionSchema<
|
|
121
133
|
path,
|
122
134
|
options
|
123
135
|
);
|
124
|
-
|
125
136
|
// eslint-disable-next-line ts/no-unnecessary-condition
|
126
137
|
if ((errors || []).length <= 0) return { parsed, errors };
|
127
138
|
else {
|
@@ -138,12 +149,12 @@ export default class UnionSchema<
|
|
138
149
|
for (const schema of this.__schemas)
|
139
150
|
transformedSchemasAsPromises.push((schema as any).__transformToAdapter(options));
|
140
151
|
|
141
|
-
console.log((await Promise.all(transformedSchemasAsPromises)).flat());
|
142
152
|
return (await Promise.all(transformedSchemasAsPromises)).flat();
|
143
|
-
}
|
153
|
+
}
|
144
154
|
}
|
145
155
|
);
|
146
156
|
},
|
157
|
+
this,
|
147
158
|
this.__transformedSchemas,
|
148
159
|
options,
|
149
160
|
'union'
|
@@ -151,7 +162,8 @@ export default class UnionSchema<
|
|
151
162
|
}
|
152
163
|
|
153
164
|
/**
|
154
|
-
* This let's you refine the schema with custom validations. This is useful when you want to validate something that
|
165
|
+
* This let's you refine the schema with custom validations. This is useful when you want to validate something that
|
166
|
+
* is not supported by default by the schema adapter.
|
155
167
|
*
|
156
168
|
* @example
|
157
169
|
* ```typescript
|
@@ -163,15 +175,22 @@ export default class UnionSchema<
|
|
163
175
|
*
|
164
176
|
* const { errors, parsed } = await numberSchema.parse(-1);
|
165
177
|
*
|
166
|
-
*
|
178
|
+
* // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
|
179
|
+
* console.log(errors);
|
167
180
|
* ```
|
168
181
|
*
|
169
182
|
* @param refinementCallback - The callback that will be called to validate the value.
|
170
183
|
* @param options - Options for the refinement.
|
171
184
|
* @param options.isAsync - Whether the callback is async or not. Defaults to true.
|
172
185
|
*/
|
173
|
-
|
174
|
-
refinementCallback: (
|
186
|
+
refine(
|
187
|
+
refinementCallback: (
|
188
|
+
value: TType['input']
|
189
|
+
) =>
|
190
|
+
| Promise<void | undefined | { code: string; message: string }>
|
191
|
+
| void
|
192
|
+
| undefined
|
193
|
+
| { code: string; message: string }
|
175
194
|
) {
|
176
195
|
return super.refine(refinementCallback) as unknown as UnionSchema<
|
177
196
|
{
|
@@ -225,8 +244,36 @@ export default class UnionSchema<
|
|
225
244
|
}
|
226
245
|
|
227
246
|
/**
|
228
|
-
*
|
229
|
-
*
|
247
|
+
* Just adds a message when the value is undefined. It's just a syntax sugar for
|
248
|
+
*
|
249
|
+
* ```typescript
|
250
|
+
* p.string().optional({ message: 'This value cannot be null', allow: false })
|
251
|
+
* ```
|
252
|
+
*
|
253
|
+
* @param options - The options of nonOptional function
|
254
|
+
* @param options.message - A custom message if the value is undefined.
|
255
|
+
*
|
256
|
+
* @returns - The schema.
|
257
|
+
*/
|
258
|
+
nonOptional(options?: { message: string }) {
|
259
|
+
return super.optional({
|
260
|
+
message: options?.message,
|
261
|
+
allow: false
|
262
|
+
}) as unknown as UnionSchema<
|
263
|
+
{
|
264
|
+
input: TType['input'];
|
265
|
+
validate: TType['validate'];
|
266
|
+
internal: TType['internal'];
|
267
|
+
output: TType['output'];
|
268
|
+
representation: TType['representation'];
|
269
|
+
},
|
270
|
+
TDefinitions
|
271
|
+
>;
|
272
|
+
}
|
273
|
+
|
274
|
+
/**
|
275
|
+
* Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value
|
276
|
+
* is NULL by setting the { message: 'Your custom message', allow: false } on the options.
|
230
277
|
*
|
231
278
|
* @example
|
232
279
|
* ```typescript
|
@@ -264,14 +311,43 @@ export default class UnionSchema<
|
|
264
311
|
}
|
265
312
|
|
266
313
|
/**
|
267
|
-
*
|
314
|
+
* Just adds a message when the value is null. It's just a syntax sugar for
|
315
|
+
*
|
316
|
+
* ```typescript
|
317
|
+
* p.string().nullable({ message: 'This value cannot be null', allow: false })
|
318
|
+
* ```
|
319
|
+
*
|
320
|
+
* @param options - The options of nonNullable function
|
321
|
+
* @param options.message - A custom message if the value is null.
|
322
|
+
*
|
323
|
+
* @returns - The schema.
|
324
|
+
*/
|
325
|
+
nonNullable(options?: { message: string }) {
|
326
|
+
return super.nullable({
|
327
|
+
message: options?.message || '',
|
328
|
+
allow: false
|
329
|
+
}) as unknown as UnionSchema<
|
330
|
+
{
|
331
|
+
input: TType['input'];
|
332
|
+
validate: TType['validate'];
|
333
|
+
internal: TType['internal'];
|
334
|
+
output: TType['output'];
|
335
|
+
representation: TType['representation'];
|
336
|
+
},
|
337
|
+
TDefinitions
|
338
|
+
>;
|
339
|
+
}
|
340
|
+
/**
|
341
|
+
* This method will remove the value from the representation of the schema. If the value is undefined it will keep
|
342
|
+
* that way
|
268
343
|
* otherwise it will set the value to undefined after it's validated.
|
269
344
|
* This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
|
270
345
|
* function. This will remove the value from the representation of the schema.
|
271
346
|
*
|
272
|
-
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
273
|
-
* But if you want to remove the value from the internal representation, you can pass the argument
|
274
|
-
* Then if you still want to remove the value from the representation, you will need to pass
|
347
|
+
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
348
|
+
* function. But if you want to remove the value from the internal representation, you can pass the argument
|
349
|
+
* `toInternal` as true. Then if you still want to remove the value from the representation, you will need to pass
|
350
|
+
* the argument `toRepresentation` as true as well.
|
275
351
|
*
|
276
352
|
* @example
|
277
353
|
* ```typescript
|
@@ -293,16 +369,17 @@ export default class UnionSchema<
|
|
293
369
|
* ```
|
294
370
|
*
|
295
371
|
*
|
296
|
-
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
297
|
-
* But if you want to remove the value from the internal representation, you can pass
|
298
|
-
* Then if you still want to remove the value from the representation, you
|
372
|
+
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
373
|
+
* the {@link data} function. But if you want to remove the value from the internal representation, you can pass
|
374
|
+
* the argument `toInternal` as true. Then if you still want to remove the value from the representation, you
|
375
|
+
* will need to pass the argument `toRepresentation` as true as well.
|
299
376
|
*
|
300
377
|
* @returns The schema.
|
301
378
|
*/
|
302
379
|
omit<
|
303
380
|
TToInternal extends boolean,
|
304
381
|
TToRepresentation extends boolean = boolean extends TToInternal ? true : false
|
305
|
-
>(args?: { toInternal?: TToInternal
|
382
|
+
>(args?: { toInternal?: TToInternal; toRepresentation?: TToRepresentation }) {
|
306
383
|
return super.omit(args) as unknown as UnionSchema<
|
307
384
|
{
|
308
385
|
input: TToInternal extends true ? TType['input'] | undefined : TType['input'];
|
@@ -317,9 +394,9 @@ export default class UnionSchema<
|
|
317
394
|
}
|
318
395
|
|
319
396
|
/**
|
320
|
-
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
|
321
|
-
* like a database. You should always return the schema after you save the value, that way we will always
|
322
|
-
* of the schema after the save operation.
|
397
|
+
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
|
398
|
+
* source like a database. You should always return the schema after you save the value, that way we will always
|
399
|
+
* have the correct type of the schema after the save operation.
|
323
400
|
*
|
324
401
|
* You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
|
325
402
|
*
|
@@ -374,9 +451,9 @@ export default class UnionSchema<
|
|
374
451
|
>;
|
375
452
|
}
|
376
453
|
|
377
|
-
|
378
454
|
/**
|
379
|
-
* This function is used to add a default value to the schema. If the value is either undefined or null, the default
|
455
|
+
* This function is used to add a default value to the schema. If the value is either undefined or null, the default
|
456
|
+
* value will be used.
|
380
457
|
*
|
381
458
|
* @example
|
382
459
|
* ```typescript
|
@@ -406,8 +483,49 @@ export default class UnionSchema<
|
|
406
483
|
}
|
407
484
|
|
408
485
|
/**
|
409
|
-
* This function
|
410
|
-
*
|
486
|
+
* This function is used to transform the value to the representation without validating it.
|
487
|
+
* This is useful when you want to return a data from a query directly to the user. But for example
|
488
|
+
* you are returning the data of a user, you can clean the password or any other sensitive data.
|
489
|
+
*
|
490
|
+
* @example
|
491
|
+
* ```typescript
|
492
|
+
* import * as p from '@palmares/schemas';
|
493
|
+
*
|
494
|
+
* const userSchema = p.object({
|
495
|
+
* id: p.number().optional(),
|
496
|
+
* name: p.string(),
|
497
|
+
* email: p.string().email(),
|
498
|
+
* password: p.string().optional()
|
499
|
+
* }).toRepresentation(async (value) => {
|
500
|
+
* return {
|
501
|
+
* id: value.id,
|
502
|
+
* name: value.name,
|
503
|
+
* email: value.email
|
504
|
+
* }
|
505
|
+
* });
|
506
|
+
*
|
507
|
+
* const user = await userSchema.data({
|
508
|
+
* id: 1,
|
509
|
+
* name: 'John Doe',
|
510
|
+
* email: 'john@gmail.com',
|
511
|
+
* password: '123456'
|
512
|
+
* });
|
513
|
+
* ```
|
514
|
+
*/
|
515
|
+
async data(value: TType['output']): Promise<TType['representation']> {
|
516
|
+
const parsedValue = await super.data(value);
|
517
|
+
for (const schema of Array.from(this.__schemas)) {
|
518
|
+
if (schema['__optional'].allow && value === undefined) return schema.data(parsedValue);
|
519
|
+
if (schema['__nullable'].allow && value === null) return schema.data(parsedValue);
|
520
|
+
if (schema['__type'].check(parsedValue)) return schema.data(parsedValue);
|
521
|
+
}
|
522
|
+
return parsedValue;
|
523
|
+
}
|
524
|
+
|
525
|
+
/**
|
526
|
+
* This function let's you customize the schema your own way. After we translate the schema on the adapter we call
|
527
|
+
* this function to let you customize the custom schema your own way. Our API does not support passthrough?
|
528
|
+
* No problem, you can use this function to customize the zod schema.
|
411
529
|
*
|
412
530
|
* @example
|
413
531
|
* ```typescript
|
@@ -419,12 +537,13 @@ export default class UnionSchema<
|
|
419
537
|
*
|
420
538
|
* const { errors, parsed } = await numberSchema.parse(-1);
|
421
539
|
*
|
422
|
-
*
|
540
|
+
* // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
|
541
|
+
* console.log(errors);
|
423
542
|
* ```
|
424
543
|
*
|
425
544
|
* @param callback - The callback that will be called to customize the schema.
|
426
|
-
* @param toStringCallback - The callback that will be called to transform the schema to a string when you want
|
427
|
-
* to a string so you can save it for future runs.
|
545
|
+
* @param toStringCallback - The callback that will be called to transform the schema to a string when you want
|
546
|
+
* to compile the underlying schema to a string so you can save it for future runs.
|
428
547
|
*
|
429
548
|
* @returns The schema.
|
430
549
|
*/
|
@@ -438,9 +557,9 @@ export default class UnionSchema<
|
|
438
557
|
}
|
439
558
|
|
440
559
|
/**
|
441
|
-
* This function is used to transform the value to the representation of the schema. When using the {@link data}
|
442
|
-
* control to add data cleaning for example, transforming the data and
|
443
|
-
* The schema maps to itself.
|
560
|
+
* This function is used to transform the value to the representation of the schema. When using the {@link data}
|
561
|
+
* function. With this function you have full control to add data cleaning for example, transforming the data and
|
562
|
+
* whatever. Another use case is when you want to return deeply nested recursive data. The schema maps to itself.
|
444
563
|
*
|
445
564
|
* @example
|
446
565
|
* ```typescript
|
@@ -497,8 +616,9 @@ export default class UnionSchema<
|
|
497
616
|
}
|
498
617
|
|
499
618
|
/**
|
500
|
-
* This function is used to transform the value to the internal representation of the schema. This is useful when you
|
501
|
-
* to a type that the schema adapter can understand. For example, you might want to
|
619
|
+
* This function is used to transform the value to the internal representation of the schema. This is useful when you
|
620
|
+
* want to transform the value to a type that the schema adapter can understand. For example, you might want to
|
621
|
+
* transform a string to a date. This is the function you use.
|
502
622
|
*
|
503
623
|
* @example
|
504
624
|
* ```typescript
|
@@ -543,8 +663,9 @@ export default class UnionSchema<
|
|
543
663
|
}
|
544
664
|
|
545
665
|
/**
|
546
|
-
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
|
547
|
-
* here BEFORE the validation. This pretty much transforms the value to a type
|
666
|
+
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
|
667
|
+
* you can convert that string to a date here BEFORE the validation. This pretty much transforms the value to a type
|
668
|
+
* that the schema adapter can understand.
|
548
669
|
*
|
549
670
|
* @example
|
550
671
|
* ```
|
@@ -578,19 +699,23 @@ export default class UnionSchema<
|
|
578
699
|
|
579
700
|
static new<
|
580
701
|
TSchemas extends readonly [Schema<any, any>, Schema<any, any>, ...Schema<any, any>[]],
|
581
|
-
TDefinitions extends DefinitionsOfSchemaType = DefinitionsOfSchemaType
|
582
|
-
>(
|
583
|
-
|
584
|
-
|
585
|
-
|
702
|
+
TDefinitions extends DefinitionsOfSchemaType = DefinitionsOfSchemaType
|
703
|
+
>(
|
704
|
+
schemas: Narrow<TSchemas>
|
705
|
+
): UnionSchema<{
|
706
|
+
input: ExtractTypeFromUnionOfSchemas<TSchemas, 'input'>;
|
707
|
+
internal: ExtractTypeFromUnionOfSchemas<TSchemas, 'internal'>;
|
708
|
+
output: ExtractTypeFromUnionOfSchemas<TSchemas, 'output'>;
|
709
|
+
representation: ExtractTypeFromUnionOfSchemas<TSchemas, 'representation'>;
|
710
|
+
validate: ExtractTypeFromUnionOfSchemas<TSchemas, 'validate'>;
|
711
|
+
}> {
|
712
|
+
const returnValue = new UnionSchema<
|
713
|
+
TSchemas[number] extends Schema<infer TType, any> ? TType : never,
|
714
|
+
TDefinitions,
|
715
|
+
TSchemas
|
716
|
+
>(schemas as TSchemas);
|
586
717
|
|
587
|
-
returnValue.__transformedSchemas[adapterInstance.constructor.name] = {
|
588
|
-
transformed: false,
|
589
|
-
adapter: adapterInstance,
|
590
|
-
schemas: [],
|
591
|
-
};
|
592
718
|
return returnValue as any;
|
593
719
|
}
|
594
720
|
}
|
595
|
-
|
596
721
|
export const union = UnionSchema.new;
|
package/src/types.ts
CHANGED
@@ -1,8 +1,13 @@
|
|
1
|
+
import type SchemaAdapter from './adapter';
|
1
2
|
import type Schema from './schema/schema';
|
2
3
|
import type { ValidatorTypes } from './validators/types';
|
3
4
|
|
4
5
|
export type MaybePromise<T> = T | Promise<T>;
|
5
6
|
|
7
|
+
export type SchemasSettingsType = {
|
8
|
+
schemaAdapter: typeof SchemaAdapter;
|
9
|
+
};
|
10
|
+
|
6
11
|
export type FallbackFunctionsType<TArguments> = {
|
7
12
|
[TKey in keyof TArguments]?: (args: NonNullable<TArguments[TKey]>) => {
|
8
13
|
type: ValidatorTypes;
|
@@ -11,3 +16,51 @@ export type FallbackFunctionsType<TArguments> = {
|
|
11
16
|
};
|
12
17
|
|
13
18
|
export type SupportedSchemas = 'number' | 'object' | 'union' | 'string' | 'array' | 'boolean' | 'datetime';
|
19
|
+
|
20
|
+
/**
|
21
|
+
* Retrieve the typescript type of a schema.
|
22
|
+
*
|
23
|
+
* First generic is `typeof myCustomSchema`.
|
24
|
+
*
|
25
|
+
* Second generic is:
|
26
|
+
* - 'input' - The data passed to `.parse` and `.validate` functions. Defaults to this.
|
27
|
+
* - 'output' - (use `'representation'` to get the data format you are
|
28
|
+
* passing to the user) The data passed to `.data` function to return to the
|
29
|
+
* user.
|
30
|
+
* - 'representation' - The data after `toRepresentation`, usually, use this over 'output'
|
31
|
+
* - 'internal' - The data after it's transformed for the `toSave` callback.
|
32
|
+
* - 'validate' - The data for `toValidate` callback.
|
33
|
+
*/
|
34
|
+
export type Infer<
|
35
|
+
TSchema extends Schema<any, any>,
|
36
|
+
/**
|
37
|
+
* - 'input' - The data passed to `.parse` and `.validate` functions.
|
38
|
+
* - 'output' - (use `'representation'` to get the data format you are
|
39
|
+
* passing to the user) The data passed to `.data` function to return to the
|
40
|
+
* user.
|
41
|
+
* - 'representation' - The data after `toRepresentation`, usually, use this over 'output'
|
42
|
+
* - 'internal' - The data after it's transformed for the `toSave` callback.
|
43
|
+
* - 'validate' - The data for `toValidate` callback.
|
44
|
+
*/
|
45
|
+
TType extends 'input' | 'output' | 'representation' | 'internal' | 'validate' = 'input'
|
46
|
+
> =
|
47
|
+
TSchema extends Schema<
|
48
|
+
{
|
49
|
+
input: infer TInput;
|
50
|
+
internal: infer TInternal;
|
51
|
+
output: infer TOutput;
|
52
|
+
representation: infer TRepresentation;
|
53
|
+
validate: infer TValidate;
|
54
|
+
},
|
55
|
+
any
|
56
|
+
>
|
57
|
+
? TType extends 'output'
|
58
|
+
? TOutput
|
59
|
+
: TType extends 'representation'
|
60
|
+
? TRepresentation
|
61
|
+
: TType extends 'internal'
|
62
|
+
? TInternal
|
63
|
+
: TType extends 'validate'
|
64
|
+
? TValidate
|
65
|
+
: TInput
|
66
|
+
: never;
|