import { ValidationSchemas } from '~/domain'; import { JoiSchema } from '~/infra/joi/helper/joi-types'; import { IJoiRuleParser } from '~/infra/joi/protocols/rule-parsers/parser'; import { extractRulesKeys } from '~/infra/joi/validators/use-cases/joi-common-validator/get-joi-common-schema/apply-rules/extract-rules-keys'; export const applyRules = ( type: T, initial: JoiSchema, rules: ValidationSchemas.Rules, rulesParser: IJoiRuleParser, JoiSchema> ): JoiSchema => { const ruleKeys = extractRulesKeys(rules); return ruleKeys.reduce((schema, ruleKey) => { const rule = rules[ruleKey]; const parser: any = rulesParser[ruleKey]; if (!parser) { throw new Error(`JoiAdapter - Unsupported rule for (type = ${type}) [ ${ruleKey} ]`); } return parser(schema, rule, rules); }, initial); };