import { PlataError } from "../tools" import { ModelsInternals } from "./tools" export class ModelTemplateEnv { public readonly template: T public readonly validations: Model.ModelTemplateEnvValidation[] = [] public readonly type: Model.ExtractTemplateEnvType constructor(template: T) { this.template = template } public addValidation(callback: Model.ModelTemplateEnvValidation) { this.validations.push(callback) } public async validate(value: Record): Promise> { let errors = await ModelsInternals.validateTemplateEnv(value, this.template) if (errors.errorID !== undefined) { return { errors: [errors], value: undefined } } if (errors.length !== 0) { return { errors, value: undefined } } const promises: Promise[] = [] for (let i = this.validations.length - 1; i >= 0; i--) { const validation = this.validations[i]; promises.push(Plata.FastPromise(() => validation(value as any))) } errors = (await Promise.all(promises)).filter(Boolean) if (errors.length !== 0) { return { errors, value: undefined } } return { value: value as any, errors: undefined } } }