import { IDictionary } from '../model.header'; export interface IValidationError extends Error { modelName: string; errors: { [key in keyof TProps]?: Array }; } // tslint:disable:max-line-length /** * Details about which properties failed to validate in which way. * * The type is an object with property names as keys and then an array with validation * names of the validations that failed * * @type { Object.> } * @name errors * @memberof NohmErrors.ValidationError# */ // tslint:enable:max-line-length /** * Error thrown whenever validation failed during {@link NohmModel#validate} or {@link NohmModel#save}. * * @class ValidationError * @memberof NohmErrors * @extends {Error} */ export class ValidationError extends Error implements IValidationError { public readonly errors: IValidationError['errors']; public readonly modelName: string; constructor( errors: IValidationError['errors'], modelName: string, errorMessage: string = 'Validation failed. See .errors on this Error or the Nohm model instance for details.', ) { super(errorMessage); const emptyErrors: IValidationError['errors'] = {}; this.modelName = modelName; this.errors = Object.keys(errors).reduce< IValidationError['errors'] >((obj, key) => { const error = errors[key]; if (error && error.length > 0) { obj[key] = error; } return obj; }, emptyErrors); } }