JSPM

@centralping/json-api-query

0.0.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 6
  • Score
    100M100P100Q34219F
  • License MIT

A JSON API compliant query parameter validator.

Package Exports

  • @centralping/json-api-query

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@centralping/json-api-query) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@CentralPing/json-api-query

Build Status Coverage Status Dependency Status Greenkeeper Status Known Vulnerabilities

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~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('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('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('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);
}

## Test

`npm test`

With coverage reporting:

`npm test -- --coverage`

With file watch:

`npm run watch`

## License

MIT