Product
Socket Now Supports uv.lock Files
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
@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.
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.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.