question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add option to un-flat the error object

See original GitHub issue

Feature request:

Add option to allow to return an un-flat error object by adding an options argument for Validator.

/**
 * Default options
 * {
 *   flat: Boolean (Default: true)
 * }
 */
const validation = new Validator(data, rules, {flat: false});
const errors = validation.errors.all();
// > {foo: {bar: 1}}

const validation = new Validator(data, rules);
const errors = validation.errors.all();
// > {'foo.bar': 1}

Personally I would find this option useful when using with redux-form where the validation function must return an nested object structure as received.

Additionally the options argument could be extended with more features e.g. instance only rules (proposed #98 )

Could be easily added using flat.unflatten, rather then implementing your own unflatten method.

I can implement the changes if you agree with my proposal.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

3reactions
MikeSuitercommented, Dec 13, 2017

+1 for redux-form nested errors support.

This library will do the trick but having one less dependency would be nice.

unflatten(validator.errors.all())

https://github.com/hughsk/flat

2reactions
iamdtangcommented, Jul 4, 2016

How about we make options the 4th argument to ensure backwards compatibility. If options needs to be passed, the invocation would look like this:

const validation = new Validator(data, rules, {}, { flat: false });

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add properties to JavaScript Error object? - Stack Overflow
In my specific code, I'd like to tag an Error object with my own tag name for further use, which means -> adding...
Read more >
Error - JavaScript - MDN Web Docs - Mozilla
Error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions.
Read more >
ActiveModel::Errors - Rails API
Active Model Errors Provides error related functionalities you can include in your object for handling error messages and interacting with Action View ...
Read more >
flat array to tree - macellerialucio.it
If the goal is to just create a flat array of all the nodes' item values, this may be better: const traverse =...
Read more >
A mostly complete guide to error handling in JavaScript.
stack : a stack trace of functions execution. For example, if we create a new TypeError object with the appropriate message, the message...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found