Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions src/schema_engine/dialects.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,14 @@ end

dialect(::Bool; default::Dialect = DRAFT7) = default

# Long names and Unicode's extra aliases (`digit`, `cntrl`, `punct`, `Combining_Mark`) of
# the General_Category values. PCRE knows only the short names.
const ECMA_GENERAL_CATEGORIES = Dict(
"Cased_Letter" => "L&",
"Combining_Mark" => "M",
"cntrl" => "Cc",
"digit" => "Nd",
"punct" => "P",
"Close_Punctuation" => "Pe",
"Connector_Punctuation" => "Pc",
"Control" => "Cc",
Expand Down Expand Up @@ -282,12 +288,28 @@ function _ecma_property(property::AbstractString)
return String(property)
end

# ECMA-262 `\s`: WhiteSpace (tab, vertical tab, form feed, every Zs, U+FEFF) and
# LineTerminator (LF, CR, U+2028, U+2029). PCRE without UCP matches only the ASCII ones.
# Julia compiles with ALT_BSUX, so code points use the JavaScript `\\uhhhh` form.
const ECMA_WHITESPACE = "\\t\\n\\x0B\\f\\r\\p{Zs}\\u2028\\u2029\\uFEFF"

function _ecma_whitespace(escaped::Char, in_class::Bool)
if in_class
# PCRE cannot subtract a set inside a class, so `[\S]` keeps its ASCII meaning.
return escaped == 's' ? ECMA_WHITESPACE : "\\S"
end
return escaped == 's' ? "[$ECMA_WHITESPACE]" : "[^$ECMA_WHITESPACE]"
end

function _ecma_pattern(pattern::AbstractString)
io = IOBuffer()
in_class = false
i = firstindex(pattern)
stop = lastindex(pattern)
while i <= stop
if pattern[i] != '\\'
pattern[i] == '[' && (in_class = true)
pattern[i] == ']' && (in_class = false)
write(io, pattern[i])
i = nextind(pattern, i)
continue
Expand All @@ -301,7 +323,15 @@ function _ecma_pattern(pattern::AbstractString)
write(io, '\\')
end
isodd(slashes) || continue
i <= stop && pattern[i] in ('p', 'P') || continue
i <= stop || continue
if pattern[i] in ('s', 'S')
seek(io, position(io) - 1)
truncate(io, position(io))
write(io, _ecma_whitespace(pattern[i], in_class))
i = nextind(pattern, i)
continue
end
pattern[i] in ('p', 'P') || continue
property_type = pattern[i]
brace = nextind(pattern, i)
brace <= stop && pattern[brace] == '{' || continue
Expand All @@ -320,4 +350,10 @@ function _ecma_pattern(pattern::AbstractString)
return String(take!(io))
end

_ecma_regex(pattern::AbstractString) = Regex(_ecma_pattern(pattern))
# ECMA-262 semantics on PCRE: `$` matches only at the very end, never before a trailing
# newline (DOLLAR_ENDONLY), and `\d`, `\w`, `\b` are ASCII (no UCP). `\p{...}` still
# matches Unicode properties, which needs only UTF mode.
const ECMA_COMPILE_OPTIONS = (Base.DEFAULT_COMPILER_OPTS & ~Base.PCRE.UCP) | Base.PCRE.DOLLAR_ENDONLY

_ecma_regex(pattern::AbstractString) =
Regex(_ecma_pattern(pattern), ECMA_COMPILE_OPTIONS, Base.DEFAULT_MATCH_OPTS)
18 changes: 18 additions & 0 deletions test/schema_engine/compiled.jl
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,24 @@ end
Dict("pattern" => "[");
dialect = SchemaEngine.DRAFT202012,
)

# ECMA-262 `pattern` semantics, which PCRE's defaults do not follow. The official
# suite's optional/ecmascript-regex.json covers this fully (OPENAPI_SCHEMA_SUITE=all).
ecma(pattern) = SchemaEngine.CompiledSchema(
Dict("pattern" => pattern);
dialect = SchemaEngine.DRAFT202012,
)
@test !isvalid(ecma("^abc\$"), "abc\n")
@test isvalid(ecma("^\\d\$"), "7")
@test !isvalid(ecma("^\\d\$"), "\u07c0")
@test !isvalid(ecma("^\\w\$"), "é")
@test isvalid(ecma("^\\s\$"), "\u00a0")
@test isvalid(ecma("^\\s\$"), "\ufeff")
@test isvalid(ecma("^\\s\$"), "\u2029")
@test !isvalid(ecma("^\\S\$"), "\u2029")
@test isvalid(ecma("^[\\s]\$"), "\u3000")
@test isvalid(ecma("^\\p{digit}+\$"), "\u07c0\u0663")
@test isvalid(ecma("^\\p{Letter}cole\$"), "école")
@test_throws SchemaEngine.CompilationError SchemaEngine.CompiledSchema(
Dict("contains" => Dict(), "minContains" => "invalid");
dialect = SchemaEngine.DRAFT202012,
Expand Down
17 changes: 15 additions & 2 deletions test/schema_engine/official.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ function _official_schema_suite_dir()
)
end

function _test_compiled_draft(directory, retriever, schema_dialect, base_uri)
files = sort(filter(name -> endswith(name, ".json"), readdir(directory)))
function _test_compiled_draft(
directory,
retriever,
schema_dialect,
base_uri;
files = sort(filter(name -> endswith(name, ".json"), readdir(directory))),
)
@testset "$(file)" for file in files
groups = JSON.parsefile(joinpath(directory, file))
@testset "$(group["description"])" for group in groups
Expand Down Expand Up @@ -78,6 +83,14 @@ end
schema_dialect,
"http://localhost:1234/$name/root",
)
# JSON Schema requires ECMA-262 `pattern` semantics; PCRE differs by default.
_test_compiled_draft(
joinpath(suite, name, "optional"),
retriever,
schema_dialect,
"http://localhost:1234/$name/root";
files = ["ecmascript-regex.json"],
)
end
end
end
Loading