Skip to content

Commit 7be34c3

Browse files
committed
Update identities.validation.service.ts
1 parent c117e24 commit 7be34c3

File tree

1 file changed

+60
-11
lines changed

1 file changed

+60
-11
lines changed

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

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,59 @@ export class IdentitiesValidationService implements OnApplicationBootstrap {
8181
const attributes = data.attributes || {};
8282
const attributesKeys = Object.keys(attributes);
8383
const validations = {};
84+
//test si il y a les attributs sans attributes
85+
await this.checkAndCreateObjectClasses(data);
8486
for (const key of attributesKeys) {
8587
await this.transformAttribute(key, attributes[key], attributes);
8688
}
8789
return data
8890
}
8991

92+
/**
93+
* check objectclasses and add missing keys
94+
* @param data
95+
*/
96+
public async checkAndCreateObjectClasses(data){
97+
const objectClasses = data.objectClasses || [];
98+
const attributes = data.attributes || {};
99+
const attributesKeys = Object.keys(attributes);
100+
for(const objectclass of objectClasses) {
101+
if (! attributesKeys.includes(objectclass)) {
102+
this.logger.log( objectclass + " attribute not found creating");
103+
await this.createAttributes(objectclass,data);
104+
}
105+
}
106+
}
107+
private async createAttributes(key:string,data:any){
108+
109+
const path = this.resolveConfigPath(key);
110+
if (path === null){
111+
this.logger.error('schema for ' + key + ' does not exist');
112+
throw new BadRequestException('schema for ' + key + ' does not exist');
113+
}
114+
const schema: any = parse(readFileSync(path, 'utf8'));
115+
//creation de la clé
116+
data.attributes[key] = {}
117+
for (const [index, def] of Object.entries(schema?.properties || {})) {
118+
switch ((def as any).type) {
119+
case 'array':
120+
data.attributes[key][index] = [];
121+
break;
122+
123+
case 'object':
124+
data.attributes[key][index] = {};
125+
break;
126+
127+
case 'number':
128+
data.attributes[key][index] = 0;
129+
break;
130+
131+
default:
132+
data.attributes[key][index] = '';
133+
break;
134+
}
135+
}
136+
}
90137
/**
91138
* Transform data following schema validation
92139
* @param key
@@ -206,24 +253,26 @@ export class IdentitiesValidationService implements OnApplicationBootstrap {
206253
// Check for missing schema files
207254
const path = this.resolveConfigPath(key);
208255
if (!existsSync(path)) {
209-
validations[key] = `Fichier de config '${key}.yml' introuvable`;
210-
reject = true;
211-
continue;
256+
validations['message'] = `Fichier de config '${key}.yml' introuvable`;
257+
throw new ValidationConfigException(validations);
212258
}
213259

214260
// Check for invalid schema
215261
const schema: ConfigObjectSchemaDTO = parse(readFileSync(path, 'utf8'));
216262
if (!this.validateSchema(schema)) {
217-
validations[key] = `Schema ${key}.yml invalide: ${this.ajv.errorsText(this.validateSchema.errors)}`;
218-
reject = true;
219-
continue;
263+
validations['message'] = `Schema ${key}.yml invalide: ${this.ajv.errorsText(this.validateSchema.errors)}`;
264+
throw new ValidationConfigException(validations);
265+
}
266+
//verification des required, il faut que l'entree soit presente dans les proprietes
267+
if (schema.hasOwnProperty('required')) {
268+
for (const required of schema['required']){
269+
if (! schema['properties'].hasOwnProperty(required)){
270+
validations['message'] = `Schema ${key}.yml invalide : required : ${required} without property`;
271+
throw new ValidationConfigException(validations);
272+
}
273+
}
220274
}
221275
}
222-
223-
if (reject) {
224-
throw new ValidationConfigException({validations});
225-
}
226-
227276
// Validate each attribute
228277
for (const key of attributesKeys) {
229278
const validationError = await this.validateAttribute(key, attributes[key], attributes);

0 commit comments

Comments
 (0)