how to validate joi sub-schema in best way - javascript

Is it possible to validate joi schema without getting casting error? i.e. I have N fields but I want to validate 1 field only.
I have tried 2 ways, as below:
const Joi = require("joi");
const _ = require('lodash');
const testSchema = Joi.object().keys({
name: Joi.string().trim().min(5).max(25).required(),
allowed: Joi.number().integer().min(0).max(1).default(0)
});
// works smoothly; no error
// const {error, value} = Joi.validate({name :"abc", allowed: 1}, testSchema);
// (Way 1) --> Error: "value" must be a number
// const {error, value} = Joi.validate({name :"abc", allowed: 1}, Joi.reach(testSchema, 'allowed'));
// (Way 2) --> Error: "value" must be a number
const {error, value} = Joi.validate({name :"abc", allowed: 1}, _.find(testSchema._inner.children, {key: 'allowed'}).schema);
console.log(error);
P.S. I know the 3rd approach to compose final schema from smaller schema(s) but I don't want to go for that.

Instead of key value object just pass the value of the key, like:
Joi.validate(1, Joi.reach(testSchema, 'allowed'));

Related

How to forbid a key from object

Seems like a no brainer but I can't figure how to add a constraint to forbid the existence of a key in Joi, please how do I do this.
const data = {foo: 'xyz', bar: '123'};
const schema = {
foo: Joi.string(),
// how do i forbid bar
bar: Joi.forbid()
};
const { error } = Joi.object(schema).validate(data)
Use forbidden: https://joi.dev/api/?v=17.4.2#anyforbidden
const schema = {
a: Joi.any().forbidden()
};
you can simple delete the key from the object :
1st Method : delete data.bar
2nd method : {bar,...schema}=data

Set field max to equals another field (array) min value

I'm currently using joi to valite my data and I want to adchieve the next:
const schema = Joi.object({
arr: Joi.array().min(1).required(),
qty: Joi.number().min(1).max(/* arr.min */)
});
Is there a way to do so? If so, how? I've been trying with:
const schema = Joi.object({
arr: Joi.array().min(1).required(),
qty: Joi.number().min(1).max(Joi.ref('arr'))
});
But it doesn't seem to work.
Thanks in advance!

XOR validation using Joi-browser

I am using joi-browser 13.4.0. In order to generate error message for each input field I am trying to validate fields using .required() like so:
config = {
input1: Joi.string()
.empty("")
.required(),
input2: Joi.string()
.empty("")
.required()
};
schema = Joi.object(this.config).xor("input1", "input2");
But this example is invalid because when input1 or input2 is set to .required(), .xor() function is being ignored. Is there any other way to implement XOR validation without using .xor() method?
Thanks.
You don't need required() if you're using xor:
config = {
input1: Joi.string().empty(""),
input2: Joi.string().empty("")
};
schema = Joi.object(config).xor("input1", "input2");
In fact, using required() like that would never validate. You'd get one of the following error messages:
ValidationError: child "input1" fails because ["input1" is required]
or
ValidationError: "value" contains a conflict between exclusive peers [input1, input2]
Use object.length()
Is there any other way to implement XOR validation without using .xor() method?
Yes, you could for example use the object().length() property to limit the keys in an object to 1.
const Joi = require('joi-browser')
const schema = Joi.object().keys({
input1: Joi.string().empty(''),
input2: Joi.string().empty('')
}).required().length(1);
const value = {
input1: "input1",
};
// this will fail
// const value = {};
// this will fail too
// const value = {
// input1: 'input1',
// input2: 'input2',
// };
const result = Joi.validate(value, schema);
console.log(JSON.stringify(result.error, null, 2));
Be careful
Don't forget to add required() to the parent object, otherwise it is possible to pass undefined to the validation function!
Without required() on the parent it is possible that a simple undefined will pass the validation:
const Joi = require('joi-browser')
const schema = Joi.object().keys({
input1: Joi.string().empty(''),
input2: Joi.string().empty('')
}).length(1); // no required()
const value = undefined; // this will pass validation
const result = Joi.validate(value, schema);
console.log(JSON.stringify(result.error, null, 2));

Joi nested schemas and default values

I'm trying to get Joi to enforce default values on a secondary schema referenced by another. I have two schemas like so:
const schemaA = Joi.object().keys({
title: Joi.string().default(''),
time: Joi.number().min(1).default(5000)
})
const schemaB = Joi.object().keys({
enabled: Joi.bool().default(false),
a: schemaA
})
What I want is to provide an object where a is not defined and have Joi apply the default values for it instead like this:
const input = {enabled: true}
const {value} = schemaB.validate(input)
//Expect value to equal this:
const expected = {
enabled: true,
a: {
title: '',
time: 5000
}
}
The problem is that since the key is optional it is simply not enforced. So what I want is for it to be optional yet properly filled with schemaA defaults if not present. I've been looking through the documentation, but can't seem to find any info on this though I'm probably missing something obvious. Any tips?
Update : April, 2020.
Now, you can use default() in nested objects. Here is the commit in repo with test.
var schema = Joi.object({
a: Joi.number().default(42),
b: Joi.object({
c: Joi.boolean().default(true),
d: Joi.string()
}).default()
}).default();
This should do it:
const schemaA = Joi.object().keys({
title: Joi.string().default(''),
time: Joi.number().min(1).default(5000),
});
const schemaB = Joi.object().keys({
enabled: Joi.bool().default(false),
a: schemaA.default(schemaA.validate({}).value),
});
Although it would be much better if they would implement a feature to let us pass in Joi schema objects for defaults, like so: schemaA.default(schemaA) or schemaA.default('object')

Stripping unknown keys when validating with Joi

I'm using Joi to validate a JavaScript object in the server. The schema is like the following:
var schema = Joi.object().keys({
displayName: Joi.string().required(),
email: Joi.string().email(),
enabled: Joi.boolean().default(false, "Default as disabled")
}).unknown(false);
The schema above will report an error if there is an unknown key in the object, which is expected, but what I want is to strip all the unknown silently, without an error. Is it possible to be done?
You need to use the stripUnknown option if you want to strip the unknown keys from the objects that you are validating.
cf options on https://github.com/hapijs/joi/blob/master/API.md#validatevalue-schema-options-callback
As in Version 14.3.4, there is a simple solution to this issue. Here is the code that solves the problem for you.
// Sample data for testing.
const user = {
fullname: "jayant malik",
email: "demo#mail.com",
password: "password111",
username: "hello",
name: "Hello"
};
// You define your schema here
const user_schema = joi
.object({
fullname: joi.string().min(4).max(30).trim(),
email: joi.string().email().required().min(10).max(50).trim(),
password: joi.string().min(6).max(20),
username: joi.string().min(5).max(20).alphanum().trim()
})
.options({ stripUnknown: true });
// You validate the object here.
const result = user_schema.validate(user);
// Here is your final result with unknown keys trimmed from object.
console.log("Object with trimmed keys: ", result.value);
const joi = require('joi');
joi.validate(object, schema, {stripUnknown:true}, callback);
Here is the current way to include the strip unknown option:
const validated = customSchema.validate(objForValidation, { stripUnknown: true });
If you pass in an objForValidation that has a key which isn't defined in your customSchema, it will remove that entry before validating.

Categories

Resources