How to use the number function from joi
Find comprehensive JavaScript joi.number code examples handpicked from public code repositorys.
joi.number is a function in the Joi library that validates whether a value is a number.
GitHub: mozilla/fxa
455 456 457 458 459 460 461 462 463 464
response: { schema: isA.object({ passwordForgotToken: isA.string(), ttl: isA.number(), codeLength: isA.number(), tries: isA.number(), }), }, }, handler: async function (request) {
+ 7 other calls in file
80 81 82 83 84 85 86 87 88 89
}).required(), topic: joi.string().required(), groupId: joi.string().required(), queueProcessor: joi.func(), fromOffset: joi.alternatives().try('latest', 'earliest', 'none'), concurrency: joi.number().greater(0).default(CONCURRENCY_DEFAULT), fetchMaxBytes: joi.number(), canary: joi.boolean().default(false), bootstrap: joi.boolean().default(false), circuitBreaker: joi.object().optional(),
+ 11 other calls in file
How does joi.number work?
joi.number is a function in the Joi library that returns a schema object used for validating that a given value is a number. The schema can be further customized by adding optional constraints and properties, such as: min and max: define minimum and maximum values the number can take. greater and less: define minimum and maximum values the number can take, but exclude the boundary values. precision: define the maximum number of decimal places the number can have. integer: validate that the number is an integer. positive and negative: validate that the number is positive or negative, respectively. The joi.number function can also take an optional locale parameter, which determines the format of the number for localization purposes. After defining the schema, it can be used to validate a value using the validate method of a Joi object. If the value passes validation, the method returns an object with a value property containing the validated value. If the value fails validation, the method throws a ValidationError with a message describing the validation error.
2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
.items( Joi.object() .keys({ userId: Joi.number().integer().positive().required(), handle: Joi.string().required(), placement: Joi.number().integer().positive().required(), type: Joi.string() .valid(_.values(constants.prizeSetTypes)) .default(constants.prizeSetTypes.ChallengePrizes), })
+ 27 other calls in file
335 336 337 338 339 340 341 342 343 344
const { error: error_id, value: id } = Joi.string().required().validate(req.params.id, { stripUnknown: true }); if (error_id) return res.status(400).send({ ok: false, code: ERRORS.INVALID_PARAMS }); // --- validate data const bodySchema = Joi.object().keys({ parent: Joi.number().valid(1, 2).required(), allow: Joi.boolean().required(), }); const result = bodySchema.validate(req.body, { stripUnknown: true }); const { error, value } = result;
+ 9 other calls in file
Ai Example
1 2 3 4 5 6 7 8 9
const Joi = require("joi"); const schema = Joi.object({ age: Joi.number().integer().min(18).max(99).required(), }); const result = schema.validate({ age: 25 }); console.log(result); // { value: { age: 25 } }
In this example, we define a schema that expects an object with a property called age, which must be an integer between 18 and 99. The required method is used to indicate that the age property is mandatory. We then use the validate method to validate an object with an age property equal to 25. Since 25 is a valid number according to the schema, the validate method returns an object with a value property containing the validated value { age: 25 }.
921 922 923 924 925 926 927 928 929 930
toRegion: Joi.string().trim().valid(null), centerId: Joi.string().trim().valid(null), centerName: Joi.string().trim().valid(null), centerCity: Joi.string().trim().valid(null), sessionId: Joi.string().trim().valid(null), youngsVolume: Joi.number().greater(0), gatheringPlaces: Joi.array().items(Joi.string()), }); const { error, value } = bodySchema.validate(req.body, { stripUnknown: true }); if (error) {
+ 34 other calls in file
GitHub: postalsys/emailengine
5258 5259 5260 5261 5262 5263 5264 5265 5266 5267
.max(1024 * 1024) .default(0) .example(0) .description('Page number (zero indexed, so use 0 for first page)') .label('PageNumber'), pageSize: Joi.number().min(1).max(1000).default(20).example(20).description('How many entries per page').label('PageSize') }).label('OutbixListFilter') }, response: {
+ 69 other calls in file
31 32 33 34 35 36 37 38 39 40 41
const tokenize = require('@avocadodb/foxx/router/tokenize'); const MIME_JSON = 'application/json; charset=utf-8'; const DEFAULT_ERROR_SCHEMA = joi.object().keys({ error: joi.allow(true).required(), errorNum: joi.number().integer().optional(), errorMessage: joi.string().optional(), code: joi.number().integer().optional() });
5 6 7 8 9 10 11 12 13 14
const addSchema = Joi.object({ transactionType: Joi.boolean().required().messages({ 'boolean.base': `{{#label}} should be a type of boolean`, 'any.required': `{{#label}} is a required field`, }), amount: Joi.number().min(0).required().messages({ 'number.base': `{{#label}} should be a type of number`, 'number.min': `{{#label}} cannot be negative and must be greater than 0`, 'any.required': `{{#label}} is a required field`, }),
GitHub: arangodb/arangodb
185 186 187 188 189 190 191 192 193 194
res.json({msg}); }) .body(joi.object({ query: joi.string().required(), bindVars: joi.object().optional(), batchSize: joi.number().optional(), id: joi.string().optional() }).required(), 'Query and bindVars to explain.') .summary('Explains a query') .description(dd`
23 24 25 26 27 28 29 30 31 32
const resultModel = Joi.object({ equals: Joi.number() }).label('Result'); const errorModel = Joi.object({ code: Joi.number(), msg: Joi.string() }).label('Error'); const sumHTTPStatus = {
+ 59 other calls in file
GitHub: koopjs/FeatureServer
10 11 12 13 14 15 16 17 18 19
const relationshipsInfo = require('./relationships-info-route-handler') const responseHandler = require('./response-handler') const queryParamSchema = joi.object({ limit: joi.number().optional(), resultRecordCount: joi.number().optional() }).unknown() const geojsonMetadataSchema = joi.object({ maxRecordCount: joi.number().prefs({ convert: false }).optional().default(2000)
+ 5 other calls in file
163 164 165 166 167 168 169 170 171 172
const envVarsSchema = joi.object({ NODE_ENV: joi.string() .valid(['development', 'production', 'test', 'provision']) .required(), PORT: joi.number() .required(), LOGGER_LEVEL: joi.string() .valid(['error', 'warn', 'info', 'verbose', 'debug', 'silly']) .default('info'),
GitHub: laino/final-pm
38 39 40 41 42 43 44 45 46 47
number: Joi.number().integer().optional() }).optional() ], stop: [ Joi.number().integer() ], kill: [ Joi.number().integer()
+ 9 other calls in file
GitHub: adobe/aio-tvm
42 43 44 45 46 47 48 49 50 51
* @protected */ constructor () { this._validationSchema = { /// deployment params - should not be setable by request - deployed as final expirationDuration: joi.number().integer().min(600).max(18000).required(), approvedList: joi.string().required(), owApihost: joi.string().uri().required(), disableAdobeIOApiGwTokenValidation: joi.string().allow('').optional(), imsEnv: joi.string().allow('').optional(),
GitHub: datawrapper/datawrapper
22 23 24 25 26 27 28 29 30 31
limit: Joi.number() .integer() .min(0) .default(1000) .description('Maximum items to fetch. Useful for pagination.'), offset: Joi.number() .integer() .min(0) .default(0) .description('Number of items to skip. Useful for pagination.')
+ 7 other calls in file
GitHub: datawrapper/datawrapper
53 54 55 56 57 58 59 60 61 62
.default(100) .description( 'We will take this number of latest jobs that finished and check ' + 'how many have completed and how many have failed' ), minCompletedJobsRatio: Joi.number() .min(0) .max(1) .default(1) .description(
+ 19 other calls in file
GitHub: nodemailer/wildduck
13 14 15 16 17 18 19 20 21 22
res.charSet('utf-8'); const schema = Joi.object().keys({ type: Joi.string().empty('').lowercase().max(128), user: Joi.string().hex().lowercase().length(24), limit: Joi.number().default(20).min(1).max(250), next: nextPageCursorSchema, previous: previousPageCursorSchema, page: pageNrSchema, sess: sessSchema,
+ 3 other calls in file
GitHub: derek-oneill/halacious
52 53 54 55 56 57 58 59 60 61
var optionsSchema = { absolute: joi.boolean().default(false), host: joi.string(), hostname: joi.string(), port: joi.number().integer(), protocol: joi.string(), strict: joi.boolean().default(false), relsPath: joi.string().default('/rels'), relsAuth: joi.alternatives().try(joi.boolean().allow(false),joi.object()).default(false),
+ 3 other calls in file
GitHub: arangodb/arangodb
38 39 40 41 42 43 44 45 46
const { MergeStatisticSamples } = require('@arangodb/statistics-helper'); const router = createRouter(); module.exports = router; const startOffsetSchema = joi.number().default( () => internal.time() - STATISTICS_INTERVAL * 10, 'Default offset' );
GitHub: scality/cloudserver
16 17 18 19 20 21 22 23 24 25
IAMEndpoint: joi.string().required(), STSEndpoint: joi.string().required() }), SystemRecommendations: joi.object({ S3ConcurrentTaskLimit: joi.number().min(0).default(64), S3MultiObjectDeleteLimit: joi.number().min(1).default(1000), StorageCurrentTasksLimit: joi.number().min(0).default(0), KbBlockSize: joi.number() .valid(256, 512, 1024, 2048, 4096, 8192) .default(1024),
+ 15 other calls in file
joi.string is the most popular function in joi (40578 examples)