davishmcclurg / json_schemer

JSON Schema validator. Supports drafts 4, 6, 7, 2019-09, 2020-12, OpenAPI 3.0, and OpenAPI 3.1.
MIT License
408 stars 64 forks source link

validate_schema does not report regex errors #194

Closed Farjaad closed 3 months ago

Farjaad commented 3 months ago

The following raises a RegexpError instead of reporting that the regex is invalid

schema_definition = {
   "type" => "string",
    "pattern" => "/(/",
}
schema = JSONSchemer.schema(schema_definition)
errors = schema.validate_schema.to_a

Result: end pattern with unmatched parenthesis: /\/(\// (RegexpError)

davishmcclurg commented 3 months ago

Unfortunately, this is a known issue: https://github.com/davishmcclurg/json_schemer/issues/167#issuecomment-1974909924

JSONSchemer.schema still doesn't support invalid schemas (and likely won't). I don't want every schema object to have to take the hit of validating the provided schema before parsing. When working with schemas that are potentially invalid, it's best to check them with JSONSchemer.valid_schema? first.

Now that I think of it, JSONSchemer::Schema#valid_schema? and JSONSchemer::Schema#validate_schema were probably mistakes to begin with—may want to deprecate in the future.

Please use JSONSchemer.valid_schema? (or JSONSchemer.validate_schema) instead:

schema_definition = {
    "type" => "string",
    "pattern" => "/(/",
}

JSONSchemer.validate_schema(schema_definition).map { _1["error"] }
# ["value at `/pattern` does not match format: regex"]