How to check a value in joi against a set of values - joi

flag: Joi.number().integer().min(0).max(5).positive().required(),
headId: Joi.string().when('flag',{is:[0,1,2] , then: Joi.string().required(),otherwise:Joi.string()})
How to perform this type of validation in Joi?
This way of checking flag value is incorrect.I want to check if flag is between 0-2 and then only headId will be required.

Related

How do reset groupFirstKey QueueConfiguration field to default value (null) via ActiveMQServerControl.updateQueue(String queueConfiguration)

I created a queue via the method ActiveMQServerControl.createQueue(String queueConfiguration) with a value for the groupFirstKey.
How to reset groupFirstKey field of QueueConfiguration this queue to default value (null) via ActiveMQServerControl.updateQueue(String queueConfiguration)?
If I set "" for groupFirstKey in the String queueConfiguration (for example {"name":"MAXC","address":"MAXC","routing-type":"ANYCAST","group-first-key":""}), then for the groupFirstKey value I get not null, but "", respectively.
Currently there is no way to do this via the management interface (i.e. using the ActiveMQServerControl) because ActiveMQServerControl.updateQueue(String queueConfiguration) ultimately performs a check on the members of the JSON input that is passed in, and any members that are null (i.e. don't exist) are not updated.
However, if you're using an embedded server then you can use the updateQueue(QueueConfiguration, boolean) method directly on org.apache.activemq.artemis.core.server.ActiveMQServer and pass true for the boolean to force any null parameters to be used for the update.

How to set variable value after "Print when Expression" from textfield returned true

Using iReport I have a textfield with following "Print When Expression":
new Boolean($F{data}.doubleValue()<3 && $V{check}.intValue()==0);
My problem is that I only want to print this textfield once. For that I thought I can use a variable (check).
I tried to use this expression for the variable:
($F{data}.doubleValue()<3) ? 1 : 0
The problem is that the expression for the variable is called before the textfield checks its "print condition" and so the textfield never get printed.
How can I achieve that the variable value is set AFTER the textfield "print condition" returned true?
I want to mark the first value which is smaller than a reference value
I often use the parameter map (P{REPORT_PARAMETERS_MAP}) to store and evaluate custom expression depending on previous values and outputs
Example (return true if value $F{X}<100 and it has never been below before)
<printWhenExpression>
<![CDATA[$F{X}.doubleValue()<100?$P{REPORT_PARAMETERS_MAP}.put("X_LESS_100",true)==null?true:false:false]]>
</printWhenExpression>
This expression leverage the fact that Map.put(K,V) returns the previous value associated with key, that is null if never called, while second time $F{X} is below 100 it will return our previous set value that is true.
Note: you do not need to define any variable.
I found a workaround, but I don't find it a very good way, because my solution is related to the evaluation time of the conditions from the textfield and the variable.
So if anybody has a nice solution please post it.
I set the initial value of my check variable to 0.
The variable expression looks like this:
($F{data}.doubleValue() >= 3)? 0 : ($F{data}.doubleValue() < 3 && $V{check}.intValue()==0) ? 2 : 1;
The "Print When Expression" from the textfield is:
new Boolean($V{check}.intValue()==2);

Default Value Expression for Optional String Input iReport

I'm using PostgreSQL as DBMS, in my current report I need an optional String parameter to get records by Id, which is a String field.
So I set the Default Value Expression to:
($P{Param} == null || $P{Param}.equals("")) ? "" : "AND id='" + $P{Param} + "'"
When the field is empty the report is created without issues, but when I enter a valid Id the compiler complains:
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "160E0"   Position: 3126
Just like if I was adding double quotes after and before the value I'm passing. Somebody know how to handle this problem when using String values?
I guess, you've created one Parameter, named Param and in your query you have $P!{Param}. Thing is, default value is set only when parameter stays NULL after prompting, so when you input your ID, ${Param} value is passed into query as it was inputed by user (without AND id=' part).
Try creating second parameter, lets name it $P{input}; set its default value to your expression, "Use as a prompt" value to false. Pass it to your query ($P!{input}). Now, you have your ${Param} for prompting and when it's value is set to the desired ID, $P{input} is set to your query condition.

Zend framework - how to allow empty field for form element

I'm using this construction for my element:
$freetext = $this->CreateElement('textarea', 'freetext')
->setLabel('Comments')
->setAttrib('class','input-textarea')
->setOptions(array('rows' => '2', 'cols'=>'30'))
->addValidator('StringLength', false, array(0,500))
->addFilter('HtmlEntities')
->addFilter('StripTags')
->setRequired(true);
I want to add an "allowEmpty" to this but can't find the correct syntax. I was hoping for something like:
... ->addValidator('allowEmpty', false, true)
But this does not work.
Edit: I've changed the setRequired() to true - I want to allow empty string as an acceptable value on a require field.
Regardless of usage, how do I add this option to my element?
->setRequired(false);
this is enough if you want to allow an empty string and save an empty string to database.
if you want the field to be optional and keep null value in database if nothing is given, add:
->addFilter(new Zend_Filter_Null)
$freetext = $this->CreateElement('textarea', 'freetext')
->addValidator('StringLength', false, array(10,500))
->setRequired(false);
Your code should already do that, the setRequired(false) method do what you're asking for, i.e. if the value is not submitted then validators won't be run.
Do you have any issue with the code you've written, some validation error messages or something else?
Update
I've changed the setRequired() to true - I want to allow empty string as an acceptable value on a require field.
What is the semantic in setRequired(true) and allowing the empty string as a valid value? Or better what do you require if the element can be empty?
What you've asked in the edit is a no sense, because if an element is required it MUST have a value different from the empty string. If you need to accept the empty string as a valid value just use setRequired(false). When you get form values with Zend_Form::getValues() or Zend_Form_Element::getValue() you'll obtain the empty string as result.
Anyway here it's the explanation of setRequired and setAllowEmpty from ZF manual:
Using the defaults, validating an Element without passing a value, or
passing an empty string for it, skips all validators and validates to
TRUE.
setAllowEmpty(false) leaving the two other mentioned flags
untouched, will validate against the validator chain you defined for
this Element, regardless of the value passed to isValid().
setRequired(true) leaving the two other mentioned flags untouched,
will add a 'NotEmpty' validator on top of the validator chain (if none
was already set)), with the $breakChainOnFailure flag set. This
behavior lends required flag semantic meaning: if no value is passed,
we immediately invalidate the submission and notify the user, and
prevent other validators from running on what we already know is
invalid data.
If you do not want this behavior, you can turn it off by passing a
FALSE value to setAutoInsertNotEmptyValidator($flag); this will
prevent isValid() from placing the 'NotEmpty' validator in the
validator chain.

how should I set <PrintWhenExperssion> value true or false from java code

I want to set
<printWhenExpression>![CDATA[Boolean.valueOf(true)]]</printWhenExpression>
to Boolean.valueOf(false) when I fill the report according to some conditions. This means I want to set value 'false' in <printWhenExpression> at run time. Can any one help me for this?
Instead of having hard coded value Boolean.valueOf(true)/Boolean.valueOf(false) use a variable which is evaluation to a boolean:
Boolean.valueOf(someVariableOfTypeBoolean)
or
Boolean.valueOf(someMethodThatReturnsBoolean())