String
A string type can be defined with the members such as type
, default
, choices
, pattern
, minLen
, maxLen
, len
, optional
, and null
. Schema of the string TypeDef should be written as,
TypeDef Schema
The TypeDef schema ensures the validity of string
MemberDefs.
type
The first member of the typedef is type
. The string can define with a type string
or its derived types i.e email
, url
, datetime
, date
, time
. Here the next snippet shows, how the string type and its derived types can be defined.
default
The second member in the string
typedef is default
. Here is how the default values can be defined for a string.
Rules for default:
The default value is applicable only if no other value is provided for the key.
If for a key, null is set to true then it must be replaced by its default value.
The default value when set must match with the data type of a key.
Choices
The next member in the string
typedef is choices
. If set, the choices must be an array of strings. Here the snippet shows how the choices
can be added to member variables in a string so that it is restricted to the fixed set of available choices.
Pattern
The value of the pattern
must be a String. The string value passed should be a valid Regular Expression. The data will be then validated according to the Regular Expression and passed accordingly. Regular Expression can be defined in the schema by using pattern
in the schema of a string.
Different versions of schema can be created and executed for patterns in the programming environment. But to remain compatible with the host environment, it is better to stick to the constraints described below.
A single Unicode character, other than the special characters specified below matches itself.
(
.
U+002E
): Matches any character except newline character (U+000A
).(
^
U+005E
): Matches only at the start of the string.(
$
U+0024
): Matches only at the end of the string.(
...
): Assembles the sequence of regular expressions into a single regular expression.(
|
U+007C
): Matches the regular expression either preceding or following with the"|"
symbol.[abc]
: Matches any of the characters enclosed by the square brackets.[a-z]
: Matches the range of characters enclosed by the square bracket.[^abc]
: Matches any character not in the list.[^a-z]
: Matches any character out of the given range.(
+
U+002B
): repeats the preceding regular expression one or more times and is greedy as they match as many items as possible.(
*
U+002A
): repeats the preceding regular expression zero or more times and greedy as they match as many items as possible.(
?
U+003F
): makes the preceding regular expression optional. Greedy, matches zero or one preceding regular expression.+?
,*?
,??
: The*
,+
, and?
qualifiers are used to match as much text as possible which is not always desired.(
?!x
), (?=x
): Negative and positive lookahead.{x}
: Match exactly x occurrences of the preceding regular expression.{x,y}
: Match at least x and at most y occurrences of the preceding regular expression.{x,}
: Match x or more occurrences of the preceding regular expression.{x}?
,{x,y}?
,{x,}?
: Lazy versions of the above expressions.
maxLen
The value of maxLen
must be a non-negative integer. The string instance is valid only if the number of characters in the string will be less than or equal to the value of maxLen
. Here is the snippet showing how to assign maxLen.
minLen
The value of minLen
must be a non-negative integer. The string instance is valid only if the number of characters in the string will be greater than or equal to the value of minLen
. Here is the snippet showing how to assign minLen.
len
The value of length represented as len
must be a non-negative integer. The string instance is valid only if the number of characters in the string will be equal to the value of len
. The code snippet shows how to assign len
.
Thelen
have the highest precedence over minLen
and maxLen
constraints. When the len
is set, the implementation must ignore minLen
and maxLen
constraints.
optional
The member of a string type can be set to optional. Here is the code snippet that demonstrates how a string can be set to optional.
null
A string when set to null: true
will accept null values. The snippet below shows how to set a nullable string.
Examples
Here are some of the examples that demonstrate how to define string member definition.
In the above snippet, name
can be kept optional
and null
. When no value is passed for the name then, its default value is set to anonymous
. The name
should be a string containing characters from a to z (upper or lower case) with a minimum length of 5 and a maximum length of 50.
Here the code snippet shows that the users can only select the department provided in choices
i.e input is restricted to the set of available departments.
In the above code snippet, users can select the location provided in choices
i.e the input is restricted to the set of available locations ( locations are enclosed in double-quotes to pass numeric data as string ).
Last updated