Validaciones para los endpints

This commit is contained in:
2026-02-10 15:57:03 +01:00
parent 70ef1131df
commit a39b84e107
13 changed files with 175 additions and 48 deletions

View File

@@ -0,0 +1,25 @@
export type Validator<T extends Object> = {
field: keyof T,
errorMsg: string,
validationFunc: (obj: T) => boolean
}
/**
* Ejecuta una lista de validadores en orden, si alguno
* falla devuelve un Error
*/
export class BodyValidator<T extends Object> {
validatorList: Validator<T>[] = []
constructor(
validators: Validator<T>[]
) {
this.validatorList = validators
}
public validate(obj: T) {
for (const validator of this.validatorList) {
if (validator.validationFunc(obj) == false) throw new Error(validator.errorMsg)
}
return true;
}
}