joi

Implement Joi in Typescript

前提是你 提交于 2019-12-13 20:13:40
问题 Simple joi validation snippet in javascript.It will simply return an error object when validation fails. validate.js const Joi =require("joi"); function validateObject (input) { const schema = { key: Joi.string().required(), }; return Joi.validate(input, schema); }; let {error} = validateObject({key:5}) console.log(error) Now I am learning typescript and like to do the exact functionality in TS.I am aware that Joi is a javascript library but can we make use of it in Typescript.When exploring

npm joi with null, undefined, empty values and a default value

亡梦爱人 提交于 2019-12-12 06:23:54
问题 I am using older version of npm module joi => 10.2.2 and i am trying to figure out how can i build the schema so empty, null, undefined values are allowed with a default value. This works https://github.com/hapijs/joi/issues/516 var schema = joi.object().keys({a:[joi.string().optional(), joi.allow(null)]}) but i don't know how to specify a default value with this. What i am looking for is this https://github.com/hapijs/joi/issues/1066 (version v10.5.2) but with the syntax of older version of

Hapi/Joi Validation For Number Fails

梦想与她 提交于 2019-12-11 17:09:01
问题 I am trying to validate number value which will include integer as well as float values. Following is my implementation for the same. Joi Schema. const numcheckschema = Joi.object().keys({ v1:Joi.number().empty("").allow(null).default(99999), v2:Joi.number().empty("").allow(null).default(99999), v3:Joi.number().empty("").allow(null).default(99999) }) Object objnum={ v1:"15", v2:"13.", v3:"15" } objValidated = Joi.validate(objnum, numcheckschema); console.log(objValidated); When i execute the

Unusual behaviour of any.when() in Joi

北城以北 提交于 2019-12-11 16:56:03
问题 const Joi = require('@hapi/joi') var schema_1 = Joi.object({ a: Joi.number().integer(), b: Joi.number().integer() }).when(Joi.object({ 'a': Joi.number().valid(5), 'b': Joi.number().valid(10), }), {then: Joi.any().forbidden()}) var schema_2 = Joi.object({ a: Joi.number().integer(), b: Joi.number().integer() }).when(Joi.object({ 'a': Joi.number().valid(5), }), {then: Joi.any().forbidden()}) var object = { a: 5, b: 10 } schema_1.validate(object) // this throws ValidationError schema_2.validate

Joi validation set default as empty object

南楼画角 提交于 2019-12-11 09:07:04
问题 I'm running into an issue (or what I believe to be one) with Joi validation. I'm trying to assign a value to a non-existent key if it's been passed as part of the request body. So for example: parameters: Joi.object().keys({ keyA: Joi.string().allow('').allow(null).default(null), keyB: Joi.object().keys({ b1: Joi.string(), b2: Joi.string(), b3: Joi.object().keys({ b3_1: Joi.string(), b3_2: Joi.string(), b3_3: Joi.string() }) }).default({}), keyC: Joi.object().keys({ c1: Joi.number(), c2: Joi

Validate two properties are equal

最后都变了- 提交于 2019-12-11 05:58:43
问题 Is it possible to validate that two object properties of type string are equal using Joi ? I found Joi.ref() but I wonder if there's another way of doing it. Especially as Joi.ref() doesn't seem to support any.error() 回答1: Yes, it is possible to check if two properties on a object are the same. And using Joi.ref() is the preferred way to do it. If you want to use custom error messages the Joi.any.messages() option works the best. The Joi.any.messages() lets you overwrite the different error

hapi route joi validation of password confirmation

不问归期 提交于 2019-12-10 03:59:37
问题 How do I check that password and password_confirmation are the same ? var Joi = require('joi'), S = Joi.string().required().min(3).max(15); exports.create = { payload: { username: S, email: Joi.string().email(), password: S, password_confirmation: S } } 回答1: You can use Joi.any().valid() with Joi.ref(): password: Joi.string().min(3).max(15).required(), password_confirmation: Joi.any().valid(Joi.ref('password')).required().options({ language: { any: { allowOnly: 'must match password' } } }) 来源

How to capture callback from failed validation in Joi

你。 提交于 2019-12-08 04:54:49
问题 We're building a web service using Hapi. Our routes have some validation. I was wondering if it was possible to capture or override the default callback on failed validation, before or after hapi replies to the client. my (non-working) code: { method: 'GET', config: { tags: tags, validate: { params: { id: Joi.number() .required() .description('id of object you want to get'), }, //Tried this, and it's not working: callback: function(err, value) { if (err) { console.log('need to catch errors

Are there ways to populate a schema in Joi from another schema (like Mongoose does)?

时光毁灭记忆、已成空白 提交于 2019-12-08 03:38:17
问题 I'm currently only using Joi to validate and building schemas. However I feel like I'm missing features like reference another schema like Mongoose does. Or is the only way to do this by using both (or mongoose only)? I want to use something similar to this: const mongoose = require('mongoose'); const Schema = mongoose.Schema; const personSchema = Schema({ _id: Schema.Types.ObjectId, name: String, age: Number, stories: [{ type: Schema.Types.ObjectId, ref: 'Story' }] }); const storySchema =

Are there ways to populate a schema in Joi from another schema (like Mongoose does)?

安稳与你 提交于 2019-12-06 15:35:16
I'm currently only using Joi to validate and building schemas. However I feel like I'm missing features like reference another schema like Mongoose does. Or is the only way to do this by using both (or mongoose only)? I want to use something similar to this: const mongoose = require('mongoose'); const Schema = mongoose.Schema; const personSchema = Schema({ _id: Schema.Types.ObjectId, name: String, age: Number, stories: [{ type: Schema.Types.ObjectId, ref: 'Story' }] }); const storySchema = Schema({ author: { type: Schema.Types.ObjectId, ref: 'Person' }, title: String, fans: [{ type: Schema