title | section | prev | next | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
string |
docs |
|
|
The string
type is used for strings of text. It may contain Unicode characters.
[tabs-start "Language-specific info"]
[tab "Python"]
In Python, "string" is analogous to the unicode
type on Python
[tab "Ruby"] In Ruby, "string" is analogous to the String type.
[tab "Objective-C"]
In Objective-C, "string" is analogous to the NSString
type.
[tab "Swift"]
In Swift, "string" is analogous to the String
type.
[tabs-end]
// props { "isSchema": true }
{ "type": "string" }
// props { "indent": true, "valid": true }
"Déjà vu"
// props { "indent": true, "valid": true }
""
// props { "indent": true, "valid": true }
"42"
// props { "indent": true, "valid": false }
42
The length of a string can be constrained using the minLength
and
maxLength
keywords. For both keywords, the value must be a
non-negative number.
// props { "isSchema": true }
{
"type": "string",
"minLength": 2,
"maxLength": 3
}
// props { "indent": true, "valid": false }
"A"
// props { "indent": true, "valid": true }
"AB"
// props { "indent": true, "valid": true }
"ABC"
// props { "indent": true, "valid": false }
"ABCD"
The pattern
keyword is used to restrict a string to a particular
regular expression. The regular expression syntax is the one defined in
JavaScript (ECMA 262
specifically) with Unicode support. See
Regular Expressions for more information.
When defining the regular expressions, it's important to note that the string is considered valid if the expression matches anywhere within the string. For example, the regular expression
"p"
will match any string with ap
in it, such as"apple"
not just a string that is simply"p"
. Therefore, it is usually less confusing, as a matter of course, to surround the regular expression in^...$
, for example,"^p$"
, unless there is a good reason not to do so.
The following example matches a simple North American telephone number with an optional area code:
// props { "isSchema": true }
{
"type": "string",
"pattern": "^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$"
}
// props { "indent": true, "valid": true }
"555-1212"
// props { "indent": true, "valid": true }
"(888)555-1212"
// props { "indent": true, "valid": false }
"(888)555-1212 ext. 532"
// props { "indent": true, "valid": false }
"(800)FLOWERS"