Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/core/util/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,18 @@ function assertProp (
}
const validator = prop.validator
if (validator) {
if (!validator(value)) {
let result
try {
result = validator(value)
} catch (error) {
result = error
}

if (result !== true) {
const message = result instanceof Error ? result.message.replace(/{{\s*name\s*}}/g, name) : 'custom validator check failed for prop "' + name + '".'

warn(
'Invalid prop: custom validator check failed for prop "' + name + '".',
'Invalid prop: ' + message ,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about

const message = result instanceof Error ? result.message : 'custom validator check failed';
warn(`Invalid prop ${name}: ${message}`);

without introducing the interpolation thing?

vm
)
}
Expand Down
12 changes: 12 additions & 0 deletions test/unit/features/options/props.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,18 @@ describe('Options props', () => {
makeInstance(123, null, v => v === 234)
expect('custom validator check failed').toHaveBeenWarned()
})

it('custom validator + custom error', () => {
const validator = v => {
if (v !== 123) throw new Error(`the prop '{{name}}' should be 123.`)

return true
}
makeInstance(123, null, validator)
expect(console.error.calls.count()).toBe(0)
makeInstance(234, null, validator)
expect(`the prop 'test' should be 123.`).toHaveBeenWarned()
})

it('type check + custom validator', () => {
makeInstance(123, Number, v => v === 123)
Expand Down