imbrn / v8n

☑️ JavaScript fluent validation library

Home Page:https://imbrn.github.io/v8n

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

.every and testAll shows a wrong cause of error

ShaharMynd opened this issue · comments

The Problem
When you testAll some data (array), it shows that it failed because TypeError: split(...).every is not a function
image

The expected behavior
The cause should be because that one of models was not good according to the schema.

Code:

var v8n = require('v8n');

const goodData = [{
    a: 'adasda'
}, {
    a: 'badassdd'
}];

const badData = [{
    a: 'adasda'
}, {
    a: 123
}];

const sampleSchema = {a: v8n().string()}

var badDataValidationError = v8n().every.schema(sampleSchema).testAll(badData);

console.log(badDataValidationError);

var goodDataValidationError = v8n().every.schema(sampleSchema).testAll(goodData);

console.log(goodDataValidationError);

Hey @ShaharMynd, thank you for your report. Sorry for the delay. I'll take a look at this as soon as possible.

Actually, as you can see in the documentation about ValidationError object (https://imbrn.github.io/v8n/api/#validationerror), the cause property is related to the exception that was thrown during the validation process itself. So it's more related to debugging purposes.

The ValidationError object contains a rule property that is specific to what Rule have failed in the validation:

const badData = [
	{ a: 'adasda' },
	{ a: 123 }
];

const sampleSchema = {
	a: v8n().string()
}

var result = v8n()
	.every.schema(sampleSchema)
	.testAll(badData);

console.log(result[0].rule);
/*
{
  name: "schema",
  modifiers: [{ name: "every", ... }] },
  args: [{ a: ... }]
  ..
}
*/

See that it also has some other useful properties that you can use, as modifiers, and args, which contain detailed information about what happened to the validation.