Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

joiless

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

joiless

Utility functions for using Joi without using Joi

  • 1.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6
increased by50%
Maintainers
1
Weekly downloads
 
Created
Source

Joiless Build Status

Utility functions for using Joi without using Joi.

Installation

Joiless lives on npm, so just install it via the command line and you're good to go.

$ npm install --save joi joiless

You need to install joi itself as this module does not include it as a production dependency (so you can use your own joi version). If you try a version which has an issue caused by the module, please file an issue.

Example

I love Joi's validation, and I love Hapi, so I use Joi all the time. Unfortunately I hate Joi's chaining, so I wrote Joiless to assist. Basically, it's Joi using JSON.

Here's an example (both of these are identical, and are in fact a test case in this repo):

const Joi = require('joi');
const Joiless = require('joiless');

// Joi version
let joiSpec = Joi
  .date()
  .description('The date (in milliseconds) that this crash last appeared')
  .timestamp('javascript')
  .default(Date.now, 'Current timestamp generation');

// Joiless version
let joilessSpec = Joiless.spec('date', {
  description: 'The date (in milliseconds) that this crash last appeared',
  timestamp: 'javascript',
  default: [ Date.now, 'Current timestamp generation' ]
});

The translation is simple; the key is the function called on the Joi chain, and the value is the value passed into the Joi chain call.

  • If you pass an Array as a value, it's applied to the Joi function - if you wish to pass an Array as an argument, use [ [ <your_array> ] ].
  • If you want to call a function with no arguments, either pass undefined as the value, or you can use the after hook (whichever you prefer):
const Joiless = require('joiless');

// via undefined
Joiless.spec('string', {
  after: function (spec) {
    spec.strip();
    spec.insensitive();
  }
});

// via after
Joiless.spec('string', {
  strip: undefined,
  insensitive: undefined
})

Other Usage

Attaching Child References

If you make a complex schema, you can use attach to hook the child keys onto the parent.

For example, in the snippet below you can reference Record.Username to pull back the username spec. This is done using get, it doesn't store the value twice.

const Joiless = require('joiless');

// define the spec
const Record = Joiless.spec({

  type: 'object',

  description: 'A database model',

  keys: {

    username: Joiless.spec({
      type: 'string',
      description: 'The username for this record',
      required: true
    })

  }

});

// attach the children
Joiless.attach(Record);
Extending Schemas

Sometimes you wish to define a base schema, and then extend it to override some of the parent stuff. Naturally, the spec API doesn't fit here, so I added an extend in v1.1.

Extension is easy, you just pass the schema to extend as first arg, and your spec overrides in the second argument.

const Joi = require('joi');
const Joiless = require('joiless');

// define the base spec
let base = Joiless.spec('object', {
  keys: {
    a: Joiless.spec('number'),
    b: Joiless.spec('string')
  }
});

// define our extension
let extended = Joiless.extend(base, {
  keys: {
    a: Joiless.spec('string')
  }
});

// run validation
Joi.validate({ a: 'hello' }, base);     // fails
Joi.validate({ a: 'hello' }, extended); // passes

Keywords

FAQs

Package last updated on 27 Feb 2019

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc