@palmares/schemas 0.1.21 → 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.
Files changed (78) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/package.json +10 -4
  3. package/.turbo/turbo-build$colon$watch.log +0 -24
  4. package/.turbo/turbo-build.log +0 -13
  5. package/.turbo/turbo-build:watch.log +0 -26
  6. package/__tests__/.drizzle/migrations/0000_skinny_harrier.sql +0 -22
  7. package/__tests__/.drizzle/migrations/meta/0000_snapshot.json +0 -156
  8. package/__tests__/.drizzle/migrations/meta/_journal.json +0 -13
  9. package/__tests__/.drizzle/schema.ts +0 -35
  10. package/__tests__/drizzle.config.ts +0 -11
  11. package/__tests__/eslint.config.js +0 -10
  12. package/__tests__/manage.ts +0 -5
  13. package/__tests__/node_modules/.bin/drizzle-kit +0 -17
  14. package/__tests__/node_modules/.bin/node-gyp +0 -17
  15. package/__tests__/node_modules/.bin/tsc +0 -17
  16. package/__tests__/node_modules/.bin/tsserver +0 -17
  17. package/__tests__/node_modules/.bin/tsx +0 -17
  18. package/__tests__/package.json +0 -34
  19. package/__tests__/sqlite.db +0 -0
  20. package/__tests__/src/core/array.test.ts +0 -131
  21. package/__tests__/src/core/boolean.test.ts +0 -66
  22. package/__tests__/src/core/datetime.test.ts +0 -102
  23. package/__tests__/src/core/index.ts +0 -35
  24. package/__tests__/src/core/model.test.ts +0 -260
  25. package/__tests__/src/core/models.ts +0 -50
  26. package/__tests__/src/core/numbers.test.ts +0 -177
  27. package/__tests__/src/core/object.test.ts +0 -218
  28. package/__tests__/src/core/string.test.ts +0 -222
  29. package/__tests__/src/core/test.test.ts +0 -59
  30. package/__tests__/src/core/types.test.ts +0 -97
  31. package/__tests__/src/core/union.test.ts +0 -99
  32. package/__tests__/src/settings.ts +0 -69
  33. package/__tests__/tsconfig.json +0 -11
  34. package/src/adapter/fields/array.ts +0 -31
  35. package/src/adapter/fields/boolean.ts +0 -43
  36. package/src/adapter/fields/datetime.ts +0 -43
  37. package/src/adapter/fields/index.ts +0 -72
  38. package/src/adapter/fields/number.ts +0 -43
  39. package/src/adapter/fields/object.ts +0 -52
  40. package/src/adapter/fields/string.ts +0 -43
  41. package/src/adapter/fields/union.ts +0 -43
  42. package/src/adapter/index.ts +0 -37
  43. package/src/adapter/types.ts +0 -276
  44. package/src/compile.ts +0 -14
  45. package/src/conf.ts +0 -30
  46. package/src/constants.ts +0 -7
  47. package/src/domain.ts +0 -15
  48. package/src/exceptions.ts +0 -17
  49. package/src/index.ts +0 -318
  50. package/src/middleware.ts +0 -52
  51. package/src/model.ts +0 -518
  52. package/src/parsers/convert-from-number.ts +0 -13
  53. package/src/parsers/convert-from-string.ts +0 -19
  54. package/src/parsers/index.ts +0 -2
  55. package/src/schema/array.ts +0 -825
  56. package/src/schema/boolean.ts +0 -792
  57. package/src/schema/datetime.ts +0 -704
  58. package/src/schema/index.ts +0 -5
  59. package/src/schema/number.ts +0 -929
  60. package/src/schema/object.ts +0 -799
  61. package/src/schema/schema.ts +0 -1179
  62. package/src/schema/string.ts +0 -941
  63. package/src/schema/types.ts +0 -154
  64. package/src/schema/union.ts +0 -724
  65. package/src/types.ts +0 -66
  66. package/src/utils.ts +0 -389
  67. package/src/validators/array.ts +0 -183
  68. package/src/validators/boolean.ts +0 -52
  69. package/src/validators/datetime.ts +0 -121
  70. package/src/validators/number.ts +0 -178
  71. package/src/validators/object.ts +0 -56
  72. package/src/validators/schema.ts +0 -142
  73. package/src/validators/string.ts +0 -278
  74. package/src/validators/types.ts +0 -1
  75. package/src/validators/union.ts +0 -52
  76. package/src/validators/utils.ts +0 -226
  77. package/tsconfig.json +0 -9
  78. package/tsconfig.types.json +0 -10
@@ -1,218 +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>('Object Tests', ({ test }) => {
7
- test('optional', async ({ expect }) => {
8
- const objectSchema = p.object({
9
- name: p.string(),
10
- age: p.number()
11
- });
12
- const objectSchemaWithCustomMessage = p
13
- .object({
14
- name: p.string(),
15
- age: p.number()
16
- })
17
- .nonOptional({ message: 'hello' });
18
-
19
- const [{ errors: errorsOnFail }, { errors: errorsOnFailWithCustomMessage }, { errors: errorsOnValid, parsed }] =
20
- await Promise.all([
21
- objectSchema.parse(undefined as any),
22
- objectSchemaWithCustomMessage.parse(undefined as any),
23
- objectSchema.parse({
24
- name: 'John',
25
- age: 30
26
- })
27
- ]);
28
-
29
- expect(errorsOnFailWithCustomMessage?.[0]?.message).toBe('hello');
30
- expect(errorsOnFail?.[0]?.code).toBe('required');
31
- expect(errorsOnFail?.[0]?.message).toBe('Required');
32
- expect((errorsOnValid || []).length).toBe(0);
33
- expect(parsed.age).toBe(30);
34
- expect(parsed.name).toBe('John');
35
- });
36
-
37
- test('nullable', async ({ expect }) => {
38
- const objectSchema = p.object({
39
- name: p.string(),
40
- age: p.number()
41
- });
42
- const objectSchemaWithCustomMessage = p
43
- .object({
44
- name: p.string(),
45
- age: p.number()
46
- })
47
- .nonNullable({ message: 'hello' });
48
-
49
- const [{ errors: errorsOnFail }, { errors: errorsOnFailWithCustomMessage }, { errors: errorsOnValid, parsed }] =
50
- await Promise.all([
51
- objectSchema.parse(null as any),
52
- objectSchemaWithCustomMessage.parse(null as any),
53
- objectSchema.parse({
54
- name: 'John',
55
- age: 30
56
- })
57
- ]);
58
-
59
- expect(errorsOnFailWithCustomMessage?.[0]?.message).toBe('hello');
60
- expect(errorsOnFail?.[0]?.code).toBe('null');
61
- expect(errorsOnFail?.[0]?.message).toBe('Cannot be null');
62
- expect((errorsOnValid || []).length).toBe(0);
63
- expect(parsed.age).toBe(30);
64
- expect(parsed.name).toBe('John');
65
- });
66
-
67
- test('optional on key', async ({ expect }) => {
68
- const objectSchema = p.object({
69
- name: p.string(),
70
- age: p.number()
71
- });
72
- const objectSchemaWithCustomMessage = p.object({
73
- name: p.string(),
74
- age: p.number().nonOptional({ message: 'hello' })
75
- });
76
-
77
- const [{ errors: errorsOnFail }, { errors: errorsOnFailWithCustomMessage }, { errors: errorsOnValid, parsed }] =
78
- await Promise.all([
79
- objectSchema.parse({
80
- name: 'John'
81
- } as any),
82
- objectSchemaWithCustomMessage.parse({
83
- name: 'John'
84
- } as any),
85
- objectSchema.parse({
86
- name: 'John',
87
- age: 30
88
- })
89
- ]);
90
-
91
- expect(errorsOnFailWithCustomMessage?.[0]?.message).toBe('hello');
92
- expect(errorsOnFail?.[0]?.code).toBe('required');
93
- expect(errorsOnFail?.[0]?.message).toBe('Required');
94
- expect(errorsOnFail?.[0]?.path?.[0]).toBe('age');
95
- expect((errorsOnValid || []).length).toBe(0);
96
- expect(parsed.age).toBe(30);
97
- expect(parsed.name).toBe('John');
98
- });
99
-
100
- test('nullable on key', async ({ expect }) => {
101
- const objectSchema = p.object({
102
- name: p.string(),
103
- age: p.number()
104
- });
105
- const objectSchemaWithCustomMessage = p.object({
106
- name: p.string(),
107
- age: p.number().nonNullable({ message: 'hello' })
108
- });
109
-
110
- const [{ errors: errorsOnFail }, { errors: errorsOnFailWithCustomMessage }, { errors: errorsOnValid, parsed }] =
111
- await Promise.all([
112
- objectSchema.parse({
113
- name: 'John',
114
- age: null
115
- } as any),
116
- objectSchemaWithCustomMessage.parse({
117
- name: 'John',
118
- age: null
119
- } as any),
120
- objectSchema.parse({
121
- name: 'John',
122
- age: 30
123
- })
124
- ]);
125
-
126
- expect(errorsOnFailWithCustomMessage?.[0]?.message).toBe('hello');
127
- expect(errorsOnFail?.[0]?.code).toBe('null');
128
- expect(errorsOnFail?.[0]?.message).toBe('Cannot be null');
129
- expect(errorsOnFail?.[0]?.path?.[0]).toBe('age');
130
- expect((errorsOnValid || []).length).toBe(0);
131
- expect(parsed.age).toBe(30);
132
- expect(parsed.name).toBe('John');
133
- });
134
-
135
- test('max from string and number', async ({ expect }) => {
136
- const objectSchema = p.object({
137
- name: p.string().maxLength(10),
138
- age: p.number().max(10)
139
- });
140
-
141
- const [{ errors: errorsWhenString }, { errors: errorWhenNumber }] = await Promise.all([
142
- objectSchema.parse({
143
- name: 'John12345789123',
144
- age: 10
145
- } as any),
146
- objectSchema.parse({
147
- name: 'John',
148
- age: 12
149
- } as any)
150
- ]);
151
-
152
- expect(errorsWhenString?.[0]?.code).toBe('maxLength');
153
- expect(errorWhenNumber?.[0]?.code).toBe('max');
154
- });
155
-
156
- test('min from string and number', async ({ expect }) => {
157
- const objectSchema = p.object({
158
- name: p.string().minLength(10),
159
- age: p.number().min(10)
160
- });
161
-
162
- const [{ errors: errorsWhenString }, { errors: errorWhenNumber }] = await Promise.all([
163
- objectSchema.parse({
164
- name: 'John',
165
- age: 11
166
- } as any),
167
- objectSchema.parse({
168
- name: 'John123456789123',
169
- age: 9
170
- } as any)
171
- ]);
172
-
173
- expect(errorsWhenString?.[0]?.code).toBe('minLength');
174
- expect(errorWhenNumber?.[0]?.code).toBe('min');
175
- });
176
-
177
- test('nested issue', async ({ expect }) => {
178
- const objectSchema = p.object({
179
- nested: p.object({
180
- name: p.string(),
181
- age: p.number().max(10)
182
- }),
183
- value: p.number()
184
- });
185
-
186
- const { errors } = await objectSchema.parse({
187
- nested: {
188
- name: 'John',
189
- age: 12
190
- },
191
- value: 20
192
- });
193
-
194
- expect(errors?.[0]?.code).toBe('max');
195
- expect(errors?.[0]?.path?.[0]).toBe('nested');
196
- expect(errors?.[0]?.path?.[1]).toBe('age');
197
- });
198
-
199
- test('nested issue error message', async ({ expect }) => {
200
- const schema = p.object({
201
- name: p.string(),
202
- age: p.number(),
203
- }).toInternal(async (data) => {
204
- return {
205
- name: 'Hello ' + data.name,
206
- age: data.age,
207
- };
208
- }).toRepresentation(async (data) => {
209
- return {
210
- name: 'Hello from backend',
211
- };
212
- });
213
-
214
- const data = await schema.data({ name: 'John', age: 20 });
215
- expect(data.name).toBe('Hello from backend');
216
- });
217
- });
218
-
@@ -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
- });