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-decimal

v1.1.2

Published

Joi extension for decimals

Downloads

112

Readme

joi-decimal

Joi extension for Decimal type.

Useful to validate any scientific / financial number.

⚠️ do not use .precision() for now as it doesn't work as expected. It will be fixed in v1.2.0.

Build Status npm version

Usage

const BaseJoi = require('@hapi/joi');
const DecimalExtension = require('joi-decimal');
const Joi = BaseJoi.extend(DecimalExtension);

const schema = Joi.decimal().greater(100.0);

const result = schema.validate(101.00);
// result.error === null -> valid

API

decimal - inherits from Any

Generates a schema object that matches a Decimal type (as well as a JavaScript string or number that can be converted to Decimal type). If the validation convert option is on (enabled by default), a string or number will be converted to a Decimal, if specified. Also, if convert is on and decimal.precision() is used, the value will be converted to the specified precision as well.

const dec = Joi.decimal();
dec.validate('0.046875', (err, value) => { });

Possible validation errors: decimal.base

decimal.finite()

Requires the number to be finite.

const schema = Joi.decimal().finite();

Possible validation errors: decimal.finite

decimal.greater(limit)

Specifies that the value must be greater than limit or a reference.

const schema = Joi.decimal().greater(5.000);
const schema = Joi.object({
  min: Joi.decimal().required(),
  max: Joi.decimal().greater(Joi.ref('min')).required()
});

Possible validation errors: decimal.greater, decimal.ref

decimal.integer()

Requires the number to be an integer (no floating point).

const schema = Joi.decimal().integer();

Possible validation errors: decimal.integer

decimal.less(limit)

Specifies that the value must be less than limit or a reference.

const schema = Joi.decimal().less(10);
const schema = Joi.object({
  min: Joi.decimal().less(Joi.ref('max')).required(),
  max: Joi.decimal().required()
});

Possible validation errors: decimal.less, decimal.ref

decimal.max(limit)

Specifies the maximum value where:

  • limit - the maximum value allowed or a reference.
const schema = Joi.decimal().max(10);
const schema = Joi.object({
  min: Joi.decimal().max(Joi.ref('max')).required(),
  max: Joi.decimal().required()
});

Possible validation errors: decimal.max, decimal.ref

decimal.min(limit)

Specifies the minimum value where:

  • limit - the minimum value allowed or a reference.
const schema = Joi.decimal().min(2);
const schema = Joi.object({
  min: Joi.decimal().required(),
  max: Joi.decimal().min(Joi.ref('min')).required()
});

Possible validation errors: decimal.min, decimal.ref

decimal.multiple(base)

Specifies that the value must be a multiple of base (or a reference):

const schema = Joi.decimal().multiple(3);

Possible validation errors: decimal.multiple, decimal.ref

decimal.nan()

Requires the number to be NaN.

const schema = Joi.decimal().nan();

Possible validation errors: decimal.nan

decimal.negative()

Requires the number to be negative.

const schema = Joi.decimal().negative();

Possible validation errors: decimal.negative

decimal.positive()

Requires the number to be positive.

const schema = Joi.decimal().positive();

Possible validation errors: decimal.positive

decimal.precision(sd, rm)

Specifies the maximum precision where:

  • sd - the number of significant digits on which to round.
  • rm - the rounding mode to use.
const schema = Joi.decimal().precision(2, 5);

Possible validation errors: decimal.precision

decimal.zero()

Requires the number to be zero.

const schema = Joi.decimal().zero();

Possible validation errors: decimal.zero

List of errors

decimal.base

The value is not a Decimal or could not be cast to a Decimal.

decimal.finite

The number was not finite.

decimal.greater

The number is lower or equal to the limit that you set.

Additional local context properties:

{
    limit: number // Minimum value that was expected for this number
}

decimal.integer

The number is not a valid integer.

decimal.less

The number is higher or equal to the limit that you set.

Additional local context properties:

{
    limit: number // Maximum value that was expected for this number
}

decimal.max

The number is higher than the limit that you set.

Additional local context properties:

{
    limit: number // Maximum value that was expected for this number
}

decimal.min

The number is lower than the limit that you set.

Additional local context properties:

{
    limit: number // Minimum value that was expected for this number
}

decimal.multiple

The number could not be divided by the base you provided.

Additional local context properties:

{
    base: number // The number of which the input is supposed to be a multiple of
}

decimal.nan

The number is not NaN.

decimal.negative

The number was positive.

decimal.only

Only some values were allowed, the input didn't match any of them.

Additional local context properties:

{
    values: Array<any> // Contains the list of the valid values that were expected
}

decimal.positive

The number was negative.

decimal.precision

The arguments (sd and/or rm) are not numbers.

decimal.ref

A reference was used in one of decimal.greater(), decimal.less(), decimal.max(), decimal.min() or decimal.multiple() and the value pointed to by that reference in the input is not a valid Decimal or could not be cast to a Decimal.

decimal.zero

The number is not zero.