This is a webpack loader for ajv-pack that converts a JSON schema into JavaScript validation function.
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
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 aftervalidate(...)
invocation.
console.log(validate.errors);