Skip to content

Commit bda6a74

Browse files
authored
Merge pull request #177 from dwightjack/fix/custom-in-shape
Fix: force execution of custom validator function in validateType
2 parents a2b1197 + f0faa5f commit bda6a74

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

__tests__/utils.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ describe('`validateType()`', () => {
122122
utils.validateType({ type: String, required: true }, undefined),
123123
).toBe(false)
124124
})
125+
126+
it('should validate validators with no type or type === true', () => {
127+
expect(utils.validateType({}, 'anyValue')).toBe(true)
128+
expect(utils.validateType({ type: true }, 'anyValue')).toBe(true)
129+
})
130+
131+
it('should execute the validator for type === null', () => {
132+
const validator = jest.fn().mockReturnValue(false)
133+
expect(utils.validateType({ type: null, validator }, 'anyValue')).toBe(
134+
false,
135+
)
136+
expect(validator).toHaveBeenLastCalledWith('anyValue')
137+
})
125138
})
126139

127140
describe('`toType()`', () => {

__tests__/validators/custom.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import custom from '../../src/validators/custom'
2+
import { validateType } from '../../src/utils'
23
import { VueTypeDef } from '../../src/types'
34
import { forceNoContext, checkRequired } from '../helpers'
45

@@ -12,6 +13,7 @@ describe('`.custom`', () => {
1213
it('should match an object with a validator method', () => {
1314
expect(customType).toEqual(
1415
expect.objectContaining({
16+
type: null,
1517
validator: expect.any(Function),
1618
}),
1719
)
@@ -30,4 +32,11 @@ describe('`.custom`', () => {
3032
expect(validator('mytest')).toBe(true)
3133
expect(validator(0)).toBe(false)
3234
})
35+
36+
it('should trigger the validator in validateType', () => {
37+
const spy = jest.fn().mockReturnValue(true)
38+
const type = custom(spy)
39+
expect(validateType(type, '__TEST__')).toBe(true)
40+
expect(spy).toHaveBeenCalledWith('__TEST__')
41+
})
3342
})

src/validators/custom.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { toType, warn } from '../utils'
2-
import { ValidatorFunction, VueTypeDef } from '../types'
2+
import { ValidatorFunction, VueTypeDef, PropType } from '../types'
33

44
export default function custom<T>(
55
validatorFn: ValidatorFunction<T>,
@@ -12,6 +12,7 @@ export default function custom<T>(
1212
}
1313

1414
return toType<T>(validatorFn.name || '<<anonymous function>>', {
15+
type: null as unknown as PropType<T>,
1516
validator(this: VueTypeDef<T>, value: T) {
1617
const valid = validatorFn(value)
1718
if (!valid) warn(`${this._vueTypes_name} - ${warnMsg}`)

0 commit comments

Comments
 (0)