field-validation-error

A simple FieldValidationError constructor

Usage no npm install needed!

<script type="module">
  import fieldValidationError from 'https://cdn.skypack.dev/field-validation-error';
</script>

README

FieldValidationError

When you've messed something up

Dependency Status devDependency Status

An exception for use in certain situations (e.g. to signal that some fields didn't pass validation). Created because of a need for a cross-package standard exception.

Getting started

Install the package with this command:

npm install field-validation-error --save

Usage

// Import ES6-style
import FieldValidationError from 'field-validation-error';

// Or CommonJS-style
const FieldValidationError = require('field-validation-error');

// Then just use like this:
throw new FieldValidationError({ name: 'Name is missing' });

// You can get access to the fields on a thrown exception using the `fields` property:
try {
  ...
} catch (err) {
  if (err instanceof FieldValidationError) {
    console.error('Invalid fields:', err.fields);
  }
}

Contributing

In lieu of a formal style guide, take care to maintain the existing coding style.

License

Copyright (c) 2016 Rafał Ruciński. Licensed under the WTFPL license.