import { ValidationError } from "yup"; import { Validator } from "./Validator"; import { ResolverInfo } from "."; import { Schema, ValidateOptions, AnyObject } from "yup"; /** * YupValidator * * @export * @template TInput * @template TSource * @template TContext * @template TArgs * @param {( * info: ResolverInfo * ) => Promise} getSchema * @param {ValidateOptions} [options] * @return {*} {Validator} */ export function YupValidator( getSchema: (info: ResolverInfo) => Promise, options?: ValidateOptions ): Validator { return async (input, info) => { try { // validate the input using the schema const schema = await getSchema(info); console.log("-------------------------------------"); await schema.validate(input, options); } catch (validationResult: any) { const validationError = validationResult as ValidationError; const result: { [key: string]: string } = {}; // on abortEarly = false, we get an array of errors if (validationError.inner.length > 0) { validationError.inner.forEach((error) => { const key = error.path as string; result[key] = error.message; }); } else { // on abortEarly = true (default), we get a single error const key = validationError.path as string; result[key] = validationResult.message; } return result; } return {}; }; }