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

jke-next

v2.0.2

Published

Joi Key Extensions provides extensions to the [Joi](https://github.com/hapijs/joi) validation library that allow validation of: * Foreign key style relationships, where a value must be a reference to a value elsewhere in an object tree. Unlike the Joi

Downloads

6

Readme

Joi Key Extensions

Joi Key Extensions provides extensions to the Joi validation library that allow validation of:

  • Foreign key style relationships, where a value must be a reference to a value elsewhere in an object tree. Unlike the Joi built in built in ref validation, it can traverse arrays to look up references
  • Primary key values, validating that a set of keys are unique within an array of objects

fkExtension

Usage

npm install --save joi-key-extensions
const VanillaJoi = require('joi');
const { fkExtension } = require('joi-key-extensions');

// add the extension to Joi
const Joi = VanillaJoi.extend(fkExtension.string);

const makeSchema = Joi.object({
  makeId: Joi.string(),
  name: Joi.string(),
});

const modelSchema = Joi.object({
  modelId: Joi.string(),
  name: Joi.string(),
  makeId: Joi.string()
    // define the foreign key reference
    .fk('makes.[].makeId'),
});

const schema = Joi.object({
  makes: Joi.array().items(makeSchema),
  models: Joi.array().items(modelSchema),
});

console.log('Examples with valid data');

const data = {
  makes: [
    { makeId: 'ford', name: 'Ford' },
    { makeId: 'mazda', name: 'Mazda' },
  ],
  models: [
    { modelId: 'laser', name: 'Laser', makeId: 'ford' },
    { modelId: 'familia', name: 'Familia', makeId: 'mazda' },
  ],
};

console.log('Validating whole object tree');
let validationResult = Joi.validate(data, schema, {
  context: {
    data, // pass whole object tree as context.data
    schema, // pass schema of whole object tree as context.schema
  },
});

console.log(validationResult.error);
// null - no error

console.log('Validating one model out of the object tree');
validationResult = Joi.validate(data.models[0], modelSchema, {
  context: {
    data,
    schema,
  },
});

console.log(validationResult.error);
// null - no error

console.log('Examples with invalid data');

data.models[0].makeId = 'fnord';

console.log('Validating whole object tree');
validationResult = Joi.validate(data, schema, {
  context: {
    data,
    schema,
  },
});

console.log(validationResult.error);
// { ValidationError: child "models" fails because
// ["models" at position 0 fails because
//   [child "makeId" fails because
//     ["makeId" "fnord" could not be found as a reference to "makes.[].makeId"]]]...

console.log('Validating one model out of the object tree');
validationResult = Joi.validate(data.models[0], modelSchema, {
  context: {
    data,
    schema,
  },
});

console.log(validationResult.error);
// { ValidationError: child "makeId" fails because
// ["makeId" "fnord" could not be found as a reference to "makes.[].makeId"]...

API

Joi.string().fk(args)

Joi.date().fk(args)

Joi.number().fk(args)

Requires that a field value must be a reference to a value elsewhere in the object tree.

In the simplest case args is a string with a dot seperated list of object fields, with search across an array identified by square brackets ([]).

E.g. Joi.number().fk('species.[].speciesId') requires that the value must match the speciesId field in one of the items in the species array field.

The extension also supports multi-part foreign keys. E.g.

const animalSchema = Joi.object({
  animalId: Joi.string(),
  // genusId must correspond to an element in the genus array, matching on the genusId field
  genusId: Joi.string().fk('genus.[].genusId'),
  // speciesId must correspond to an element in the species array belonging to the genus element
  // identified by the genusId field
  speciesId: Joi.string().fk('genus.[].species.[].speciesId', { parentFieldName: 'genusId' }),
});

Refer to __tests__ /fkExtension.text.js for more examples

pkExtension

Usage

npm install --save joi-key-extensions
const VanillaJoi = require('joi');
const { pkExtension } = require('joi-key-extensions');

// add the extension to Joi
const Joi = VanillaJoi
  .extend(pkExtension.number)
  .extend(pkExtension.array);

const countrySchema = Joi.object({
  countryId: Joi.number().pk(),
  countryName: Joi.string(),
});

const schema = Joi.object({
  countries: Joi.array()
    .items(countrySchema)
    .uniqueOnPks(),
});

const data = {
  countries: [
    { countryId: 1, countryName: 'Estonia' },
    { countryId: 2, countryName: 'Uruguay' },
  ],
};

console.log('Example with valid data');

let validationResult = Joi.validate(data, schema, {
  context: {
    data, // pass whole object tree as context.data
    schema, // pass schema of whole object tree as context.schema
  },
});

console.log(validationResult.error);
// null - no error

console.log('Example with invalid data');

data.countries.push({ countryId: 1, countryName: 'Fiji' });

validationResult = Joi.validate(data, schema, {
  context: {
    data,
    schema,
  },
});

console.log(validationResult.error);
// null - no error
// { ValidationError: child "countries" fails because
// ["countries" There is a duplicate value at path countries for keys {"countryId":1}]

API

Joi.string().pk()

Joi.number().pk()

Joi.date().pk()

Identifies that the field is part of a primary key (composed of one or more fields).

Joi.array().uniqueOnPks()

Requires that the elements of the array must be unique with respect to their primary key fields