1.0.4 • Published 6 years ago

joi-extension-enums v1.0.4

Weekly downloads
52
License
MIT
Repository
github
Last release
6 years ago

joi-enum-extension

A Joi extension that allows for Enum validation.

The enum() type extends on the Joi.any() type. The enum type takes an object:

const BaseJoi = require('joi');
const EnumExtension = require('joi-extension-enums');
const Joi = BaseJoi.extend(EnumExtension);

const schema = Joi.any().enum({ ONE: 1, TWO: 2 });

schema.validate('TWO'); // value: 2
schema.validate('THREE'); // ValidationError: "value" must be one of [ONE, TWO]

You can also allow some extra allowed values:

const schema = Joi.any().allow(null, 4).enum({ ONE: 1, TWO: 2 });

schema.validate(null); // value: null
schema.validate(5); // ValidationError: "value" must be one of [ONE, TWO, 4]

Note that both the Enum key as value are considered valid:

const schema = Joi.any().enum({ ONE: 1, TWO: 2 });

schema.validate('ONE'); // value: 1
schema.validate(1); // value: 1

For more examples, have a peek at the tests.

Installing

npm install joi-extension-enums

Running the tests

npm install && npm test

Authors

License

This project is licensed under the MIT License - see the LICENSE file for details

Acknowledgements

Inspired by joi-enums-extension by WesTyler

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago