Skip to content

Commit

Permalink
feat: improve error reporting for fields inside arrays
Browse files Browse the repository at this point in the history
Closes: #5
  • Loading branch information
thetutlage committed Mar 28, 2024
1 parent 8ff246f commit 3d59dad
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
15 changes: 13 additions & 2 deletions src/messages_provider/simple_messages_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class SimpleMessagesProvider implements MessagesProviderContact {
/**
* 1st priority is given to the field messages
*/
const fieldMessage = this.#messages[`${field.wildCardPath}.${rule}`]
const fieldMessage = this.#messages[`${field.getFieldPath()}.${rule}`]
if (fieldMessage) {
return this.#interpolate(fieldMessage, {
field: fieldName,
Expand All @@ -69,7 +69,18 @@ export class SimpleMessagesProvider implements MessagesProviderContact {
}

/**
* 2nd priority is for rule messages
* 2nd priority is for the wildcard path messages
*/
const wildcardMessage = this.#messages[`${field.wildCardPath}.${rule}`]
if (wildcardMessage) {
return this.#interpolate(wildcardMessage, {
field: fieldName,
...args,
})
}

/**
* 3rd priority is for rule messages
*/
const ruleMessage = this.#messages[rule]
if (ruleMessage) {
Expand Down
2 changes: 1 addition & 1 deletion src/reporters/simple_error_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class SimpleErrorReporter implements ErrorReporterContract {
const error: SimpleError = {
message,
rule,
field: field.wildCardPath,
field: field.getFieldPath(),
}

if (meta) {
Expand Down
12 changes: 6 additions & 6 deletions tests/integration/schema/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ test.group('Array | array of numbers', () => {

await assert.validationErrors(vine.validate({ schema, data }), [
{
field: 'categories.*',
field: 'categories.1',
index: 1,
message: 'The 1 field must be a number',
rule: 'number',
},
{
field: 'categories.*',
field: 'categories.2',
index: 2,
message: 'The 2 field must be a number',
rule: 'number',
Expand Down Expand Up @@ -68,7 +68,7 @@ test.group('Array | array of objects', () => {

await assert.validationErrors(vine.validate({ schema, data }), [
{
field: 'contacts.*',
field: 'contacts.0',
index: 0,
message: 'The 0 field must be an object',
rule: 'object',
Expand Down Expand Up @@ -142,7 +142,7 @@ test.group('Array | array of unions', () => {

await assert.validationErrors(vine.validate({ schema, data }), [
{
field: 'contacts.*',
field: 'contacts.0',
index: 0,
message: 'Invalid value provided for 0 field',
rule: 'union',
Expand Down Expand Up @@ -202,7 +202,7 @@ test.group('Array | array of unions', () => {

await assert.validationErrors(vine.validate({ schema, data }), [
{
field: 'contacts.*',
field: 'contacts.0',
index: 0,
message: 'Invalid contact. Either provide an email or a phone number',
rule: 'unknown_contact_type',
Expand Down Expand Up @@ -267,7 +267,7 @@ test.group('Array | array of unions', () => {

await assert.validationErrors(vine.validate({ schema, data }), [
{
field: 'contacts.*.email',
field: 'contacts.0.email',
message: 'The email field must be a valid email address',
rule: 'email',
},
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/simple_error_reporter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@ test.group('Simple error reporter', () => {
isArrayMember: true,
name: 0,
wildCardPath: 'scores.*',
getFieldPath() {
return `scores.0`
},
parent: [],
},
})

assert.isTrue(reporter.hasErrors)
assert.deepEqual(reporter.errors, [
{
field: 'scores.*',
field: 'scores.0',
message: 'Scores are required',
index: 0,
rule: 'required',
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/simple_messages_provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ test.group('Simple messages provider | resolving messages', () => {
)
})

test('give priority to wildcard path message', ({ assert }) => {
const provider = new SimpleMessagesProvider(
{
'required': 'The field is required',
'users.*.username.required': 'Username is required',
},
{}
)

const ctx = fieldContext.create('username', undefined)
ctx.wildCardPath = 'users.*.username'
assert.equal(provider.getMessage('Enter value', 'required', ctx), 'Username is required')
})

test('use default message when no message is provided', ({ assert }) => {
const provider = new SimpleMessagesProvider(
{
Expand Down

0 comments on commit 3d59dad

Please sign in to comment.