diff --git a/src/Contracts.ts b/src/Contracts.ts index 782e406..39223a5 100644 --- a/src/Contracts.ts +++ b/src/Contracts.ts @@ -11,8 +11,9 @@ * file that was distributed with this source code. */ -import { ErrorFormatterContract } from 'indicative-compiler' import { Schema, Messages } from 'indicative-parser' +import { ErrorFormatterContract } from 'indicative-compiler' +import { ErrorCollectorFn } from 'indicative-compiler/build/src/contracts' /** * Shape of validator config @@ -21,6 +22,7 @@ export type ValidatorConfig = { cacheKey?: string, existyStrict: boolean, removeAdditional: boolean, + customErrorCollector?: ErrorCollectorFn, formatter: { new (): ErrorFormatterContract }, } diff --git a/src/Validator/index.ts b/src/Validator/index.ts index 64b9ce4..b3023d9 100644 --- a/src/Validator/index.ts +++ b/src/Validator/index.ts @@ -16,15 +16,19 @@ import { Schema, Messages } from 'indicative-parser' import { ValidatorCompiler, ValidatorExecutor } from 'indicative-compiler' import { CacheManager } from '../CacheManager' -import { ValidateFn, ValidatorConfig } from '../Contracts' import { config as validatorConfig } from './config' +import { ValidateFn, ValidatorConfig } from '../Contracts' const cacheManager = new CacheManager>() /** * Returns executor by pre-compiling and optionally caching schema. */ -function getExecutor (schema: Schema, messages: Messages, config: ValidatorConfig) { +function getExecutor ( + schema: Schema, + messages: Messages, + config: ValidatorConfig, +): ValidatorExecutor { /** * Always compile schema, when there is no cacheKey */ @@ -63,6 +67,7 @@ export const validate: ValidateFn = (data, schema, messages, config?) => { config, true, config.removeAdditional!, + config.customErrorCollector, ) } @@ -83,5 +88,6 @@ export const validateAll: ValidateFn = (data, schema, messages, config?) => { config, false, config.removeAdditional!, + config.customErrorCollector, ) } diff --git a/test/validator.spec.ts b/test/validator.spec.ts index 48f8c7a..2de34b4 100644 --- a/test/validator.spec.ts +++ b/test/validator.spec.ts @@ -29,6 +29,24 @@ test.group('validate', () => { await validate({}, {}, {}, { cacheKey: 'foo' }) await validate({}, { username: 'required' }, {}, { cacheKey: 'foo' }) }) + + test('define custom error collector', async (assert) => { + assert.plan(1) + + try { + await validate({}, { username: 'required', age: 'required' }, {}, { + customErrorCollector: (formatter, _m, field, rule, args) => { + formatter.addError('Validation failed', field, rule, args) + }, + }) + } catch (errors) { + assert.deepEqual(errors, [{ + message: 'Validation failed', + validation: 'required', + field: 'username', + }]) + } + }) }) test.group('validateAll', () => { @@ -57,4 +75,29 @@ test.group('validateAll', () => { await validateAll({}, {}, {}, { cacheKey: 'foo' }) await validateAll({}, { username: 'required' }, {}, { cacheKey: 'foo' }) }) + + test('define custom error collector', async (assert) => { + assert.plan(1) + + try { + await validateAll({}, { username: 'required', age: 'required' }, {}, { + customErrorCollector: (formatter, _m, field, rule, args) => { + formatter.addError('Validation failed', field, rule, args) + }, + }) + } catch (errors) { + assert.deepEqual(errors, [ + { + message: 'Validation failed', + validation: 'required', + field: 'username', + }, + { + message: 'Validation failed', + validation: 'required', + field: 'age', + }, + ]) + } + }) })