Skip to content
This repository has been archived by the owner on Jun 18, 2020. It is now read-only.

corpix/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-pack

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