-1

I m trying to understand Json schema definition of below snippet,

  "translated_string": {
    "description": "A translated string",
    "type": "string",
    "minLength": 1,
    "not": {
      "type": "string",
      "pattern": "^\\s+$"
    }
  },

Questions:

  1. what does \\s represents.

  2. some explanation of snippet by giving some examples valid schema.

Mistalis
  • 17,793
  • 13
  • 73
  • 97
Arun G
  • 1,678
  • 15
  • 17
  • `\\s` represents an [escaped](https://stackoverflow.com/a/17597400/4607733) space metacharacter (see [Shorthand Character Classes](http://www.regular-expressions.info/shorthand.html)). What can't you understand about the snippet? – logi-kal Jun 22 '17 at 12:42
  • @horcrux some explanation of snippet by giving some examples for valid schemas and not valid schemas – Arun G Jun 22 '17 at 12:54
  • "^\\s+$" is "Any non empty string that is only whitespace", but the validator is taking the NOT of that (so anything BUT that) – Tezra Jun 22 '17 at 12:58
  • Why down vote for the question? – Arun G Jun 22 '17 at 15:13

1 Answers1

3

In regex syntax, \s is a metacharacter representing a whitespace (see Shorthand Character Classes). In some languages you need to double escape it because of the slash (so \\s).

Lets analyze the regex:

  • ^ is the begin of the string
  • \\s is the space
  • + is an operator meaning "one or more"
  • $ is the end of the string

So the pattern matches the strings composed only of at least one space and nothing more.

Let's now analyze the snippet:

  • "description" is just the description associated to the translated_string property
  • "type": "string" means that in the JSON you can only use a string value for the translated_string property
  • "not" means that the following is not permitted:
    • "pattern": "^\\s+$" is the regex above (so it must not be matched)

Note that the snippet is equivalent to:

"translated_string": {
  "description": "A translated string",
  "type": "string",
  "not": {
    "type": "string",
    "pattern": "^\\s*$"
  }
},

(where the * operator means "zero or more")
or even simplier:

"translated_string": {
  "description": "A translated string",
  "type": "string",
  "pattern": "^\\S+$"
},

(where \\S is anything but a space)

So, the following JSON are correct:

"translated_string": "l"
"translated_string": " l"
"translated_string": " l "
"translated_string": "  l "

While the following JSON are not correct:

"translated_string": ""
"translated_string": " "
"translated_string": "  "
"translated_string": 1
"translated_string": [ SOMETHING ]
"translated_string": { SOMETHING }
logi-kal
  • 7,107
  • 6
  • 31
  • 43