Skip to content

Validating number with comma as decimal point #2017

@AlexZeitler

Description

@AlexZeitler

I have this code

const Joi = require('@hapi/joi')
const dn = '37,5'
const x = { dn }
const schema = Joi.object({
  dn: Joi.number()
})

const { error, result } = Joi.validate(x, schema)
console.log('error', error)

Result:

error { ValidationError: child "dn" fails because ["dn" must be a number]
    at Object.exports.process (/Users/alexzeitler/src/pdmlab/joi-numbers/node_modules/@hapi/joi/lib/errors.js:202:19)
    at internals.Object._validateWithOptions (/Users/alexzeitler/src/pdmlab/joi-numbers/node_modules/@hapi/joi/lib/types/any/index.js:764:31)
    at module.exports.internals.Any.root.validate (/Users/alexzeitler/src/pdmlab/joi-numbers/node_modules/@hapi/joi/lib/index.js:145:23)
    at Object.<anonymous> (/Users/alexzeitler/src/pdmlab/joi-numbers/index.js:8:31)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
  isJoi: true,
  name: 'ValidationError',
  details:
   [ { message: '"dn" must be a number',
       path: [Array],
       type: 'number.base',
       context: [Object] } ],
  _object: { dn: '37,5' },
  annotate: [Function] }

How can I assign the the comma sign in 37,5 as decimal point which is the default in Germany?

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew functionality or improvement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions