npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@centralping/json-api-query

v0.1.3

Published

A JSON API compliant query parameter validator.

Downloads

1

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~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