Package Exports
- fluent-json-validator
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (fluent-json-validator) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Fluent JSON validator
An easy-to-use, expressive, and composable JSON object validator, with a fluent builder pattern interface!
// this is what you want to validate:
// coming from the user, read from a file, sent back by some API, etc.
const person = {
name: 'John Doe',
age: 42,
hobbies: ['eating', 'coding', 'sleeping'],
favoriteNumberOrColor: 'green'
};
// this is the structure you want your data to have:
const personSchema = is.Object({
name: is.String(),
nickname: is.optional().String(),
// ^ nickname may be missing, but if not, it must be a string.
age: is.Number().Where(
age => age > 10
// ^ age must be more than 10.
),
hobbies: is.ArrayOf(
is.String().Where(
hobby => hobby !== 'illegal activities'
// ^ hobbies can't include illegal activities!
)
),
favoriteNumberOrColor: is.OneOf([is.Number(), is.String()])
});
// and this is how you check if it matches or not:
personSchema.validate(person); // == trueFeatures
- Lightweight, since it has no dependencies!
- Has a small and simple API, only a handful of methods.
- Can validate primitive types, arrays (
is.ArrayOf) and even variable types (is.OneOf)! - Can validate any arbitrary JSON object structure, just mix'n'match the needed validators!
- Schemas are reusable and composable for validating complex data structures painlessly!
- Can be used for formal1 and functional1 validation as well! (with
is.Where)
1: I may be using slightly incorrect words, but by formal validation I mean the data has valid structure, and by functional validation I mean the data/value itself satisfies additional arbitrary requirements if needed.
Installation
npm i fluent-json-validatorExamples
I will write some examples here, but in the meantime check out the countless tests for inspiration, especially the complex ones at the end!
Testing
Testcases are listed in tests.js. Run them with
npm testBut why?
Because there is no other JSON object validator with a builder pattern interface like this one. Okay, there is actually Superstruct, but I didn't know about it when I started developing this, and also it is much-much bigger with many features I personally don't need.
So I created this library as a smaller alternative.
License
MIT