Skip to content

Commit 23709bd

Browse files
committed
feat(json-api-nestjs): bump zod to v4
1 parent 1a47099 commit 23709bd

File tree

46 files changed

+300
-293
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+300
-293
lines changed

libs/json-api/json-api-nestjs/src/lib/modules/atomic-operation/pipes/input-operation.pipe.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
PipeTransform,
66
} from '@nestjs/common';
77
import { KEY_MAIN_INPUT_SCHEMA } from '@klerick/json-api-nestjs-shared';
8-
import { errorMap } from 'zod-validation-error';
98
import { ZodError } from 'zod';
109
import { JSONValue } from '../../mixin/types';
1110
import { InputArray, ZodInputOperation } from '../utils';
@@ -19,9 +18,7 @@ export class InputOperationPipe
1918

2019
transform(value: JSONValue): InputArray {
2120
try {
22-
return this.zodInputOperation.parse(value, {
23-
errorMap: errorMap,
24-
})[KEY_MAIN_INPUT_SCHEMA];
21+
return this.zodInputOperation.parse(value)[KEY_MAIN_INPUT_SCHEMA];
2522
} catch (e) {
2623
if (e instanceof ZodError) {
2724
throw new BadRequestException(e.issues);

libs/json-api/json-api-nestjs/src/lib/modules/atomic-operation/service/swagger.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
22
import { ModuleRef } from '@nestjs/core';
33
import { ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger';
4-
import { generateSchema } from '@anatine/zod-openapi';
4+
import { z } from 'zod';
55
import {
66
ReferenceObject,
77
SchemaObject,
@@ -34,7 +34,7 @@ export class SwaggerService implements OnModuleInit {
3434

3535
ApiBody({
3636
description: `Json api schema for new atomic operatiom`,
37-
schema: generateSchema(this.typeZodInputOperation) as
37+
schema: z.toJSONSchema(this.typeZodInputOperation) as
3838
| SchemaObject
3939
| ReferenceObject,
4040
required: true,

libs/json-api/json-api-nestjs/src/lib/modules/atomic-operation/utils/zod/zod-helper.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
ZodObject,
1515
ZodOptional,
1616
ZodString,
17-
ZodType,
1817
ZodUnion,
1918
} from 'zod';
2019
import { kebabCase } from 'change-case-commonjs';
@@ -24,15 +23,8 @@ import { MapController } from '../../types';
2423
import { UnionToTuple } from '../../../../types';
2524
import { EntityParamMap } from '../../../mixin/types';
2625

27-
const literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()]);
28-
type Literal = z.infer<typeof literalSchema>;
29-
type Json = Literal | { [key: string]: Json } | Json[];
3026

31-
const jsonSchema: ZodType<Json> = z.lazy(() =>
32-
z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)])
33-
);
34-
35-
const zodGeneralData = jsonSchema.nullable();
27+
const zodGeneralData = z.json().nullable();
3628
type ZodGeneral = typeof zodGeneralData;
3729

3830
export type ZodAdd<T extends string> = ReturnType<typeof zodAdd<T>>;
@@ -117,8 +109,7 @@ export type ZodInputArray = ZodArray<
117109
tmpId: ZodOptional<ZodUnion<[ZodNumber, ZodString]>>;
118110
}>;
119111
data: ZodOptional<ZodGeneral>;
120-
}>,
121-
'atleastone'
112+
}>
122113
>;
123114

124115
export type ZodInputOperation<E extends object> = ReturnType<
@@ -159,13 +150,13 @@ export function zodInputOperation<E extends object>(
159150
if (hasOwnProperty('deleteOne')) {
160151
array.push(zodRemove(typeName));
161152
}
162-
if (hasOwnProperty('postRelationship')) {
153+
if (hasOwnProperty('postRelationship') && relations.length > 0) {
163154
array.push(zodOperationRel(typeName, relations, Operation.add));
164155
}
165-
if (hasOwnProperty('deleteRelationship')) {
156+
if (hasOwnProperty('deleteRelationship') && relations.length > 0) {
166157
array.push(zodOperationRel(typeName, relations, Operation.remove));
167158
}
168-
if (hasOwnProperty('patchRelationship')) {
159+
if (hasOwnProperty('patchRelationship') && relations.length > 0) {
169160
array.push(zodOperationRel(typeName, relations, Operation.update));
170161
}
171162
}

libs/json-api/json-api-nestjs/src/lib/modules/mixin/pipe/patch-input/patch-input.pipe.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ describe('PostInputPipe', () => {
4242
.mockReturnValue({ data: expectedData } as any);
4343

4444
expect(pipe.transform(input)).toEqual(expectedData);
45-
expect(mockSchema.parse).toHaveBeenCalledWith(input, {
46-
errorMap: expect.any(Function),
47-
});
45+
expect(mockSchema.parse).toHaveBeenCalledWith(input);
4846
});
4947

5048
it('should throw BadRequestException if ZodError occurs', () => {

libs/json-api/json-api-nestjs/src/lib/modules/mixin/pipe/patch-input/patch-input.pipe.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
PipeTransform,
66
} from '@nestjs/common';
77
import { ZodError } from 'zod';
8-
import { errorMap } from 'zod-validation-error';
98

109
import { JSONValue } from '../../types';
1110
import { PatchData, ZodPatch } from '../../zod';
@@ -18,9 +17,7 @@ export class PatchInputPipe<E extends object>
1817
private zodInputPatchSchema!: ZodPatch<E, 'id'>;
1918
transform(value: JSONValue): PatchData<E, 'id'> {
2019
try {
21-
return this.zodInputPatchSchema.parse(value, {
22-
errorMap: errorMap,
23-
})['data'] as PatchData<E, 'id'>;
20+
return this.zodInputPatchSchema.parse(value)['data'] as PatchData<E, 'id'>;
2421
} catch (e) {
2522
if (e instanceof ZodError) {
2623
throw new BadRequestException(e.issues);

libs/json-api/json-api-nestjs/src/lib/modules/mixin/pipe/patch-relationship/patch-relationship.pipe.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
PipeTransform,
66
} from '@nestjs/common';
77
import { ZodError } from 'zod';
8-
import { errorMap } from 'zod-validation-error';
98

109
import { JSONValue } from '../../types';
1110
import { PatchRelationshipData, ZodPatchRelationship } from '../../zod';
@@ -18,9 +17,7 @@ export class PatchRelationshipPipe
1817
private zodInputPatchRelationshipSchema!: ZodPatchRelationship;
1918
transform(value: JSONValue): PatchRelationshipData {
2019
try {
21-
return this.zodInputPatchRelationshipSchema.parse(value, {
22-
errorMap: errorMap,
23-
})['data'];
20+
return this.zodInputPatchRelationshipSchema.parse(value)['data'];
2421
} catch (e) {
2522
if (e instanceof ZodError) {
2623
throw new BadRequestException(e.issues);

libs/json-api/json-api-nestjs/src/lib/modules/mixin/pipe/post-input/post-input.pipe.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ describe('PostInputPipe', () => {
4141
.mockReturnValue({ data: expectedData } as any);
4242

4343
expect(pipe.transform(input)).toEqual(expectedData);
44-
expect(mockSchema.parse).toHaveBeenCalledWith(input, {
45-
errorMap: expect.any(Function),
46-
});
44+
expect(mockSchema.parse).toHaveBeenCalledWith(input);
4745
});
4846

4947
it('should throw BadRequestException if ZodError occurs', () => {

libs/json-api/json-api-nestjs/src/lib/modules/mixin/pipe/post-input/post-input.pipe.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
PipeTransform,
66
} from '@nestjs/common';
77
import { ZodError } from 'zod';
8-
import { errorMap } from 'zod-validation-error';
98

109
import { PostData, ZodPost } from '../../zod';
1110
import { ZOD_POST_SCHEMA } from '../../../../constants';
@@ -17,9 +16,7 @@ export class PostInputPipe<E extends object>
1716
@Inject(ZOD_POST_SCHEMA) private zodInputPostSchema!: ZodPost<E, 'id'>;
1817
transform(value: JSONValue): PostData<E, 'id'> {
1918
try {
20-
return this.zodInputPostSchema.parse(value, {
21-
errorMap: errorMap,
22-
})['data'] as PostData<E, 'id'>;
19+
return this.zodInputPostSchema.parse(value)['data'] as PostData<E, 'id'>;
2320
} catch (e) {
2421
if (e instanceof ZodError) {
2522
throw new BadRequestException(e.issues);

libs/json-api/json-api-nestjs/src/lib/modules/mixin/pipe/post-relationship/post-relationship.pipe.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
PipeTransform,
66
} from '@nestjs/common';
77
import { ZodError } from 'zod';
8-
import { errorMap } from 'zod-validation-error';
98

109
import { JSONValue } from '../../types';
1110
import { PostRelationshipData, ZodPostRelationship } from '../../zod';
@@ -18,9 +17,7 @@ export class PostRelationshipPipe
1817
private zodInputPostRelationshipSchema!: ZodPostRelationship;
1918
transform(value: JSONValue): PostRelationshipData {
2019
try {
21-
return this.zodInputPostRelationshipSchema.parse(value, {
22-
errorMap: errorMap,
23-
})['data'];
20+
return this.zodInputPostRelationshipSchema.parse(value)['data'];
2421
} catch (e) {
2522
if (e instanceof ZodError) {
2623
throw new BadRequestException(e.issues);

libs/json-api/json-api-nestjs/src/lib/modules/mixin/pipe/query-filed-on-include/query-filed-in-include.pipe.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ export class QueryFiledInIncludePipe<E extends object>
3434
}
3535

3636
if (fields) {
37-
const { target: targetResourceFields, ...relationFields } = fields;
38-
const selectRelationFields = ObjectTyped.keys(relationFields);
37+
const selectRelationFields = ObjectTyped.keys(fields).filter( (key) => key !== 'target');
3938
const fieldsErrors = selectRelationFields
4039
.filter((i) => !includeSet.has(i.toString()))
4140
.map<ValidateQueryError>((i) => ({
@@ -48,8 +47,7 @@ export class QueryFiledInIncludePipe<E extends object>
4847
}
4948

5049
if (sort) {
51-
const { target: targetResourceSorts, ...relationSorts } = sort;
52-
const selectRelationFields = ObjectTyped.keys(relationSorts);
50+
const selectRelationFields = ObjectTyped.keys(sort).filter( (key) => key !== 'target');
5351
const fieldsErrors = selectRelationFields
5452
.filter((i) => !includeSet.has(i.toString()))
5553
.map<ValidateQueryError>((i) => ({

0 commit comments

Comments
 (0)