joi validation: Set minimum array length conditionally - node.js

I have an array field which i would like to ensure that it has at least one element when a condition is met:
genre:Joi.array().includes(data.genres).when('field'{is:'fieldValue',then:Joi.required()})
If i changed the 'then' field with Joi.required().min(1), it complains.
Can i do this with Joi?

You didn't mention what the error message was, but testing your code I suppose you got:
TypeError: Object [object Object] has no method 'min'
This error occurs because min() is a function of the array type. In the then: part you create a new validation object and Joi doesn't know you expect an array there. So you need to specify it:
then: Joi.array().min(1).required()
The full validation code is:
genre: Joi.array().includes(data.genres).when('field', {is: 'fieldValue', then: Joi.array().min(1).required()})

Related

Error: Incorrect parameter type for operator '<'. Expected Boolean, received Text

Here, I'm creating a new formulae field(text) based on the checkboxes. My requirement is if Attended, Canceled, Confirmed, Invited, Pending are unchecked show invited, if the confirmed checkbox is checked before the start date, The text to be populated as "confirmed". But, I'm getting error there. How can we compare those both. Any help
IF(AND(Attended__c ,Canceled__c ,Confirmed__c , Invited__c ,Pending__c),"False","Invited",
IF(OR(Confirmed__c,Confirmed__c<Start_Date_Time__c),"TRUE", "Confirmed",
IF((AND(Waiting_List__c,(!Confirmed__c)),"TRUE","Waitlisted","")))
The IF function is not set up correctly as written. The set up for the function is =IF(condition_is_met, value, value_if_not_met), so to have multiple IF's nested together, you must set it up like so: =IF(condition1_met, value, IF(condition2_met, value, value_if_condition2_not_met))
Right now you have it set up like =IF(condition1_met, value, value_if condition1_not_met, IF(condition2_met, value, value, value_if_condition2_not_met...

Azure Logic App condition - Property contains in object within an array

value is an array with objects that have a property called skuPartNumber (string). How do I make a condition that is true when there are any objects where the skuPartNumber is equal to "X" in the array.
For your requirement, you can use contains function to implement it easily. As your screenshot shows, but need to make some changes.
First, you need to know the expression of value. It seems the value comes from "Parse JSON" in your logic app. So the expression of value should be like body('Parse_JSON')?['value']. Then use a string() function to convert it to string, then judge if it contains "skuPartNumber":"x".
The expression is string(body('Parse_JSON')?['value']).
I think the solution above is easy enough, but if you don't want to think of it as a string to judge if it contains "skuPartNumber":"x". You can also loop the value array, get each item and judge if the field skuPartNumber equals to x. Do it like below screenshot:
After the "For each" loop, use a "If" condition to judge if the variable result equals true or false.

ElasticSearch datatype for a concrete value

I've encountered following error message when I've set Nested data type for such input ['abc', 'def', 'ghi'].
object mapping for [tags] tried to parse field [null] as object, but
found a concrete value
Please let me know which kind of datatype should I set for concrete value.
If it's an array of strings then use text datatype. if it's an array of objects use Nested datatype. See reference https://www.elastic.co/guide/en/elasticsearch/reference/current/array.html. the reason you've received this error it's because you've tried to index array of strings (concrete values) as Nested datatype but Nested datatype expects to see an object and not a string (concrete value). Also check this out https://discuss.elastic.co/t/object-mapping-for-configurationitems-configuration-state-tried-to-parse-field-state-as-object-but-found-a-concrete-value/80995

Hapi/Joi Validation For Number Fails

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 above mentioned code I get an error
ValidationError: child "v2" fails because ["v2" must be a number]
as per the documentation when we tries to pass any numeric value as a string it converts the values to number but here in this case my value is 13. which is not able to convert into number and throwing an error.
Is there any way by which we can convert this value to 13.0
You can use a regex in order to match numbers with a dot, for instance:
Joi.string().regex(/\d{1,2}[\,\.]{1}/)
And then combine both validations using Joi.alternatives:
Joi.alternatives().try([
Joi.number().empty("").allow(null),
Joi.string().regex(/\d{1,2}[\,\.]{1}/)
])
However, I think you may need to convert the payload to number using Number(string value). You need to check the payload type, if it isn't a Number, you need to convert it.
If you want to know more about the regex used in the example, you can test it in here: https://regexr.com/

Allow string to be null or empty in joi and express-validation

I am using "joi": "10.0.6" and "express-validation": "1.0.1" in my project to validate a JSON object which I will save in the DB.
One of the field is a string but it could be null or empty.
How can I allow my string to pass the validation in the cases it is empty or null?
I have already tried these cases:
dataId: Joi.string().allow(null).allow('').optional()
and
dataId: Joi.alternatives().try(Joi.string(), Joi.allow(null))
but both them don't work.
You could also specify this with a comma delimited list.
dataId: Joi.string().allow(null, '')
See Joi documentation on allow(value)
It's very simple, suppose the field is data then you could impose the validation as
data: Joi.string().allow('')
allowing empty string to pass,
for multiple string validation it could also be done as
data: Joi.string().allow('', null)
allowing both empty as well as null value for the data field to pass.
If you want to allow the empty string and treat it the same as undefined (i.e., convert it to undefined), then use Joi's empty method:
data: Joi.string().empty('')
Joi.string().allow('').allow(null) should have worked. I tested locally and it works fine.
Show your test cases and errors?
If you need only null or empty string use this:
dataId: Joi.string().valid(null, '')
If you just specify it as
dataId: Joi.string()
it should be optional per default.
EDIT: Could not try it on my own currently. Here an alternative, if above does not work:
dataId: Joi.string().min(0).allow('').allow(null)

Resources