Skip to content

fix(json-api-nestjs): Fix validation #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ describe('Check "filter" zod schema', () => {
},
relation: null,
};
const check9: FilterQuerySchema = {
target: {
createdAt: {
eq: 'null',
},
},
relation: null,
};

const checkArray = [
check1,
Expand All @@ -235,6 +243,10 @@ describe('Check "filter" zod schema', () => {
const result = filterQuerySchema.parse(check);
expect(result).toEqual(check);
}
const result = filterQuerySchema.parse(check9);
expect(result.target!.createdAt!.eq).toEqual(null);
result.target!.createdAt!.eq = 'null';
expect(result).toEqual(check9);
});

it('Invalid schema', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import {
z,
ZodArray,
ZodEffects,
ZodLiteral,
ZodNullable,
ZodObject,
ZodOptional,
ZodString,
ZodUnion,
} from 'zod';
import {
arrayItemStringLongerThan,
Expand Down Expand Up @@ -41,15 +43,21 @@ import {
ZodFilterRelationSchema,
} from '../zod-input-query-schema/filter';

type ZodForString = ZodEffects<ZodString>;
const zodForString: ZodForString = z.string().refine(stringLongerThan(), {
message: 'String should be not empty',
});
type ZodForString = ZodUnion<
[ZodEffects<ZodLiteral<'null'>, null, 'null'>, ZodEffects<ZodString>]
>;

const zodForString: ZodForString = z.union([
z.literal('null').transform(() => null),
z.string().refine(stringLongerThan(), {
message: 'String should be not empty',
}),
]);

type ZodForStringArray = ZodEffects<
ZodArray<ZodForString, 'atleastone'>,
string[],
string[]
[string | null, ...(string | null)[]],
[string, ...string[]]
>;
const zodForStringArray: ZodForStringArray = zodForString
.array()
Expand Down
7 changes: 4 additions & 3 deletions libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ export const stringLongerThan =

export const arrayItemStringLongerThan =
(length = 0) =>
(array: [string, ...string[]]) => {
(array: [string | null, ...(string | null)[]]) => {
const checkFunction = stringLongerThan(length);
return !array.some((i) => !checkFunction(i));
return !array.some((i) => i !== null && !checkFunction(i));
};

export const stringMustBe =
(type: TypeField = TypeField.string) =>
(inputString: string) => {
(inputString: string | null) => {
if (inputString === null) return true;
switch (type) {
case TypeField.boolean:
return inputString === 'true' || inputString === 'false';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
} from '../../constants';
import { TypeormUtilsService } from './typeorm-utils.service';
import {
ObjectTyped,
PostData,
PostRelationshipData,
Query,
Expand Down Expand Up @@ -253,7 +252,7 @@ describe('TypeormUtilsService', () => {
`test for ${filterOperand}`;
const valueTestArray = (
filterOperand: FilterOperand.nin | FilterOperand.in
) => [valueTest(filterOperand)];
): [string, ...string[]] => [valueTest(filterOperand)];

const query = getDefaultQuery<Users>();
query.filter.target = {
Expand Down Expand Up @@ -429,9 +428,10 @@ describe('TypeormUtilsService', () => {
createdAt: {
[FilterOperand.eq]: 'test1',
[FilterOperand.ne]: 'test2',
[FilterOperand.nin]: ['test3'],
[FilterOperand.nin]: ['test3'] as [string, ...string[]],
},
};

query.filter.relation = {
roles: conditional,
};
Expand Down
Loading