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

joi-decimal

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

joi-decimal

Joi extension for decimals

  • 1.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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.

Keywords

FAQs

Package last updated on 31 Jul 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