Skip to content

Commit 2c96fe1

Browse files
authored
Merge pull request #62 from klerick/fix-enum-schema-and-class-validator
fix(json-api-nestjs): fix enum and class validator
2 parents a61b9ad + d188a0c commit 2c96fe1

File tree

5 files changed

+36
-10
lines changed

5 files changed

+36
-10
lines changed

libs/json-api-nestjs/src/lib/factory/ajv/ajv.factory.spec.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import AjvCall from 'ajv';
88
import { AjvCallFactory } from './ajv.factory';
99
import { GLOBAL_MODULE_OPTIONS_TOKEN } from '../../constants';
1010
import { ModuleOptions, QueryParams } from '../../types';
11-
import { mockDBTestModule, entities, Users } from '../../mock-utils';
11+
import { mockDBTestModule, entities, Users, Comments } from '../../mock-utils';
1212

1313
describe('AJV factory', () => {
1414
let options: ModuleOptions;
@@ -264,4 +264,20 @@ describe('AJV factory', () => {
264264
propsColumns
265265
);
266266
});
267+
268+
it('Should be correct dchema for enum field', () => {
269+
const transformQuerySchema = ajvCall.getSchema<QueryParams<Comments>>(
270+
`inputBodyPostSchema-${Comments.name}`
271+
);
272+
const {enum: enumData} = dataSource.getRepository(Comments).metadata.columns.find(i => i.propertyName === 'kind');
273+
const dataProperty =
274+
transformQuerySchema.schema['properties']['data']['properties']['attributes']['properties'];
275+
expect(dataProperty).toHaveProperty('kind');
276+
expect(dataProperty['kind']).toHaveProperty('type');
277+
expect(dataProperty['kind']).toHaveProperty('enum');
278+
expect(Array.isArray(dataProperty['kind']['enum'])).toEqual(true);
279+
expect(dataProperty['kind']['enum']).toEqual(enumData);
280+
281+
282+
})
267283
});

libs/json-api-nestjs/src/lib/factory/ajv/ajv.factory.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function AjvCallFactory(
2525
const AjvCallInst = new AjvCall({ allErrors: true });
2626

2727
for (const entity of options.entities) {
28-
const arrayProps: { [key: string]: boolean } = {};
28+
const arrayProps: { [key: string]: boolean | any} = {};
2929
const uuidProps: { [key: string]: boolean } = {};
3030
const relationArrayProps: { [key: string]: { [key: string]: boolean } } =
3131
{};
@@ -38,6 +38,21 @@ export function AjvCallFactory(
3838
.filter((i) => !relations.includes(i.propertyName))
3939
.map((i) => {
4040
arrayProps[i.propertyName] = i.isArray;
41+
if (i.isArray) {
42+
arrayProps[i.propertyName] = {
43+
type: 'array',
44+
items: {
45+
type: 'string',
46+
},
47+
};
48+
}
49+
if (i.type === 'enum') {
50+
arrayProps[i.propertyName] = {
51+
type: 'string',
52+
enum: i.enum
53+
};
54+
}
55+
4156
uuidProps[i.propertyName] = i.generationStrategy === 'uuid';
4257
return i.propertyName;
4358
});

libs/json-api-nestjs/src/lib/factory/ajv/utils/input-body-post-schema.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,7 @@ export function inputBodyPostSchema(
8888
type: 'string',
8989
};
9090
}
91-
acum[item] = arrayPropsConfig.arrayProps[item]
92-
? {
93-
type: 'array',
94-
items: {
95-
type: 'string',
96-
},
97-
}
98-
: dataType;
91+
acum[item] = arrayPropsConfig.arrayProps[item] === false ? dataType : arrayPropsConfig.arrayProps[item];
9992
return acum;
10093
}, {});
10194

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

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export class BodyInputPatchPipe<Entity> implements PipeTransform {
8080

8181
const validationErrors = await classValidate(temporaryEntity, {
8282
skipUndefinedProperties: true,
83+
forbidUnknownValues: false
8384
});
8485

8586
if (validationErrors.length > 0) {

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

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export class BodyInputPostPipe<Entity> implements PipeTransform {
7979

8080
const validationErrors = await classValidate(temporaryEntity, {
8181
skipUndefinedProperties: false,
82+
forbidUnknownValues: false
8283
});
8384

8485
if (validationErrors.length > 0) {

0 commit comments

Comments
 (0)