@centralping/json-api-query
v0.1.3
Published
A JSON API compliant query parameter validator.
Downloads
1
Readme
@CentralPing/json-api-query
An extensible JSON Schema for validating and optionally coercing JSON API query parameters for fetching data.
Notes
- The request querystring is expected to have been parsed into an object.
// Example Original URL:
// http://localhost:3000/?include=author&fields%5Barticles%5D=title%2Cbody&fields%5Bpeople%5D=name
// Parsed querystring:
{
include: 'author',
fields: {
articles: ['title', 'body']
},
fields: {
people: 'name'
}
}
- The values of the query object can be strings or coerced to expected value types prior to validation. By default the object values will be coerced if strings and validation succeeds.
- Per the JSON API specification, any additional query parameters are ignored for validation and coercion by the validation method.
Installation
npm i --save @centralping/json-api-query
API Reference
jsonApiQuery~schema : Object
Module property that generates a new deep copy of the default schema
on every import. Apply any extensions and provide as an optional schema
for the validate
method.
Kind: inner property of jsonApiQuery
jsonApiQuery~parse ⇒ Object
Kind: inner property of jsonApiQuery
Returns: Object - A url parse object.
| Param | Type | Description | | --- | --- | --- | | url | String | Any URL string. |
Example
const url = '/foo/bar?include=author&fields%5Barticles%5D=title%2Cbody&fields%5Bpeople%5D=name';
const {query, pathname, ...extra} = parse(url);
// query
// {
// include: [ 'author' ],
// fields: {
// articles: ['title', 'body'],
// people: ['name']
// }
// }
// pathname
// '/foo/bar'
jsonApiQuery~validate ⇒ function
Kind: inner property of jsonApiQuery
Returns: function - the configured validator function
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [options] | Object | | any AJV option. |
| [options.coerceTypes] | Boolean | String | 'array' | coerce validated values to specified types. |
| [options.ownProperties] | Boolean | true | restrict validation to own properties of data object. |
| [schema] | Object | | JSON Schema. Defaults to the included schema
. |
Example
const validator = validate();
const valid = validator(queryParams); // where queryParams is an object
if (!valid) {
// Log errors
console.log(validator.errors);
}
Examples
For Default Verification
const {validate} = require('@centralping/json-api-query');
const validator = validate();
// queryParams would be an query param object to validate/coerce
const valid = validator(queryParams);
if (!valid) {
// Log errors
console.log(validator.errors);
}
For Extended Verification
const {validate, schema} = require('@centralping/json-api-query');
// extend schema
const validator = validate(undefined, schema);
// queryParams would be an query param object to validate/coerce
const valid = validator(queryParams);
if (!valid) {
// Log errors
console.log(validator.errors);
}
For AJV options
const {validate} = require('@centralping/json-api-query');
const validator = validate({allErrors: true});
// queryParams would be an query param object to validate/coerce
const valid = validator(queryParams);
if (!valid) {
// Log errors
console.log(validator.errors);
}
For Parsing & Verification
const {parse, validate} = require('@centralping/json-api-query');
const validator = validate();
const {query} = parse(url); // url is a string
const valid = validator(query);
if (!valid) {
// Log errors
console.log(validator.errors);
}
Test
npm test
With coverage reporting:
npm test -- --coverage
With file watch:
npm run watch
License
MIT