Skip to content

Commit 1d048a8

Browse files
committed
fix(json-api-nestjs): Fix validation
Fix validation if try create conditional for date filed. If check field for null validation throw error: "String should be as date" Closes: #81
1 parent cd0d9d4 commit 1d048a8

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

Diff for: libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-query-schema/filter.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,14 @@ describe('Check "filter" zod schema', () => {
220220
},
221221
relation: null,
222222
};
223+
const check9: FilterQuerySchema = {
224+
target: {
225+
createdAt: {
226+
eq: 'null',
227+
},
228+
},
229+
relation: null,
230+
};
223231

224232
const checkArray = [
225233
check1,
@@ -235,6 +243,10 @@ describe('Check "filter" zod schema', () => {
235243
const result = filterQuerySchema.parse(check);
236244
expect(result).toEqual(check);
237245
}
246+
const result = filterQuerySchema.parse(check9);
247+
expect(result.target!.createdAt!.eq).toEqual(null);
248+
result.target!.createdAt!.eq = 'null';
249+
expect(result).toEqual(check9);
238250
});
239251

240252
it('Invalid schema', () => {

Diff for: libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-query-schema/filter.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import {
22
z,
33
ZodArray,
44
ZodEffects,
5+
ZodLiteral,
56
ZodNullable,
67
ZodObject,
78
ZodOptional,
89
ZodString,
10+
ZodUnion,
911
} from 'zod';
1012
import {
1113
arrayItemStringLongerThan,
@@ -41,15 +43,21 @@ import {
4143
ZodFilterRelationSchema,
4244
} from '../zod-input-query-schema/filter';
4345

44-
type ZodForString = ZodEffects<ZodString>;
45-
const zodForString: ZodForString = z.string().refine(stringLongerThan(), {
46-
message: 'String should be not empty',
47-
});
46+
type ZodForString = ZodUnion<
47+
[ZodEffects<ZodLiteral<'null'>, null, 'null'>, ZodEffects<ZodString>]
48+
>;
49+
50+
const zodForString: ZodForString = z.union([
51+
z.literal('null').transform(() => null),
52+
z.string().refine(stringLongerThan(), {
53+
message: 'String should be not empty',
54+
}),
55+
]);
4856

4957
type ZodForStringArray = ZodEffects<
5058
ZodArray<ZodForString, 'atleastone'>,
51-
string[],
52-
string[]
59+
[string | null, ...(string | null)[]],
60+
[string, ...string[]]
5361
>;
5462
const zodForStringArray: ZodForStringArray = zodForString
5563
.array()

Diff for: libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-utils.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ export const stringLongerThan =
1414

1515
export const arrayItemStringLongerThan =
1616
(length = 0) =>
17-
(array: [string, ...string[]]) => {
17+
(array: [string | null, ...(string | null)[]]) => {
1818
const checkFunction = stringLongerThan(length);
19-
return !array.some((i) => !checkFunction(i));
19+
return !array.some((i) => i !== null && !checkFunction(i));
2020
};
2121

2222
export const stringMustBe =
2323
(type: TypeField = TypeField.string) =>
24-
(inputString: string) => {
24+
(inputString: string | null) => {
25+
if (inputString === null) return true;
2526
switch (type) {
2627
case TypeField.boolean:
2728
return inputString === 'true' || inputString === 'false';

Diff for: libs/json-api/json-api-nestjs/src/lib/mixin/service/typeorm-utils.service.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {
2626
} from '../../constants';
2727
import { TypeormUtilsService } from './typeorm-utils.service';
2828
import {
29-
ObjectTyped,
3029
PostData,
3130
PostRelationshipData,
3231
Query,
@@ -253,7 +252,7 @@ describe('TypeormUtilsService', () => {
253252
`test for ${filterOperand}`;
254253
const valueTestArray = (
255254
filterOperand: FilterOperand.nin | FilterOperand.in
256-
) => [valueTest(filterOperand)];
255+
): [string, ...string[]] => [valueTest(filterOperand)];
257256

258257
const query = getDefaultQuery<Users>();
259258
query.filter.target = {
@@ -429,9 +428,10 @@ describe('TypeormUtilsService', () => {
429428
createdAt: {
430429
[FilterOperand.eq]: 'test1',
431430
[FilterOperand.ne]: 'test2',
432-
[FilterOperand.nin]: ['test3'],
431+
[FilterOperand.nin]: ['test3'] as [string, ...string[]],
433432
},
434433
};
434+
435435
query.filter.relation = {
436436
roles: conditional,
437437
};

0 commit comments

Comments
 (0)