Skip to content

Commit 4aa5dac

Browse files
authored
feat: update integer, number and string rules - allow primitives as root types (#862)
Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent 08b59b5 commit 4aa5dac

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

pkg/grammar/json_schema.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ var (
1515

1616
PRIMITIVE_RULES = map[string]string{
1717
"boolean": `("true" | "false") space`,
18-
"number": `[0-9]+ space`, // TODO complete
19-
"integer": `[0-9]+ space`, // TODO complete
20-
"string": `"\"" [ \t!#-\[\]-~]* "\"" space`, // TODO complete
21-
"null": `"null" space`,
18+
"number": `("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? space`,
19+
"integer": `("-"? ([0-9] | [1-9] [0-9]*)) space`,
20+
"string": `"\"" (
21+
[^"\\] |
22+
"\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F])
23+
)* "\"" space`,
24+
"null": `"null" space`,
2225
}
2326

2427
INVALID_RULE_CHARS_RE = regexp.MustCompile(`[^a-zA-Z0-9-]+`)
@@ -176,6 +179,9 @@ func (sc *JSONSchemaConverter) visit(schema map[string]interface{}, name string,
176179
if !exists {
177180
panic(fmt.Sprintf("Unrecognized schema: %v", schema))
178181
}
182+
if ruleName == "root" {
183+
schemaType = "root"
184+
}
179185
return sc.addRule(schemaType, primitiveRule)
180186
}
181187
}

pkg/grammar/json_schema_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ root ::= root-0 | root-1
4848
space ::= " "?
4949
root-0-arguments ::= "{" space "\"date\"" space ":" space string "," space "\"time\"" space ":" space string "," space "\"title\"" space ":" space string "}" space
5050
root-1 ::= "{" space "\"arguments\"" space ":" space root-1-arguments "," space "\"function\"" space ":" space root-1-function "}" space
51-
string ::= "\"" [ \t!#-\[\]-~]* "\"" space
51+
string ::= "\"" (
52+
[^"\\] |
53+
"\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F])
54+
)* "\"" space
5255
root-1-function ::= "\"search\""`
5356
)
5457

0 commit comments

Comments
 (0)