@@ -6,10 +6,12 @@ import { diff } from 'radash';
66import { AdditionalFieldsPart } from '../_schemas/_parts/additionalFields.part.schema' ;
77import Ajv from 'ajv' ;
88import addFormats from 'ajv-formats' ;
9+ import localize from 'ajv-i18n' ;
910import validSchema from './_config/validSchema' ;
1011import ajvErrors from 'ajv-errors' ;
1112import { ValidationConfigException , ValidationSchemaException } from '~/_common/errors/ValidationException' ;
1213import { additionalFieldsPartDto } from '../_dto/_parts/additionalFields.dto' ;
14+ import { ConfigService } from "@nestjs/config" ;
1315
1416/**
1517 * Service responsible for validating identities.
@@ -20,12 +22,13 @@ export class IdentitiesValidationService implements OnApplicationBootstrap {
2022 private validateSchema ;
2123 private logger : Logger ;
2224
23- public constructor ( ) {
25+ public constructor ( protected config : ConfigService ) {
2426 addFormats ( this . ajv ) ;
2527 ajvErrors ( this . ajv ) ;
2628 this . ajv . addFormat ( 'number' , / ^ \d * $ / ) ;
2729 this . validateSchema = this . ajv . compile ( validSchema ) ;
2830 this . logger = new Logger ( IdentitiesValidationService . name ) ;
31+
2932 }
3033
3134 public onApplicationBootstrap ( ) : void {
@@ -57,14 +60,15 @@ export class IdentitiesValidationService implements OnApplicationBootstrap {
5760 }
5861
5962 private resolveConfigPath ( key : string ) : string | null {
60- const hardConfigPath = `./src/management/identities/validations/_config/${ key } .yml` ;
63+ //lecture deja dans le repertoire /validation pour les schemas non modifiables
64+ const hardConfigPath = `./validation/${ key } .yml` ;
6165 const dynamicConfigPath = `./configs/identities/validations/${ key } .yml` ;
62- if ( existsSync ( dynamicConfigPath ) ) {
63- return dynamicConfigPath ;
64- }
6566 if ( existsSync ( hardConfigPath ) ) {
6667 return hardConfigPath ;
6768 }
69+ if ( existsSync ( dynamicConfigPath ) ) {
70+ return dynamicConfigPath ;
71+ }
6872 return null ;
6973 }
7074
@@ -309,7 +313,23 @@ export class IdentitiesValidationService implements OnApplicationBootstrap {
309313 throw new BadRequestException ( 'schema for ' + key + ' does not exist' ) ;
310314 }
311315 const schema : any = parse ( readFileSync ( path , 'utf8' ) ) ;
312-
316+ // mise de min length et minItems dans les champs requis
317+ if ( schema . hasOwnProperty ( 'required' ) ) {
318+ for ( const required of schema [ 'required' ] ) {
319+ switch ( schema [ 'properties' ] [ required ] [ 'type' ] ) {
320+ case 'array' :
321+ if ( ! schema [ 'properties' ] [ required ] [ 'minItems' ] ) {
322+ schema [ 'properties' ] [ required ] [ 'minItems' ] = 1
323+ }
324+ break ;
325+ case 'string' :
326+ if ( ! schema [ 'properties' ] [ required ] [ 'minLength' ] ) {
327+ schema [ 'properties' ] [ required ] [ 'minLength' ] = 1
328+ }
329+ break ;
330+ }
331+ }
332+ }
313333 for ( const [ index , def ] of Object . entries ( schema ?. properties || { } ) ) {
314334 if ( typeof data [ key ] [ index ] === 'undefined' || data [ key ] [ index ] === null ) {
315335 switch ( ( def as any ) . type ) {
@@ -342,6 +362,7 @@ export class IdentitiesValidationService implements OnApplicationBootstrap {
342362 const ok = await this . ajv . validate ( schema , data [ key ] ) ;
343363 if ( ok === false ) {
344364 const retErrors = { } ;
365+ await this . translateAjv ( this . ajv . errors )
345366 for ( const err of this . ajv . errors ) {
346367 retErrors [ err [ 'instancePath' ] . substring ( 1 ) ] = err [ 'instancePath' ] . substring ( 1 ) + ' ' + err [ 'message' ]
347368 }
@@ -409,9 +430,9 @@ export class IdentitiesValidationService implements OnApplicationBootstrap {
409430 this . logger . debug ( [ 'findOne' , JSON . stringify ( Object . values ( arguments ) ) ] . join ( ' ' ) ) ;
410431 let filePath = '' ;
411432 if ( schema === 'inetorgperson' ) {
412- filePath = './validation/inetorgperson.json ' ;
433+ filePath = './validation/inetorgperson.yml ' ;
413434 if ( ! existsSync ( filePath ) ) {
414- const message = `File not found /validation/inetorgperson.json ` ;
435+ const message = `File not found /validation/inetorgperson.yml ` ;
415436 throw new ValidationConfigException ( { message } ) ;
416437 }
417438 } else {
@@ -423,4 +444,15 @@ export class IdentitiesValidationService implements OnApplicationBootstrap {
423444 }
424445 return parse ( readFileSync ( filePath , 'utf-8' ) ) ;
425446 }
447+ private async translateAjv ( messages ) {
448+ switch ( this . config . get ( 'application.lang' ) ) {
449+ case 'en' :
450+ break
451+ case 'fr' :
452+ case 'fr_FR.UTF-8' :
453+ case 'fr_FR' :
454+ localize . fr ( messages )
455+ break
456+ }
457+ }
426458}
0 commit comments