Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@centralping/json-api-query

Package Overview
Dependencies
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@centralping/json-api-query

A JSON API compliant query parameter validator.

  • 0.1.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

@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~parse ⇒ Object

Kind: inner property of jsonApiQuery
Returns: Object - A url parse object.

ParamTypeDescription
urlStringAny 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

ParamTypeDefaultDescription
[options]Objectany AJV option.
[options.coerceTypes]Boolean | String'array'coerce validated values to specified types.
[options.ownProperties]Booleantruerestrict validation to own properties of data object.
[schema]ObjectJSON 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

Keywords

FAQs

Package last updated on 22 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc