Skip to content

Commit 625bfbe

Browse files
committed
update with ajv
1 parent f40591e commit 625bfbe

File tree

5 files changed

+22
-18
lines changed

5 files changed

+22
-18
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"@the-software-compagny/nestjs_module_restools": "^0.0.11",
6464
"ajv": "^8.16.0",
6565
"ajv-errors": "^3.0.0",
66+
"ajv-formats": "^3.0.1",
6667
"argon2": "^0.41.1",
6768
"awesome-phonenumber": "^6.10.0",
6869
"bullmq": "^5.8.2",

src/management/identities/validations/_config/validSchema.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ const validSchema = {
2828
},
2929
format: {
3030
type: 'string',
31-
enum: ['date', 'email'],
32-
errorMessage: "Le 'format' doit être 'date' ou 'email'.",
31+
enum: ['date', 'email','number','time','date-time','duration','uri','uri-reference','hostname','ipv4','ipv6','regex','uuid'],
32+
errorMessage: "Format invalide",
3333
},
3434
items: {
3535
type: 'object',
@@ -118,7 +118,7 @@ const validSchema = {
118118
camelCase: {
119119
type: 'boolean',
120120
errorMessage: "L'option 'camelCase' doit être un booléen.",
121-
},
121+
}
122122
},
123123
required: ['type'],
124124
additionalProperties: false,

src/management/identities/validations/_dto/config.dto.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IsString, ArrayNotEmpty, ValidateNested, IsEnum, IsBoolean } from 'class-validator';
1+
import {IsString, ArrayNotEmpty, ValidateNested, IsEnum, IsBoolean, IsOptional} from 'class-validator';
22
import { Type } from 'class-transformer';
33
import { ConfigObjectType } from '../_enums/config.object';
44

@@ -15,6 +15,10 @@ export class ConfigObjectAttributeDTO {
1515

1616
@IsBoolean()
1717
public required: boolean;
18+
19+
@IsString()
20+
@IsOptional()
21+
public format: string;
1822
}
1923

2024
export class ConfigObjectObjectClassDTO {

src/management/identities/validations/identities.validation.service.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ import {ConfigObjectSchemaDTO} from './_dto/config.dto';
55
import {diff} from 'radash';
66
import {AdditionalFieldsPart} from '../_schemas/_parts/additionalFields.part.schema';
77
import Ajv from 'ajv';
8-
import {buildYup} from 'schema-to-yup';
8+
import addFormats from 'ajv-formats';
99
import validSchema from './_config/validSchema';
1010
import ajvErrors from 'ajv-errors';
1111
import {ValidationConfigException, ValidationSchemaException} from '~/_common/errors/ValidationException';
1212
import {additionalFieldsPartDto} from '../_dto/_parts/additionalFields.dto';
1313

14-
1514
/**
1615
* Service responsible for validating identities.
1716
*/
@@ -22,7 +21,9 @@ export class IdentitiesValidationService implements OnApplicationBootstrap {
2221
private logger: Logger;
2322

2423
public constructor() {
24+
addFormats(this.ajv);
2525
ajvErrors(this.ajv);
26+
this.ajv.addFormat('number',/^\d*$/);
2627
this.validateSchema = this.ajv.compile(validSchema);
2728
this.logger = new Logger(IdentitiesValidationService.name);
2829
}
@@ -242,7 +243,7 @@ export class IdentitiesValidationService implements OnApplicationBootstrap {
242243
* @param attribute - The attribute value to validate.
243244
* @returns A promise that resolves with an error message if validation fails, otherwise null.
244245
*/
245-
public async validateAttribute(key: string, attribute: any, data: any): Promise<string | null> {
246+
public async validateAttribute(key: string, attribute: any, data: any): Promise<any | null> {
246247
const path = this.resolveConfigPath(key);
247248
const schema: any = parse(readFileSync(path, 'utf8'));
248249

@@ -269,17 +270,15 @@ export class IdentitiesValidationService implements OnApplicationBootstrap {
269270
}
270271

271272
this.logger.debug(`Additionalfields object validation: ${JSON.stringify(data[key])}`);
272-
273-
const yupSchema = buildYup(schema, {noSortEdges: true});
274-
try {
275-
await yupSchema.validate(attribute, {strict: true, abortEarly: false});
276-
return null;
277-
} catch (error) {
278-
return error.inner.reduce((acc, err) => {
279-
acc[err.path] = err.message;
280-
return acc;
281-
}, {});
273+
const ok= await this.ajv.validate(schema,data[key]);
274+
if (ok === false) {
275+
const retErrors = {};
276+
for (const err of this.ajv.errors) {
277+
retErrors[err['instancePath'].substring(1)]= err['instancePath'].substring(1) + ' ' + err['message']
278+
}
279+
return(retErrors)
282280
}
281+
return null
283282
}
284283

285284
public async findAll(): Promise<any> {

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4210,7 +4210,7 @@ ajv-formats@2.1.1:
42104210
dependencies:
42114211
ajv "^8.0.0"
42124212

4213-
ajv-formats@3.0.1:
4213+
ajv-formats@3.0.1, ajv-formats@^3.0.1:
42144214
version "3.0.1"
42154215
resolved "https://registry.yarnpkg.com/ajv-formats/-/ajv-formats-3.0.1.tgz#3d5dc762bca17679c3c2ea7e90ad6b7532309578"
42164216
integrity sha512-8iUql50EUR+uUcdRQ3HDqa6EVyo3docL8g5WJ3FNcWmu62IbkGUue/pEyLBW8VGKKucTPgqeks4fIU1DA4yowQ==

0 commit comments

Comments
 (0)