2.0.5 • Published 3 years ago

js-object-schema v2.0.5

Weekly downloads
2
License
MIT
Repository
github
Last release
3 years ago

js-object-schema

A very small, flexible, easy to use, javascript object validation tool.

Install

$ npm install js-object-schema

Usage

// Import  
import JsObjectSchema from 'js-object-schema';
// Or: const JsObjectSchema = require('js-object-schema');

// Create schema
const schema = new JsObjectSchema('Pokemon', {
    name: ({ name }) => typeof name === 'string', 
    maxLevel: ({ maxLevel }) => typeof maxLevel === 'number' && maxLevel > 0,
    level: ({ level, maxLevel }) => typeof level === 'number' && level <= maxLevel
})

const pokemon = { name: 'Pikachu', level: 99, maxLevel: 99};

const result = schema.validate(object);

// Check result
if (result.error) {
    // Do somthing with the errors if any
}

Api Reference

JsObjectSchema(name, schema, options)

ArgumentDescriptionDefault value
name: StringThe name of the schema. Providing name will give more accurate error message'Object'
schema: SchemaSee Schema reference.{}
options: OptionsSee Options reference{parseObject: false, rootObjectValidation: null }

JsObjectSchema.validate(object)

Call this function to validate an object

ArgumentDescriptionDefault value
object: ObjectThe object that should be validatedundefined
Return ValueDescriptionExample
resultAn object containing the validated object and error{ object: Object, error: null }
result.objectThe validated and optionally parsed object.{ key: 'value' }
result.errorThe error object. When no errors, this is null.{ message: 'Schema validation error', errors: [] }
result.error.errorsThe validation errors for each prop.[ new Error("'key' is invalid") ]

Example:

const JsObjectSchema = require('js-object-schema');
const schema = new JsObjectSchema('Pokemon', {
    name: ({ name }) => typeof name === 'string', 
    maxLevel: ({ maxLevel }) => typeof maxLevel === 'number' && maxLevel > 0,
    level: ({ level, maxLevel }) => typeof level === 'number' && level <= maxLevel
}, { parseObject: true });

const pokemon = { name: 'Pikachu', level: 99, maxLevel: 99 };
const { error, object } = schema.validate(object);

Schema

The schema should be an object that reflects the object it should validate. The schema property can be a function, array or JsObjectSchema.

Basic Schema

A schema property function is called with the whole object as argument. If it returns true, the property is interpreted as valid:

const pokemonSchema = new JsObjectSchema('Pokemon', {
    name: ({ name }) => typeof name === 'string', // name must be a string 
    maxLevel: ({ maxLevel }) => typeof maxLevel === 'number' && maxLevel > 0, // maxLevel must be a number grater than 0
    level: ({ level, maxLevel }) => typeof level === 'number' && level <= maxLevel // level must be a number lesser than or equal to maxLevel
});

Nested Schemas

The schema can be nested! Simply create an object within the schema object:

const trainerSchema = new JsObjectSchema('Pokemon', {
    name: ({ name }) => typeof name === 'string',
    badges: ({ badges }) => Array.isArray(badges),
    pokemon: {
        name: ({ name }) => typeof name === 'string',
        maxLevel: ({ maxLevel }) => typeof maxLevel === 'number' && maxLevel > 0,
        level: ({ level, maxLevel }) => typeof level === 'number' && level <= maxLevel
    },
});

Or create a schema and map it to the prop:

const pokemonSchema = new JsObjectSchema('Pokemon', { /* ..props */ });

const trainerSchema = new JsObjectSchema('Pokemon', {
    // ...props
    pokemon: pokemonSchema,
});

If the property is an array, simply put the validation function, object or schema inside an array. Each item in the array will be validated:

const trainerSchema = new JsObjectSchema('Pokemon', {
    pokemon: [ pokemonSchema ],
    bag: [ { /*.. props */ } ],
    badges: [ (badge) => badge !== null ]
});

Options

PropDescriptionDefault value
parseObject: BooleanWhen true, the object will pe parsed to match the schema. Not inherited by nested schemas.false
rootObjectValidation: functionA function that is invoked with the root object. When returning true, object is interpreted as valid.null

License

MIT

Enjoy!

2.0.5

3 years ago

2.0.3

4 years ago

2.0.4

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago