Skip to content

Add throw option to throw an error with violations  #61

Description

@ricardogama

Following the concept of having an options argument on the validate method suggested in #60, it would be useful to have a throw option that would enable throwing an error when validation fails, exposing violations on an errors or violations property.

Such feature would facilitate handling validation errors on catch blocks alongside other error, which is a very common practice, for example:

import { Validator, ViolationError } from 'validator.js';

function foo(data) {
  try {
    Validator.validate(data, { bar: is.required() }, { 
      throw: true 
    });

    // ... other code which could throw other custom errors
  } catch (e) {
    if (e instanceof ViolationError) {
      // `e.errors` should contain all raised violations
    }

    // ... handle other errors

    throw e;
  }
}

Enabling this through the options argument will also be a breaking change, so I guess this feature should be discussed with a new major release in mind.

@guillaumepotier @fixe @ruimarinho WDYT?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions