![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@centralping/json-api-query
Advanced tools
An extensible JSON Schema for validating and optionally coercing JSON API query parameters for fetching data.
// 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'
}
}
npm i --save @centralping/json-api-query
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
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'
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);
}
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);
}
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);
}
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);
}
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);
}
npm test
With coverage reporting:
npm test -- --coverage
With file watch:
npm run watch
MIT
v0.1.3 (2018-11-22)
FAQs
A JSON API compliant query parameter validator.
The npm package @centralping/json-api-query receives a total of 0 weekly downloads. As such, @centralping/json-api-query popularity was classified as not popular.
We found that @centralping/json-api-query demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
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.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.