The JSON::Validator provides three primary methods for validation. All methods accept two required arguments: the schema (a Ruby object, JSON string, or file path) and the data to validate (a Ruby object, JSON string, or file path). An optional third argument for options is also accepted.
validate: Returns true if validation passes, false otherwise.validate!: Raises a JSON::Schema::ValidationError if validation fails.fully_validate: Returns an array of error messages (strings) if validation fails, or an empty array if it passes.
By default, the validator uses JSON Schema Draft 4. You can specify older drafts using the :version option (e.g., :draft1, :draft2, :draft3) or by using the $schema attribute within the schema itself.
require "json-schema"
schema = { "type" => "object", "required" => ["a"], "properties" => { "a" => {"type" => "integer"} } }
# Returns boolean
JSON::Validator.validate(schema, { "a" => 5 })
# Raises JSON::Schema::ValidationError
JSON::Validator.validate!(schema, { "a" => "taco" })
# Returns array of error strings
JSON::Validator.fully_validate(schema, { "a" => "taco" })