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

joi-validate-patch

v0.0.4

Published

Validator for json patch contents according to joi document schemas

Downloads

215

Readme

joi-validate-patch

version npm build status Coveralls deps status mit license

JoiValidatePatch is a node library which validates that operations in a JSON patch document fit within a Joi validation schema describing a document structure. Validation is performed using only the schema, independently from the document(s) to be modified.

Note: only validation of independent values can be meaningfully supported.

The primary use-case is for simple schemas covering the basics of sanity validation when accepting a JSON patch to be converted into some other form of dynamic operation where loading the documents, applying the patch, and validating the result is impractical. The typical example would be updating a mongo store or relational database

Within the limitations of the use-case, some validations are easy (can the path of the operation exist in the schema?), others are challenging (if moving content from one path to another, are the schema rules compatible?), and others still are impossible (if two paths have interdependent rules, will they still be satisfied when changing one of those paths?). JoiValidatePatch only handles the easy rules and leaves the rest up to custom solutions. It can however sidestep some complexities by simply receiving a subset of the true document schema, consisting only of the paths that are safe to independently modify and/or covered by additional validation logic elsewhere.

Basic Usage

Validating a patch document against a Joi schema:

const
    Joi = require('joi'),
    JVPatch = require('joi-validate-patch');

const schema = Joi.object().keys({
    id: Joi.string().guid().required().label('id'),
    name: Joi.string().required().label('name'),
    description: Joi.string().optional().label('description'),
    favoriteToys: Joi.array().items(Joi.string().label('toy')).default([]).label('favoriteToys'),
    meta: {
        born: Joi.date().required().label('born'),
        weight: Joi.number().positive().unit('pounds').label('weight')
    }
}).label('cat');

const patch = [
    {op: 'replace', path: '/name', value: 'Tigger'},
    {op: 'add', path: '/favoriteToys/-', value: 'laser pointer'},
];

const result = JVPatch.validate(patch, schema);

if(result.error) throw result.error;

const normalizedPatch = result.value;

API

lib.ValidationError(message, details) ⇒ ValidationError

Constructor for custom error class. Takes on the properties of a patch step passed into it, or adds an errors property aggregating sub-errors.

Params:

  • string message
  • object details (optional single JSON patch operation or {errors: [ValidationError, ...]})

Returns: ValidationError

lib.validate(patchDocs, joiSchema, [options], [callback]) ⇒ {error: ValidationError|null, value: normalizedPatchDocs}

Main library method, performs validation against a Joi schema like Joi, but accepts a json-patch item or array rather than the actual document.

Maintains consistency with Joi.validate signature and behavior (even down to the non-async callback support).

Params:

  • array patchDocs (array of objects describing JSON patch operations)
  • object joiSchema
  • object options
    • abortEarly (default true)
    • allowedOps (array of strings for allowed patch operation types - default all)
    • allowUnknown (default false)
    • convert (default true)
  • function callback (if provided, called with error, value instead of returning an object)

Returns: {error: ValidationError|null, value: [patchOperation, ...]}