0.5.0 • Published 2 years ago

@beanc16/joi-helpers v0.5.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@beanc16/joi-helpers

Helpers to make JOI validation simpler.

npm license downloads

Table of Contents

Install

This is a Node.js module available through the npm registry.

$ npm install @beanc16/joi-helpers

Usage

Basic

const {
    JoiRequired,
    validateJoiSchema,
    joiSchemaIsValid,
} = require("@beanc16/joi-helpers");

const payload = {
    id: 1,
    name: "Example",
};

/*
The following schema is equivalent to:
const mySchema = Joi.object({
    id: Joi.number().required(),
    name: Joi.string().required(),
}).required();
*/
const mySchema = JoiRequired.object({
    id: JoiRequired.number(),
    name: JoiRequired.string(),
});

// Logs: { id: 1, name: "Example" }
validateJoiSchema(mySchema, payload)
.then((value) =>
{
    console.log(value);
})
.catch((error) =>
{
    console.log(error);
});

// Logs: Schema is valid
if (joiSchemaIsValid(mySchema, payload))
{
    console.log("Schema is valid");
}
else
{
    console.log("Schema is invalid");
}

MongoDB

const {
    JoiMongoDb,
    JoiMongoDbObjectId,
    JoiMongoDbObjectIdRequired,
    validateJoiSchema,
} = require("@beanc16/joi-helpers");

const payload = getSomeMongoDbObjectId();

/*
The following is the equivalent of running:
const { ObjectId } = require("mongodb");
const result = ObjectId.isValid(payload);

if (result)
{
    console.log(value);
}
else
{
    console.log(error);
}
*/
const mySchema = JoiMongoDbObjectId();

validateJoiSchema(mySchema, payload)
.then((value) =>
{
    console.log(value);
})
.catch((error) =>
{
    console.log(error);
});


// const JoiMongoDbObjectId = JoiMongoDb.string().objectId();
// const JoiMongoDbObjectIdRequired = JoiMongoDbObjectId().required();

/*
Instead of JoiMongoDb.string().objectId(), you can also do:
const schema = JoiMongoDb.number().objectId();
*/

Microservices

// Common helpers for microservices I make

const Joi = require("joi");
const {
    envString,
    envStringRequired,
    envsArray,
    envsArrayRequired,
    dataObj,
    dataObjRequired,
} = require("@beanc16/joi-helpers");


/*
const envsEnum = {
    "envs": [
        "dev",
        "staging",
        "prod",
    ],
};

const envString = Joi.string().valid(...envsEnum.envs);
const envStringRequired = envString.required();


const envsArray = Joi.array()
                     .items(envStringRequired)
                     .min(1)                      // At least one env
                     .max(envsEnum.envs.length);  // No more than all envs

const envsArrayRequired = envsArray.required();


const dataObj = Joi.object()
.max(100)
.pattern(
    Joi.string().max(100),                                  // Keys
    Joi.alternatives().try(                                 // Values
        Joi.string()
           .min(1)
           .max(500),
        Joi.number()
           .min(-999999999999999999999999999999)            // 30 digit
           .max(999999999999999999999999999999),            // 30 digit
        Joi.boolean(),
        Joi.date(),
    ),
)

const dataObjRequired = dataObj.required();;
*/

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

0.5.0

2 years ago

0.3.0

2 years ago

0.4.0

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago