Skip to content

Exact-Realty/ajv-pack-loader

 
 

Repository files navigation

ajv-pack-loader

Build Status

This is a webpack loader for ajv-pack that converts a JSON schema into JavaScript validation function.

Installation

We depend on two npm packages as peerDependencies, so you should install them first:

npm install ajv ajv-formats

Now you could install the loader:

npm install ajv-pack-loader

Usage example

Add a loader into your webpack config:

{
    loaders: [
        {
            test: /\.?schema.json$/,
            loader: 'ajv-pack-loader'
        }
    ]
}

Import your schema in the project code:

import validate from 'app/external/microformats/schema.json';

Signature of a validate function is validate(data, dataPath, parentData, parentDataProperty, rootData)

Validate your data:

validate(
    {
        user: {
            name: 'John',
            surname: 'Doe'
        }
    }
); // true || false

Access validation errors:

This is a bit strange, but .errors attribute will become available after validate(...) invocation.

console.log(validate.errors);

License

MIT

About

Webpack loader for ajv-pack

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 81.0%
  • Makefile 19.0%