import { ObjectSchema } from 'joi'; import { ValidationSchemas } from '~/domain'; import { Joi, JoiSchema } from '~/infra/joi/helper/joi-types'; import { IGetSchema, ObjectParser } from '~/infra/joi/protocols'; import { IParseNestedSchemas } from '~/infra/joi/protocols/parse-nested-schemas'; import { GetJoiCommonSchema } from '~/infra/joi/validators/use-cases/joi-common-validator/get-joi-common-schema'; type Type = 'object'; export class GetJoiObjectSchema extends GetJoiCommonSchema implements IGetSchema<'object'> { public type: Type = 'object'; constructor(joi: Joi, rulesParser: ObjectParser, private readonly shapeParser: IParseNestedSchemas) { super(joi, rulesParser); } getSchema = (rules: ValidationSchemas.Rules): JoiSchema => { const initial = this.applyAfterOnSchema(this.getInitial(rules)); return this.getInitialSchema(initial, rules); }; private getInitial = (rules: ValidationSchemas.Rules): JoiSchema => { if (rules.shape) { const joiRules: any = this.shapeParser.parseRules(rules.shape); return this.joi.object(joiRules); } return this.joi.object(); }; private applyAfterOnSchema = (schema: ObjectSchema): ObjectSchema => { return schema.strict().options({ abortEarly: false, allowUnknown: true }); }; }