@palmares/schemas 0.1.20 → 0.1.22
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 +19 -0
- package/dist/cjs/src/adapter/index.js +2 -0
- package/dist/cjs/src/conf.js +3 -4
- package/dist/cjs/src/model.js +34 -38
- package/dist/cjs/src/schema/array.js +1 -0
- package/dist/cjs/src/schema/object.js +2 -8
- package/dist/cjs/src/schema/schema.js +1 -0
- package/dist/cjs/src/utils.js +12 -14
- package/dist/cjs/src/validators/utils.js +1 -0
- package/dist/cjs/tsconfig.types.tsbuildinfo +1 -1
- package/dist/cjs/types/adapter/index.d.ts +2 -0
- package/dist/cjs/types/adapter/index.d.ts.map +1 -1
- package/dist/cjs/types/conf.d.ts +3 -0
- package/dist/cjs/types/conf.d.ts.map +1 -1
- package/dist/cjs/types/domain.d.ts +3 -0
- package/dist/cjs/types/domain.d.ts.map +1 -1
- package/dist/cjs/types/model.d.ts +2 -2
- package/dist/cjs/types/model.d.ts.map +1 -1
- package/dist/cjs/types/schema/array.d.ts +1 -0
- package/dist/cjs/types/schema/array.d.ts.map +1 -1
- package/dist/cjs/types/schema/object.d.ts.map +1 -1
- package/dist/cjs/types/schema/schema.d.ts +1 -0
- package/dist/cjs/types/schema/schema.d.ts.map +1 -1
- package/dist/cjs/types/utils.d.ts +2 -1
- package/dist/cjs/types/utils.d.ts.map +1 -1
- package/dist/cjs/types/validators/utils.d.ts +1 -0
- package/dist/cjs/types/validators/utils.d.ts.map +1 -1
- package/dist/esm/src/adapter/index.js +2 -0
- package/dist/esm/src/conf.js +3 -4
- package/dist/esm/src/model.js +32 -30
- package/dist/esm/src/schema/array.js +1 -0
- package/dist/esm/src/schema/object.js +2 -1
- package/dist/esm/src/schema/schema.js +1 -0
- package/dist/esm/src/utils.js +9 -6
- package/dist/esm/src/validators/utils.js +1 -0
- package/package.json +11 -5
- package/.turbo/turbo-build$colon$watch.log +0 -24
- package/.turbo/turbo-build.log +0 -4
- package/.turbo/turbo-build:watch.log +0 -26
- package/__tests__/.drizzle/migrations/0000_skinny_harrier.sql +0 -22
- package/__tests__/.drizzle/migrations/meta/0000_snapshot.json +0 -156
- package/__tests__/.drizzle/migrations/meta/_journal.json +0 -13
- package/__tests__/.drizzle/schema.ts +0 -35
- package/__tests__/drizzle.config.ts +0 -11
- package/__tests__/eslint.config.js +0 -10
- package/__tests__/manage.ts +0 -5
- package/__tests__/node_modules/.bin/drizzle-kit +0 -17
- package/__tests__/node_modules/.bin/tsc +0 -17
- package/__tests__/node_modules/.bin/tsserver +0 -17
- package/__tests__/node_modules/.bin/tsx +0 -17
- package/__tests__/package.json +0 -34
- package/__tests__/sqlite.db +0 -0
- package/__tests__/src/core/array.test.ts +0 -131
- package/__tests__/src/core/boolean.test.ts +0 -66
- package/__tests__/src/core/datetime.test.ts +0 -102
- package/__tests__/src/core/index.ts +0 -35
- package/__tests__/src/core/model.test.ts +0 -260
- package/__tests__/src/core/models.ts +0 -50
- package/__tests__/src/core/numbers.test.ts +0 -177
- package/__tests__/src/core/object.test.ts +0 -218
- package/__tests__/src/core/string.test.ts +0 -222
- package/__tests__/src/core/test.test.ts +0 -59
- package/__tests__/src/core/types.test.ts +0 -97
- package/__tests__/src/core/union.test.ts +0 -99
- package/__tests__/src/settings.ts +0 -69
- package/__tests__/tsconfig.json +0 -11
- package/src/adapter/fields/array.ts +0 -31
- package/src/adapter/fields/boolean.ts +0 -43
- package/src/adapter/fields/datetime.ts +0 -43
- package/src/adapter/fields/index.ts +0 -72
- package/src/adapter/fields/number.ts +0 -43
- package/src/adapter/fields/object.ts +0 -52
- package/src/adapter/fields/string.ts +0 -43
- package/src/adapter/fields/union.ts +0 -43
- package/src/adapter/index.ts +0 -35
- package/src/adapter/types.ts +0 -276
- package/src/compile.ts +0 -14
- package/src/conf.ts +0 -27
- package/src/constants.ts +0 -7
- package/src/domain.ts +0 -15
- package/src/exceptions.ts +0 -17
- package/src/index.ts +0 -318
- package/src/middleware.ts +0 -52
- package/src/model.ts +0 -507
- package/src/parsers/convert-from-number.ts +0 -13
- package/src/parsers/convert-from-string.ts +0 -19
- package/src/parsers/index.ts +0 -2
- package/src/schema/array.ts +0 -824
- package/src/schema/boolean.ts +0 -792
- package/src/schema/datetime.ts +0 -704
- package/src/schema/index.ts +0 -5
- package/src/schema/number.ts +0 -929
- package/src/schema/object.ts +0 -798
- package/src/schema/schema.ts +0 -1178
- package/src/schema/string.ts +0 -941
- package/src/schema/types.ts +0 -154
- package/src/schema/union.ts +0 -724
- package/src/types.ts +0 -66
- package/src/utils.ts +0 -383
- package/src/validators/array.ts +0 -183
- package/src/validators/boolean.ts +0 -52
- package/src/validators/datetime.ts +0 -121
- package/src/validators/number.ts +0 -178
- package/src/validators/object.ts +0 -56
- package/src/validators/schema.ts +0 -142
- package/src/validators/string.ts +0 -278
- package/src/validators/types.ts +0 -1
- package/src/validators/union.ts +0 -52
- package/src/validators/utils.ts +0 -225
- package/tsconfig.json +0 -9
- package/tsconfig.types.json +0 -10
@@ -1,222 +0,0 @@
|
|
1
|
-
import * as p from '@palmares/schemas';
|
2
|
-
import { describe } from '@palmares/tests';
|
3
|
-
|
4
|
-
import type JestTestAdapter from '@palmares/jest-tests';
|
5
|
-
|
6
|
-
describe<JestTestAdapter>('String Schema', ({ test }) => {
|
7
|
-
test('optional', async ({ expect }) => {
|
8
|
-
const stringSchema = p.string();
|
9
|
-
const stringSchemaWithCustomMessage = p.string().nonOptional({ message: 'hello' });
|
10
|
-
|
11
|
-
const [{ errors: errorsOnFail }, { errors: errorsOnFailWithCustomMessage }, { errors: errorsOnValid, parsed }] =
|
12
|
-
await Promise.all([
|
13
|
-
stringSchema.parse(undefined as any),
|
14
|
-
stringSchemaWithCustomMessage.parse(undefined as any),
|
15
|
-
stringSchema.parse('a')
|
16
|
-
]);
|
17
|
-
|
18
|
-
expect(errorsOnFailWithCustomMessage?.[0]?.message).toBe('hello');
|
19
|
-
expect(errorsOnFail?.[0]?.code).toBe('required');
|
20
|
-
expect(errorsOnFail?.[0]?.message).toBe('Required');
|
21
|
-
expect((errorsOnValid || []).length).toBe(0);
|
22
|
-
expect(parsed).toBe('a');
|
23
|
-
});
|
24
|
-
|
25
|
-
test('nullable', async ({ expect }) => {
|
26
|
-
const stringSchema = p.string();
|
27
|
-
const stringSchemaWithCustomMessage = p.string().nonNullable({ message: 'hello' });
|
28
|
-
|
29
|
-
const [{ errors: errorsOnFail }, { errors: errorsOnFailWithCustomMessage }, { errors: errorsOnValid, parsed }] =
|
30
|
-
await Promise.all([
|
31
|
-
stringSchema.parse(null as any),
|
32
|
-
stringSchemaWithCustomMessage.parse(null as any),
|
33
|
-
stringSchema.parse('a')
|
34
|
-
]);
|
35
|
-
|
36
|
-
expect(errorsOnFailWithCustomMessage?.[0]?.message).toBe('hello');
|
37
|
-
expect(errorsOnFail?.[0]?.code).toBe('null');
|
38
|
-
expect(errorsOnFail?.[0]?.message).toBe('Cannot be null');
|
39
|
-
expect((errorsOnValid || []).length).toBe(0);
|
40
|
-
expect(parsed).toBe('a');
|
41
|
-
});
|
42
|
-
|
43
|
-
test('is', async ({ expect }) => {
|
44
|
-
const stringSchema = p.string().is(['a', 'b']);
|
45
|
-
const stringSchemaWithCustomMessage = p.string().is(['a', 'b'], { message: 'hello' });
|
46
|
-
|
47
|
-
const [{ errors: errorsOnFail }, { errors: errorsOnFailWithCustomMessage }, { errors: errorsOnValid, parsed }] =
|
48
|
-
await Promise.all([
|
49
|
-
stringSchema.parse('c' as any),
|
50
|
-
stringSchemaWithCustomMessage.parse('c' as any),
|
51
|
-
stringSchema.parse('a')
|
52
|
-
]);
|
53
|
-
|
54
|
-
expect(errorsOnFailWithCustomMessage?.[0]?.message).toBe('hello');
|
55
|
-
expect(errorsOnFail?.[0]?.code).toBe('is');
|
56
|
-
expect(errorsOnFail?.[0]?.message).toBe('The value should be equal to a, b');
|
57
|
-
expect((errorsOnValid || []).length).toBe(0);
|
58
|
-
expect(parsed).toBe('a');
|
59
|
-
});
|
60
|
-
|
61
|
-
test('includes', async ({ expect }) => {
|
62
|
-
const stringSchema = p.string().includes('Hello');
|
63
|
-
const stringSchemaWithCustomMessage = p.string().includes('Hello', { message: 'hello' });
|
64
|
-
|
65
|
-
const [{ errors: errorsOnFail }, { errors: errorsOnFailWithCustomMessage }, { errors: errorsOnValid, parsed }] =
|
66
|
-
await Promise.all([
|
67
|
-
stringSchema.parse('World'),
|
68
|
-
stringSchemaWithCustomMessage.parse('World'),
|
69
|
-
stringSchema.parse('Hello World')
|
70
|
-
]);
|
71
|
-
|
72
|
-
expect(errorsOnFailWithCustomMessage?.[0]?.message).toBe('hello');
|
73
|
-
expect(errorsOnFail?.[0]?.code).toBe('includes');
|
74
|
-
expect(errorsOnFail?.[0]?.message).toBe(`The string value should include the following substring 'Hello'`);
|
75
|
-
expect((errorsOnValid || []).length).toBe(0);
|
76
|
-
expect(parsed).toBe('Hello World');
|
77
|
-
});
|
78
|
-
|
79
|
-
test('maxLength', async ({ expect }) => {
|
80
|
-
const stringSchema = p.string().maxLength(8);
|
81
|
-
const stringSchemaWithCustomMessage = p.string().maxLength(8, { message: 'hello' });
|
82
|
-
|
83
|
-
const [{ errors: errorsOnFail }, { errors: errorsOnFailWithCustomMessage }, { errors: errorsOnValid, parsed }] =
|
84
|
-
await Promise.all([
|
85
|
-
stringSchema.parse('Hello World'),
|
86
|
-
stringSchemaWithCustomMessage.parse('Hello World'),
|
87
|
-
stringSchema.parse('Hello')
|
88
|
-
]);
|
89
|
-
|
90
|
-
expect(errorsOnFailWithCustomMessage?.[0]?.message).toBe('hello');
|
91
|
-
expect(errorsOnFail?.[0]?.code).toBe('maxLength');
|
92
|
-
expect(errorsOnFail?.[0]?.message).toBe(`The value should have a maximum length of 8`);
|
93
|
-
expect((errorsOnValid || []).length).toBe(0);
|
94
|
-
expect(parsed).toBe('Hello');
|
95
|
-
});
|
96
|
-
|
97
|
-
test('minLength', async ({ expect }) => {
|
98
|
-
const stringSchema = p.string().minLength(8);
|
99
|
-
const stringSchemaWithCustomMessage = p.string().minLength(8, { message: 'hello' });
|
100
|
-
|
101
|
-
const [{ errors: errorsOnFail }, { errors: errorsOnFailWithCustomMessage }, { errors: errorsOnValid, parsed }] =
|
102
|
-
await Promise.all([
|
103
|
-
stringSchema.parse(''),
|
104
|
-
stringSchemaWithCustomMessage.parse(''),
|
105
|
-
stringSchema.parse('Hello World')
|
106
|
-
]);
|
107
|
-
|
108
|
-
expect(errorsOnFailWithCustomMessage?.[0]?.message).toBe('hello');
|
109
|
-
expect(errorsOnFail?.[0]?.code).toBe('minLength');
|
110
|
-
expect(errorsOnFail?.[0]?.message).toBe(`The value should have a minimum length of 8`);
|
111
|
-
expect((errorsOnValid || []).length).toBe(0);
|
112
|
-
expect(parsed).toBe('Hello World');
|
113
|
-
});
|
114
|
-
|
115
|
-
test('startsWith', async ({ expect }) => {
|
116
|
-
const stringSchema = p.string().startsWith('Hello');
|
117
|
-
const stringSchemaWithCustomMessage = p.string().startsWith('Hello', { message: 'hello' });
|
118
|
-
|
119
|
-
const [{ errors: errorsOnFail }, { errors: errorsOnFailWithCustomMessage }, { errors: errorsOnValid, parsed }] =
|
120
|
-
await Promise.all([
|
121
|
-
stringSchema.parse('Palmares, the best FW in the world!!!!!'),
|
122
|
-
stringSchemaWithCustomMessage.parse(`I actually think it's bad`),
|
123
|
-
stringSchema.parse('Hello, World')
|
124
|
-
]);
|
125
|
-
|
126
|
-
expect(errorsOnFailWithCustomMessage?.[0]?.message).toBe('hello');
|
127
|
-
expect(errorsOnFail?.[0]?.code).toBe('startsWith');
|
128
|
-
expect(errorsOnFail?.[0]?.message).toBe(`The value should start with Hello`);
|
129
|
-
expect((errorsOnValid || []).length).toBe(0);
|
130
|
-
expect(parsed).toBe('Hello, World');
|
131
|
-
});
|
132
|
-
|
133
|
-
test('endsWith', async ({ expect }) => {
|
134
|
-
const stringSchema = p.string().endsWith('World');
|
135
|
-
const stringSchemaWithCustomMessage = p.string().endsWith('World', { message: 'hello' });
|
136
|
-
|
137
|
-
const [{ errors: errorsOnFail }, { errors: errorsOnFailWithCustomMessage }, { errors: errorsOnValid, parsed }] =
|
138
|
-
await Promise.all([
|
139
|
-
stringSchema.parse('Palmares, the best FW in the world!!!!!'),
|
140
|
-
stringSchemaWithCustomMessage.parse(`I actually think it's bad`),
|
141
|
-
stringSchema.parse('Hello, World')
|
142
|
-
]);
|
143
|
-
|
144
|
-
expect(errorsOnFailWithCustomMessage?.[0]?.message).toBe('hello');
|
145
|
-
expect(errorsOnFail?.[0]?.code).toBe('endsWith');
|
146
|
-
expect(errorsOnFail?.[0]?.message).toBe(`The value should end with World`);
|
147
|
-
expect((errorsOnValid || []).length).toBe(0);
|
148
|
-
expect(parsed).toBe('Hello, World');
|
149
|
-
});
|
150
|
-
|
151
|
-
test('endsWith', async ({ expect }) => {
|
152
|
-
const stringSchema = p.string().endsWith('World');
|
153
|
-
const stringSchemaWithCustomMessage = p.string().endsWith('World', { message: 'hello' });
|
154
|
-
|
155
|
-
const [{ errors: errorsOnFail }, { errors: errorsOnFailWithCustomMessage }, { errors: errorsOnValid, parsed }] =
|
156
|
-
await Promise.all([
|
157
|
-
stringSchema.parse('Palmares, the best FW in the world!!!!!'),
|
158
|
-
stringSchemaWithCustomMessage.parse(`I actually think it's bad`),
|
159
|
-
stringSchema.parse('Hello, World')
|
160
|
-
]);
|
161
|
-
|
162
|
-
expect(errorsOnFailWithCustomMessage?.[0]?.message).toBe('hello');
|
163
|
-
expect(errorsOnFail?.[0]?.code).toBe('endsWith');
|
164
|
-
expect(errorsOnFail?.[0]?.message).toBe(`The value should end with World`);
|
165
|
-
expect((errorsOnValid || []).length).toBe(0);
|
166
|
-
expect(parsed).toBe('Hello, World');
|
167
|
-
});
|
168
|
-
|
169
|
-
test('regex', async ({ expect }) => {
|
170
|
-
const regex = new RegExp('Hello', 'g');
|
171
|
-
const stringSchema = p.string().regex(regex);
|
172
|
-
const stringSchemaWithCustomMessage = p.string().regex(regex, { message: 'hello' });
|
173
|
-
|
174
|
-
const [{ errors: errorsOnFail }, { errors: errorsOnFailWithCustomMessage }, { errors: errorsOnValid, parsed }] =
|
175
|
-
await Promise.all([
|
176
|
-
stringSchema.parse('Palmares, the best FW in the world!!!!!'),
|
177
|
-
stringSchemaWithCustomMessage.parse(`I actually think it's bad`),
|
178
|
-
stringSchema.parse('Hello, World')
|
179
|
-
]);
|
180
|
-
|
181
|
-
expect(errorsOnFailWithCustomMessage?.[0]?.message).toBe('hello');
|
182
|
-
expect(errorsOnFail?.[0]?.code).toBe('regex');
|
183
|
-
expect(errorsOnFail?.[0]?.message).toBe(`The value should match the following regex '/Hello/g'`);
|
184
|
-
expect((errorsOnValid || []).length).toBe(0);
|
185
|
-
expect(parsed).toBe('Hello, World');
|
186
|
-
});
|
187
|
-
|
188
|
-
test('uuid', async ({ expect }) => {
|
189
|
-
const stringSchema = p.string().uuid();
|
190
|
-
const stringSchemaWithCustomMessage = p.string().uuid({ message: 'hello' });
|
191
|
-
|
192
|
-
const [{ errors: errorsOnFail }, { errors: errorsOnFailWithCustomMessage }, { errors: errorsOnValid, parsed }] =
|
193
|
-
await Promise.all([
|
194
|
-
stringSchema.parse('Palmares, the best FW in the world!!!!!'),
|
195
|
-
stringSchemaWithCustomMessage.parse(`I actually think it's bad`),
|
196
|
-
stringSchema.parse('f54bb64b-0aed-47f9-976e-a8900d86f6c9')
|
197
|
-
]);
|
198
|
-
|
199
|
-
expect(errorsOnFailWithCustomMessage?.[0]?.message).toBe('hello');
|
200
|
-
expect(errorsOnFail?.[0]?.code).toBe('uuid');
|
201
|
-
expect(errorsOnFail?.[0]?.message).toBe(`The value should be a valid UUID`);
|
202
|
-
expect((errorsOnValid || []).length).toBe(0);
|
203
|
-
expect(parsed).toBe('f54bb64b-0aed-47f9-976e-a8900d86f6c9');
|
204
|
-
});
|
205
|
-
|
206
|
-
test('email', async ({ expect }) => {
|
207
|
-
const stringSchema = p.string().email();
|
208
|
-
const stringSchemaWithCustomMessage = p.string().email({ message: 'hello' });
|
209
|
-
|
210
|
-
const [{ errors: errorsOnFail }, { errors: errorsOnFailWithCustomMessage }, { errors: errorsOnValid, parsed }] =
|
211
|
-
await Promise.all([
|
212
|
-
stringSchema.parse('Palmares, the best FW in the world!!!!!'),
|
213
|
-
stringSchemaWithCustomMessage.parse(`I actually think it's bad`),
|
214
|
-
stringSchema.parse('test@test.com')
|
215
|
-
]);
|
216
|
-
expect(errorsOnFailWithCustomMessage?.[0]?.message).toBe('hello');
|
217
|
-
expect(errorsOnFail?.[0]?.code).toBe('email');
|
218
|
-
expect(errorsOnFail?.[0]?.message).toBe(`The value should be a valid email`);
|
219
|
-
expect((errorsOnValid || []).length).toBe(0);
|
220
|
-
expect(parsed).toBe('test@test.com');
|
221
|
-
});
|
222
|
-
});
|
@@ -1,59 +0,0 @@
|
|
1
|
-
import * as p from '@palmares/schemas';
|
2
|
-
import { describe } from '@palmares/tests';
|
3
|
-
|
4
|
-
import type JestTestAdapter from '@palmares/jest-tests';
|
5
|
-
|
6
|
-
describe<JestTestAdapter>('Basic Schemas', ({ test }) => {
|
7
|
-
test('basic representation', async ({ expect }) => {
|
8
|
-
const basicSchema = p.number();
|
9
|
-
|
10
|
-
const data = await basicSchema.data(2);
|
11
|
-
|
12
|
-
expect(data).toBe(2);
|
13
|
-
});
|
14
|
-
|
15
|
-
test('basic to representation', async ({ expect }) => {
|
16
|
-
const defaultMessage = 'This used to be a number';
|
17
|
-
// eslint-disable-next-line ts/require-await
|
18
|
-
const numberSchema = p.number().toRepresentation(async () => {
|
19
|
-
return defaultMessage;
|
20
|
-
});
|
21
|
-
|
22
|
-
// eslint-disable-next-line ts/require-await
|
23
|
-
const schemaTransformingData = p.number().toRepresentation(async (data) => {
|
24
|
-
return {
|
25
|
-
number: data,
|
26
|
-
message: defaultMessage
|
27
|
-
};
|
28
|
-
});
|
29
|
-
|
30
|
-
const stringMessage = await numberSchema.data(2);
|
31
|
-
const objectMessage = await schemaTransformingData.data(2);
|
32
|
-
|
33
|
-
expect(stringMessage).toBe(defaultMessage);
|
34
|
-
expect(objectMessage.message).toBe(defaultMessage);
|
35
|
-
expect(objectMessage.number).toBe(2);
|
36
|
-
});
|
37
|
-
|
38
|
-
test('basic internal', async ({ expect }) => {
|
39
|
-
// eslint-disable-next-line ts/require-await
|
40
|
-
const internalSchema = p.number().toInternal(async (data) => {
|
41
|
-
return data * 2;
|
42
|
-
});
|
43
|
-
|
44
|
-
const { parsed } = await internalSchema.parse(2);
|
45
|
-
|
46
|
-
expect(parsed).toBe(4);
|
47
|
-
});
|
48
|
-
|
49
|
-
test('basic validation', async ({ expect }) => {
|
50
|
-
// eslint-disable-next-line ts/require-await
|
51
|
-
const internalSchema = p.number().toValidate(async () => {
|
52
|
-
return 'Should fail';
|
53
|
-
});
|
54
|
-
|
55
|
-
const { errors } = await internalSchema.parse(2);
|
56
|
-
|
57
|
-
expect((errors?.length || 0) > 0).toBe(true);
|
58
|
-
});
|
59
|
-
});
|
@@ -1,97 +0,0 @@
|
|
1
|
-
import * as p from '@palmares/schemas';
|
2
|
-
import { describe } from '@palmares/tests';
|
3
|
-
|
4
|
-
import type JestTestAdapter from '@palmares/jest-tests';
|
5
|
-
|
6
|
-
describe<JestTestAdapter>('Schema Types', ({ test }) => {
|
7
|
-
test('number schema', async ({ expect }) => {
|
8
|
-
const numberSchema = p.number();
|
9
|
-
|
10
|
-
const { errors } = await numberSchema.parse('value' as any);
|
11
|
-
|
12
|
-
expect((errors?.length || 0) > 0).toBe(true);
|
13
|
-
});
|
14
|
-
|
15
|
-
test('boolean schema', async ({ expect }) => {
|
16
|
-
const booleanSchema = p.boolean();
|
17
|
-
|
18
|
-
const { errors } = await booleanSchema.parse('value' as any);
|
19
|
-
|
20
|
-
expect((errors?.length || 0) > 0).toBe(true);
|
21
|
-
});
|
22
|
-
|
23
|
-
test('date schema', async ({ expect }) => {
|
24
|
-
const dateSchema = p.datetime();
|
25
|
-
|
26
|
-
const { errors } = await dateSchema.parse('value' as any);
|
27
|
-
|
28
|
-
expect((errors?.length || 0) > 0).toBe(true);
|
29
|
-
});
|
30
|
-
|
31
|
-
test('string schema', async ({ expect }) => {
|
32
|
-
const stringSchema = p.string();
|
33
|
-
|
34
|
-
const { errors } = await stringSchema.parse(2 as any);
|
35
|
-
|
36
|
-
expect((errors?.length || 0) > 0).toBe(true);
|
37
|
-
});
|
38
|
-
|
39
|
-
test('array schema', async ({ expect }) => {
|
40
|
-
const tupleSchema = p.array(p.number(), p.string());
|
41
|
-
const arraySchema = p.array([p.number()]);
|
42
|
-
|
43
|
-
const [
|
44
|
-
{ errors: errorsOfInvalidArray },
|
45
|
-
{ errors: errorsOfInvalidTuple },
|
46
|
-
{ errors: errorsOfValidArray, parsed: parsedArray },
|
47
|
-
{ errors: errorsOfValidTuple, parsed: parsedTuple }
|
48
|
-
] = await Promise.all([
|
49
|
-
arraySchema.parse('value' as any),
|
50
|
-
tupleSchema.parse([1, 2, 3] as any),
|
51
|
-
arraySchema.parse([1, 2, 3]),
|
52
|
-
tupleSchema.parse([1, 'string'])
|
53
|
-
]);
|
54
|
-
|
55
|
-
expect((errorsOfInvalidArray?.length || 0) > 0).toBe(true);
|
56
|
-
expect((errorsOfInvalidTuple?.length || 0) > 0).toBe(true);
|
57
|
-
expect(errorsOfValidArray?.length || 0).toBe(0);
|
58
|
-
expect(errorsOfValidTuple?.length || 0).toBe(0);
|
59
|
-
expect(parsedArray[0]).toBe(1);
|
60
|
-
expect(parsedArray[1]).toBe(2);
|
61
|
-
expect(parsedArray[2]).toBe(3);
|
62
|
-
expect(parsedTuple[0]).toBe(1);
|
63
|
-
expect(parsedTuple[1]).toBe('string');
|
64
|
-
});
|
65
|
-
|
66
|
-
test('object schema', async ({ expect }) => {
|
67
|
-
const objectSchema = p.object({
|
68
|
-
number: p.number(),
|
69
|
-
string: p.string()
|
70
|
-
});
|
71
|
-
|
72
|
-
const [{ errors: errorsOfInvalid }, { errors: errorsOfValid, parsed }] = await Promise.all([
|
73
|
-
objectSchema.parse('value' as any),
|
74
|
-
objectSchema.parse({
|
75
|
-
number: 1,
|
76
|
-
string: 'string'
|
77
|
-
})
|
78
|
-
]);
|
79
|
-
|
80
|
-
expect((errorsOfInvalid?.length || 0) > 0).toBe(true);
|
81
|
-
expect((errorsOfValid?.length || 0) === 0).toBe(true);
|
82
|
-
expect(parsed.number).toBe(1);
|
83
|
-
expect(parsed.string).toBe('string');
|
84
|
-
});
|
85
|
-
|
86
|
-
test('union schema', async ({ expect }) => {
|
87
|
-
const unionSchema = p.union([p.string(), p.number()]);
|
88
|
-
|
89
|
-
const [{ errors: errorsOfInvalid }, { errors: errorsOfValid, parsed }] = await Promise.all([
|
90
|
-
unionSchema.parse(true as any),
|
91
|
-
unionSchema.parse(2)
|
92
|
-
]);
|
93
|
-
expect((errorsOfInvalid?.length || 0) > 0).toBe(true);
|
94
|
-
expect((errorsOfValid?.length || 0) === 0).toBe(true);
|
95
|
-
expect(parsed).toBe(2);
|
96
|
-
});
|
97
|
-
});
|
@@ -1,99 +0,0 @@
|
|
1
|
-
import * as p from '@palmares/schemas';
|
2
|
-
import { getDefaultAdapter } from '@palmares/schemas';
|
3
|
-
import { describe } from '@palmares/tests';
|
4
|
-
|
5
|
-
import type JestTestAdapter from '@palmares/jest-tests';
|
6
|
-
|
7
|
-
describe<JestTestAdapter>('Union Tests', ({ test }) => {
|
8
|
-
test('optional', async ({ expect }) => {
|
9
|
-
const unionSchema = p.union([p.number(), p.string()]);
|
10
|
-
const unionSchemaWithCustomMessage = p.union([p.number(), p.string()]).nonOptional({ message: 'hello' });
|
11
|
-
|
12
|
-
const [{ errors: errorsOnFail }, { errors: errorsOnFailWithCustomMessage }, { errors: errorsOnValid, parsed }] =
|
13
|
-
await Promise.all([
|
14
|
-
unionSchema.parse(undefined as any),
|
15
|
-
unionSchemaWithCustomMessage.parse(undefined as any),
|
16
|
-
unionSchema.parse(1)
|
17
|
-
]);
|
18
|
-
|
19
|
-
expect(errorsOnFailWithCustomMessage?.[0]?.message).toBe('hello');
|
20
|
-
expect(errorsOnFail?.[0]?.code).toBe('required');
|
21
|
-
expect(errorsOnFail?.[0]?.message).toBe('Required');
|
22
|
-
expect((errorsOnValid || []).length).toBe(0);
|
23
|
-
expect(parsed).toBe(1);
|
24
|
-
});
|
25
|
-
|
26
|
-
test('nullable', async ({ expect }) => {
|
27
|
-
const unionSchema = p.union([p.number(), p.string()]);
|
28
|
-
const unionSchemaWithCustomMessage = p.union([p.number(), p.string()]).nonNullable({ message: 'hello' });
|
29
|
-
|
30
|
-
const [{ errors: errorsOnFail }, { errors: errorsOnFailWithCustomMessage }, { errors: errorsOnValid, parsed }] =
|
31
|
-
await Promise.all([
|
32
|
-
unionSchema.parse(null as any),
|
33
|
-
unionSchemaWithCustomMessage.parse(null as any),
|
34
|
-
unionSchema.parse('teste')
|
35
|
-
]);
|
36
|
-
|
37
|
-
expect(errorsOnFailWithCustomMessage?.[0]?.message).toBe('hello');
|
38
|
-
expect(errorsOnFail?.[0]?.code).toBe('null');
|
39
|
-
expect(errorsOnFail?.[0]?.message).toBe('Cannot be null');
|
40
|
-
expect((errorsOnValid || []).length).toBe(0);
|
41
|
-
expect(parsed).toBe('teste');
|
42
|
-
});
|
43
|
-
|
44
|
-
test('in case of failure use the other', async ({ expect }) => {
|
45
|
-
const unionSchema = p.union([p.number().max(20), p.number().min(12)]);
|
46
|
-
|
47
|
-
const { errors, parsed } = await unionSchema.parse(25);
|
48
|
-
expect((errors || []).length).toBe(0);
|
49
|
-
expect(parsed).toBe(25);
|
50
|
-
});
|
51
|
-
|
52
|
-
test('in case of failure, show first', async ({ expect }) => {
|
53
|
-
const unionSchema = p.union([p.number().min(25), p.number().max(20)]);
|
54
|
-
|
55
|
-
const { errors } = await unionSchema.parse(23);
|
56
|
-
expect((errors || []).length > 0).toBe(true);
|
57
|
-
});
|
58
|
-
|
59
|
-
test('works when not defined on adapter', async ({ expect }) => {
|
60
|
-
const adapter = getDefaultAdapter();
|
61
|
-
const existingUnion = adapter.union;
|
62
|
-
adapter.union = undefined;
|
63
|
-
const unionSchema = p.union([p.number().min(25), p.number().max(20)]);
|
64
|
-
const { errors } = await unionSchema.parse(23);
|
65
|
-
expect((errors || []).length > 0).toBe(true);
|
66
|
-
|
67
|
-
adapter.union = existingUnion;
|
68
|
-
});
|
69
|
-
|
70
|
-
test('nested in case of failure use the other', async ({ expect }) => {
|
71
|
-
const unionSchema = p.object({
|
72
|
-
test: p.union([p.number().max(20), p.number().min(12)] as const)
|
73
|
-
});
|
74
|
-
|
75
|
-
const { errors, parsed } = await unionSchema.parse({
|
76
|
-
test: 23
|
77
|
-
});
|
78
|
-
expect((errors || []).length).toBe(0);
|
79
|
-
expect(parsed.test).toBe(23);
|
80
|
-
});
|
81
|
-
|
82
|
-
test('nested in case of failure, show first', async ({ expect }) => {
|
83
|
-
const unionSchema = p.object({ test: p.union([p.number().min(25), p.number().max(20)]) });
|
84
|
-
|
85
|
-
const { errors } = await unionSchema.parse({ test: 23 });
|
86
|
-
expect((errors || []).length > 0).toBe(true);
|
87
|
-
});
|
88
|
-
|
89
|
-
test('nested works when not defined on adapter', async ({ expect }) => {
|
90
|
-
const adapter = getDefaultAdapter();
|
91
|
-
const existingUnion = adapter.union;
|
92
|
-
adapter.union = undefined;
|
93
|
-
const unionSchema = p.object({ test: p.union([p.number().min(25), p.number().max(20)]) });
|
94
|
-
const { errors } = await unionSchema.parse({ test: 23 });
|
95
|
-
expect((errors || []).length > 0).toBe(true);
|
96
|
-
|
97
|
-
adapter.union = existingUnion;
|
98
|
-
});
|
99
|
-
});
|
@@ -1,69 +0,0 @@
|
|
1
|
-
import ConsoleLogging from '@palmares/console-logging';
|
2
|
-
import CoreDomain, { defineSettings } from '@palmares/core';
|
3
|
-
import DatabasesDomain from '@palmares/databases';
|
4
|
-
import DrizzleEngine from '@palmares/drizzle-engine';
|
5
|
-
import { drizzle as drizzleBetterSqlite3 } from '@palmares/drizzle-engine/better-sqlite3';
|
6
|
-
import JestTestAdapter from '@palmares/jest-tests';
|
7
|
-
import LoggingDomain from '@palmares/logging';
|
8
|
-
import NodeStd from '@palmares/node-std';
|
9
|
-
import SchemasDomain from '@palmares/schemas';
|
10
|
-
import TestsDomain from '@palmares/tests';
|
11
|
-
import { ZodSchemaAdapter } from '@palmares/zod-schema';
|
12
|
-
import Database from 'better-sqlite3';
|
13
|
-
import { dirname, resolve } from 'path';
|
14
|
-
|
15
|
-
import CustomCoreDomain from './core';
|
16
|
-
//import * as schema from '../.drizzle/schema';
|
17
|
-
|
18
|
-
const database = new Database('sqlite.db');
|
19
|
-
|
20
|
-
const args = DrizzleEngine.new({
|
21
|
-
output: './.drizzle/schema.ts',
|
22
|
-
type: 'better-sqlite3',
|
23
|
-
drizzle: drizzleBetterSqlite3(database)
|
24
|
-
});
|
25
|
-
export const db = args[1].instance.instance;
|
26
|
-
export default defineSettings({
|
27
|
-
basePath: dirname(resolve(__dirname)),
|
28
|
-
settingsLocation: __filename,
|
29
|
-
std: NodeStd,
|
30
|
-
installedDomains: [
|
31
|
-
[
|
32
|
-
LoggingDomain,
|
33
|
-
{
|
34
|
-
logger: ConsoleLogging
|
35
|
-
}
|
36
|
-
],
|
37
|
-
// Domain Core, required for palmares to work
|
38
|
-
[
|
39
|
-
CoreDomain,
|
40
|
-
{
|
41
|
-
appName: 'example'
|
42
|
-
}
|
43
|
-
],
|
44
|
-
[
|
45
|
-
TestsDomain,
|
46
|
-
{
|
47
|
-
testAdapter: JestTestAdapter
|
48
|
-
}
|
49
|
-
],
|
50
|
-
[
|
51
|
-
SchemasDomain,
|
52
|
-
{
|
53
|
-
schemaAdapter: ZodSchemaAdapter
|
54
|
-
}
|
55
|
-
],
|
56
|
-
[
|
57
|
-
DatabasesDomain,
|
58
|
-
{
|
59
|
-
databases: {
|
60
|
-
default: {
|
61
|
-
engine: args
|
62
|
-
}
|
63
|
-
}
|
64
|
-
}
|
65
|
-
],
|
66
|
-
// We have just created this custom domain, and it defines our routes.
|
67
|
-
CustomCoreDomain
|
68
|
-
]
|
69
|
-
});
|
package/__tests__/tsconfig.json
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"extends": "../../../tsconfig.json",
|
3
|
-
"compilerOptions": {
|
4
|
-
"declaration": true,
|
5
|
-
"module": "ESNext",
|
6
|
-
"moduleResolution": "Bundler",
|
7
|
-
"outDir": "./dist",
|
8
|
-
"types": ["node"]
|
9
|
-
},
|
10
|
-
"include": ["./src/**/*", "manage.drizzle.ts", "manage.sequelize.ts", "./src/core/types.test.ts"]
|
11
|
-
}
|
@@ -1,31 +0,0 @@
|
|
1
|
-
import { FieldAdapter } from '.';
|
2
|
-
|
3
|
-
import type { WithFallback } from '../../utils';
|
4
|
-
import type { ArrayAdapterTranslateArgs } from '../types';
|
5
|
-
|
6
|
-
export function arrayFieldAdapter<
|
7
|
-
TTranslate extends ArrayFieldAdapter['translate'],
|
8
|
-
TToString extends ArrayFieldAdapter['toString'],
|
9
|
-
TFormatError extends ArrayFieldAdapter['formatError'],
|
10
|
-
TParse extends ArrayFieldAdapter['parse']
|
11
|
-
>(args: { translate: TTranslate; toString?: TToString; formatError?: TFormatError; parse?: TParse }) {
|
12
|
-
class CustomArrayFieldAdapter extends ArrayFieldAdapter {
|
13
|
-
translate = args.translate;
|
14
|
-
toString = args.toString as TToString;
|
15
|
-
formatError = args.formatError as TFormatError;
|
16
|
-
parse = args.parse as TParse;
|
17
|
-
}
|
18
|
-
|
19
|
-
return CustomArrayFieldAdapter as typeof ArrayFieldAdapter & {
|
20
|
-
new (): ArrayFieldAdapter & {
|
21
|
-
translate: TTranslate;
|
22
|
-
toString: TToString;
|
23
|
-
formatError: TFormatError;
|
24
|
-
parse: TParse;
|
25
|
-
};
|
26
|
-
};
|
27
|
-
}
|
28
|
-
|
29
|
-
export class ArrayFieldAdapter extends FieldAdapter {
|
30
|
-
translate(_fieldAdapter: FieldAdapter, _args: ArrayAdapterTranslateArgs): any | WithFallback<'array'> {}
|
31
|
-
}
|
@@ -1,43 +0,0 @@
|
|
1
|
-
import { FieldAdapter } from '.';
|
2
|
-
import { SchemaAdapterNotImplementedError } from '../../exceptions';
|
3
|
-
|
4
|
-
import type { SchemaAdapter } from '..';
|
5
|
-
import type { WithFallback } from '../../utils';
|
6
|
-
import type { BooleanAdapterTranslateArgs } from '../types';
|
7
|
-
|
8
|
-
export function booleanFieldAdapter<
|
9
|
-
TTranslate extends BooleanFieldAdapter['translate'],
|
10
|
-
TToString extends BooleanFieldAdapter['toString'],
|
11
|
-
TFormatError extends BooleanFieldAdapter['formatError'],
|
12
|
-
TParse extends BooleanFieldAdapter['parse']
|
13
|
-
>(args: { translate: TTranslate; toString?: TToString; formatError?: TFormatError; parse?: TParse }) {
|
14
|
-
class CustomBooleanFieldAdapter extends BooleanFieldAdapter {
|
15
|
-
translate = args.translate;
|
16
|
-
toString = args.toString as TToString;
|
17
|
-
formatError = args.formatError as TFormatError;
|
18
|
-
parse = args.parse as TParse;
|
19
|
-
}
|
20
|
-
|
21
|
-
return CustomBooleanFieldAdapter as typeof BooleanFieldAdapter & {
|
22
|
-
new (): BooleanFieldAdapter & {
|
23
|
-
translate: TTranslate;
|
24
|
-
toString: TToString;
|
25
|
-
formatError: TFormatError;
|
26
|
-
parse: TParse;
|
27
|
-
};
|
28
|
-
};
|
29
|
-
}
|
30
|
-
|
31
|
-
export class BooleanFieldAdapter extends FieldAdapter {
|
32
|
-
translate(_fieldAdapter: FieldAdapter, _args: BooleanAdapterTranslateArgs): any | WithFallback<'boolean'> {}
|
33
|
-
|
34
|
-
parse(
|
35
|
-
_adapter: SchemaAdapter,
|
36
|
-
_fieldAdapter: FieldAdapter,
|
37
|
-
_result: any,
|
38
|
-
_value: any,
|
39
|
-
_args: BooleanAdapterTranslateArgs
|
40
|
-
): Promise<{ errors: any; parsed: any }> {
|
41
|
-
throw new SchemaAdapterNotImplementedError({ className: this.constructor.name, functionName: 'parse' });
|
42
|
-
}
|
43
|
-
}
|
@@ -1,43 +0,0 @@
|
|
1
|
-
import { FieldAdapter } from '.';
|
2
|
-
import { SchemaAdapterNotImplementedError } from '../../exceptions';
|
3
|
-
|
4
|
-
import type { SchemaAdapter } from '..';
|
5
|
-
import type { WithFallback } from '../../utils';
|
6
|
-
import type { DatetimeAdapterTranslateArgs } from '../types';
|
7
|
-
|
8
|
-
export function datetimeFieldAdapter<
|
9
|
-
TTranslate extends DatetimeFieldAdapter['translate'],
|
10
|
-
TToString extends DatetimeFieldAdapter['toString'],
|
11
|
-
TFormatError extends DatetimeFieldAdapter['formatError'],
|
12
|
-
TParse extends DatetimeFieldAdapter['parse']
|
13
|
-
>(args: { translate: TTranslate; toString?: TToString; formatError?: TFormatError; parse?: TParse }) {
|
14
|
-
class CustomDatetimeFieldAdapter extends DatetimeFieldAdapter {
|
15
|
-
translate = args.translate;
|
16
|
-
toString = args.toString as TToString;
|
17
|
-
formatError = args.formatError as TFormatError;
|
18
|
-
parse = args.parse as TParse;
|
19
|
-
}
|
20
|
-
|
21
|
-
return CustomDatetimeFieldAdapter as typeof DatetimeFieldAdapter & {
|
22
|
-
new (): DatetimeFieldAdapter & {
|
23
|
-
translate: TTranslate;
|
24
|
-
toString: TToString;
|
25
|
-
formatError: TFormatError;
|
26
|
-
parse: TParse;
|
27
|
-
};
|
28
|
-
};
|
29
|
-
}
|
30
|
-
|
31
|
-
export class DatetimeFieldAdapter extends FieldAdapter {
|
32
|
-
translate(_fieldAdapter: FieldAdapter, _args: DatetimeAdapterTranslateArgs): any | WithFallback<'datetime'> {}
|
33
|
-
|
34
|
-
parse(
|
35
|
-
_adapter: SchemaAdapter,
|
36
|
-
_fieldAdapter: FieldAdapter,
|
37
|
-
_result: any,
|
38
|
-
_value: any,
|
39
|
-
_args: DatetimeAdapterTranslateArgs
|
40
|
-
): Promise<{ errors: any; parsed: any }> {
|
41
|
-
throw new SchemaAdapterNotImplementedError({ className: this.constructor.name, functionName: 'parse' });
|
42
|
-
}
|
43
|
-
}
|