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

felicity

v6.0.0

Published

Javascript object instantiation from Joi schema

Downloads

16,615

Readme

felicity

Felicity supports Joi schema management by providing 2 primary functions:

  1. Testing support - Felicity will leverage your Joi schema to generate randomized data directly from the schema. This can be used for database seeding or fuzz testing.
  2. Model management in source code - Felicity can additionally leverage your Joi schema to create constructor functions that contain immutable copies of the Joi schema as well as a simple .validate() method that will run Joi validation of the object instance values against the referenced Joi schema.

npm version Build Status Known Vulnerabilities

Lead Maintainer: Wes Tyler

Introduction

fe·lic·i·ty noun intense happiness; the ability to find appropriate expression for one's thoughts or intentions.

Felicity provides object instances, or expressions, of the data intentions represented by Joi schema.

Felicity builds upon Joi by allowing validation to be contained cleanly and nicely in constructors while also allowing easy example generation for documentation, tests, and more.

Installation

npm install felicity

Usage

Model Management

Given a joi schema, create an object Constructor and instantiate skeleton objects:

const Joi      = require('joi');
const Felicity = require('felicity');

const joiSchema = Joi.object().keys({
    key1: Joi.string().required(),
    key2: Joi.array().items(Joi.string().guid()).min(3).required(),
    key3: Joi.object().keys({
        innerKey: Joi.number()
    })
});

const FelicityModelConstructor = Felicity.entityFor(joiSchema);
const modelInstance = new FelicityModelConstructor({ key1: 'some value' });

console.log(modelInstance);
/*
{
    key1: 'some value',
    key2: [],
    key3: {
        innerKey: 0
    }
}
*/

These model instances can self-validate against the schema they were built upon:

modelInstance.key3.innerKey = 42;

const validationResult = modelInstance.validate(); // uses immutable copy of the Joi schema provided to `Felicity.entityFor()` above

console.log(validationResult);
/*
{
    success: false,
    errors : [
        {
            "message": "\"key2\" must contain at least 3 items",
            "path": [ "key2" ],
            "type": "array.min",
            "context": {
                "limit": 3,
                "value": [],
                "key": "key2",
                "label": "key2"
            }
        },
        // ...
    ]
}
*/

Testing Usage

Additionally, Felicity can be used to randomly generate valid examples from either your Felicity Models or directly from a Joi schema:

const randomModelValue = FelicityModelConstructor.example(); // built in by `Felicity.entityFor()`
/*
{
    key1: '2iwf8af2v4n',
    key2:[
        '077750a4-6e6d-4b74-84e2-cd34de80e95b',
        '1a8eb515-72f6-4007-aa73-a33cd4c9accb',
        'c9939d71-0790-417a-b615-6448ca95c30b'
    ],
    key3: { innerKey: 3.8538257114788257 }
}
*/

// directly from Joi schemas:
const stringSchema = Joi.string().pattern(/[a-c]{3}-[d-f]{3}-[0-9]{4}/);
const sampleString = Felicity.example(stringSchema);
// sampleString === 'caa-eff-5144'

const objectSchema = Joi.object().keys({
    id      : Joi.string().guid(),
    username: Joi.string().min(6).alphanum(),
    numbers : Joi.array().items(Joi.number().min(1))
});
const sampleObject = Felicity.example(objectSchema);
/*
sampleObject
{
    id: '0e740417-1708-4035-a495-6bccce560583',
    username: '4dKp2lHj',
    numbers: [ 1.0849635479971766 ]
}
*/

Node.js version compatibility

Please note that Felicity follows Node.js LTS support schedules as well as Joi Node.js version support.

Beginning with [email protected], only Node.js versions 12 and above will be supported.

API

For full usage documentation, see the API Reference.

Contributing

We love community and contributions! Please check out our guidelines before making any PRs.

Setting up for development

Getting yourself setup and bootstrapped is easy. Use the following commands after you clone down.

npm install && npm test

Joi features not yet supported

Some Joi schema options are not yet fully supported. Most unsupported features should not cause errors, but may be disregarded by Felicity or may result in behavior other than that documented in the Joi api.

A feature is considered Felicity-supported when it is explicitly covered in tests on both entityFor (and associated instance methods) and example.

  • Function
    • ref
  • Array
    • unique
  • Object
    • requiredKeys
    • optionalKeys