Skip to content
This repository has been archived by the owner on Apr 19, 2022. It is now read-only.

Commit

Permalink
feat: add support for custom error collector
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 2, 2019
1 parent 15d8183 commit 9123423
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,6 +22,7 @@ export type ValidatorConfig = {
cacheKey?: string,
existyStrict: boolean,
removeAdditional: boolean,
customErrorCollector?: ErrorCollectorFn,
formatter: { new (): ErrorFormatterContract },
}

Expand Down
10 changes: 8 additions & 2 deletions src/Validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReturnType<ValidatorCompiler['compile']>>()

/**
* 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
*/
Expand Down Expand Up @@ -63,6 +67,7 @@ export const validate: ValidateFn = (data, schema, messages, config?) => {
config,
true,
config.removeAdditional!,
config.customErrorCollector,
)
}

Expand All @@ -83,5 +88,6 @@ export const validateAll: ValidateFn = (data, schema, messages, config?) => {
config,
false,
config.removeAdditional!,
config.customErrorCollector,
)
}
43 changes: 43 additions & 0 deletions test/validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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',
},
])
}
})
})

0 comments on commit 9123423

Please sign in to comment.