from Typing import Optional
class PostCreateSchema(BaseModel):
contact_phone:Optional[constr(strip_whitespace=True, min_length=8,max_length=10)]
I specified the variable to be Optional but when I submitted the form without filling in the contact_phone field I am still getting the Error "contact_phone: ensure this value has at least 8 characters"
If I remove 'min-length' constraint, it will work as intended and I can submit with an empty field.
How can I make the field optional but yet have a minimum value if filled?
You just need to specify a default value:
class PostCreateSchema(BaseModel):
contact_phone: Optional[constr(strip_whitespace=True, min_length=8,max_length=10)] = None
Related
I am trying to add optional dictionary and list to my keyword, how can assign their default values?
In the RF User Guide you can find examples of how you can add arguments with default values. In short, after the variable you have to add "=" and the value by default. Here is an example:
One Required And One With Default
[Arguments] ${required} ${optional}=default
[Documentation] This keyword takes 1-2 arguments
Log Required: ${required}
Log Optional: ${optional}
While the other answer covers correctly keyword parameters with defeat values, your question is for a default list one.
And this is not allowed by Robot Framework syntax (and in python, having a default value being a predefined list is a source of a lot of pain and hidden bugs :). So the solution for that is to have a parameter which default value is None (usually, really anything the caller would not pass would do), and check was it set in the call; if not - assign it to the default list, inside the keyword:
My Keyword That Accepts Optional List
[Arguments] ${the_list}=${None}
${the default value}= Create List member0 the other one
${the_list}= Set Variable If $the_list is None ${the default value} # no argument passed when the keyword was called
... ${the_list} # an argument was passed, leave the variable as it was
I am using asp (JScript as my language) and working with getting data from a form that is sent using POST.
Specifically, I have a text input and I want to check if it was left empty. When leaving it empty, and including Response.Write(Request.form('opt2Dur')) in the called page, nothing prints (doesn't print null or undefined).
My thought was that it was just an empty string so I included this: Response.Write(Request.form('opt2Dur') === ''), however this printed false.
It will print true if I use Response.Write(Request.form('opt2Dur') == '') (== not ===). What is the true value that I can check against using ===? Or, in this case will it be sufficient to check with just ==?
Thanks for any help.
In scripting languages with "flexible" types and default values it's very easy to get confused with actual data types.
The actual type of each Request item (both QueryString and Form, it doesn't matter) is some sort of Array as it also supports more than one form element with the same name submitted to the ASP handler. It's mentioned in the documentation as well:
The Form collection is indexed by the names of the parameters in the request body. The value of Request.Form(element) is an array of all the values of element that occur in the request body.
Since the === also take into account type, it will return false in your case as array is not a string.
I wasn't able to find the exact actual type and reproduce it with local variable (it's not any plain array) so if you are keen on using the strict comparison operator, check the Count:
Response.Write(Request.Form('opt2Dur').Count === 0);
If entering a label when a value is specified is redundant do you have to specify the label? Do you just enter a blank string, null value or what?
The Google Docs for _trackEvent() recommend using undefined for optional parameters as needed.
So I have a String I need to check for a valid format for which I used a pattern, and whether the entered value is valid according to some business rules for which I used a custom validator. Now the customer would prefer it if the error message associated with the latter is only shown if the former didn't occur. But as far as I know there is no way to make this distinction.
Or can anyone think of a way to do this?
The trick is: have two different constraints (annotations),
one for the pattern, that accept null,
and a second for not null.
By default the javax.validation.constraints.Pattern "validator" accept null.
Accepts String. null elements are considered valid.
(javax.validation.constraints.Pattern javadoc)
So what you need to do in the end is this:
#NotNull(message="{validation.notNull}")
#Pattern(pattern="123" message="{validation.notPattern}")
String myString;
Added:
(Comment)
Not really the desired behaviour since I want to reuse the bean object but the conditions aren't the same for each page. – Jack Nickels 5 mins ago
In this case you can use the so called groups (See JSR303 Bean Validation Spec, Chapter 4.1.2 groups -- there is also an example). Use the default Group for #Pattern and an other group for #NotNull. Now you can enable or disable the Validation rules according the groups you specify for validation.
validator.validate(myObject, Default.class);
validator.validate(myObject, MyGroup.class);
There is one problem left: in Spring 3.0 you can not Specify the Group you want to use for the automatic validation process. But in 3.1 you can, according to that Feature Request SPR-6373 (I have not tryed it, but I hope it works)
i am using strut2.1.6 with tomcat 5.5
i have a Date field createDate in my PasswordHistory Bean, and corresponding date component on my "search.jsp" this field is optional - no validation required .
if i submit the form i am get the follwoing error on console
ognl.MethodFailedException: Method "setCreatedDate" failed for object com.security.data.PasswordHistory#d5b561 [java.lang.NoSuchMethodException: setCreatedDate([Ljava.lang.String;)] –
it looks that it is trying to convert the empty string to date, when it fails it tries to search the corresponding String argument method and if it converts the value to date successfully it calls the corresponding Date argument method – Muhammad Shahid
i want to avoid any conversion if the field is emtpy.
Do you have struts.devMode = true in struts.xml? From the docs:
And thirdly, perhaps the setting which is less widely known, and therefore a source of much confusion: it will raise the level of debug or normally ignorable problems to errors. For example: when you submit a field which cannot be set on an action 'someUnknownField', it will normally be ignored. However, when you're in development mode, an exception will be thrown, telling you an invalid field was submitted. This is very useful for debugging or testing large forms, but can also be confusing if you're relying on parameters in your request that are not set on the action, but which you are using directly in your view layer (warning: bad practice, you should always validate input from the web).
http://struts.apache.org/2.1.6/docs/devmode.html