
Product
Unify Your Security Stack with Socket Basics
A single platform for static analysis, secrets detection, container scanning, and CVE checks—built on trusted open source tools, ready to run out of the box.
@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('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('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('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('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
FAQs
A JSON API compliant query parameter validator.
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.
Product
A single platform for static analysis, secrets detection, container scanning, and CVE checks—built on trusted open source tools, ready to run out of the box.
Product
Socket is launching experimental protection for the Hugging Face ecosystem, scanning for malware and malicious payload injections inside model files to prevent silent AI supply chain attacks.
Research
/Security News
The Socket Threat Research Team uncovered a coordinated campaign that floods the Chrome Web Store with 131 rebranded clones of a WhatsApp Web automation extension to spam Brazilian users.