brandur / json_schema

A JSON Schema V4 and Hyperschema V4 parser and validator.
MIT License
230 stars 45 forks source link

Add customizable format validators #39

Closed nornagon closed 8 years ago

nornagon commented 8 years ago

Usage:

JsonSchema.configure do |c|
  c.register_format "the-answer", ->(s) { s.to_i == 42 }
end
# { "type": "string", "format": "the-answer" } now matches the string
# "42xx" but not "43xx".

I'd prefer a way to configure this on a per-parser/per-validator level, but I wasn't sure where the data about the set of format validators should go, given that the information needs to be available to both the parser (to check that the format is a valid one) and the validator (to know how to actually validate the data).

Fixes #38.

brandur commented 8 years ago

@nornagon Hey Jeremy, this looks great! Thanks for the contribution. I left a couple minor comments, but let's take it forward.

I'd prefer a way to configure this on a per-parser/per-validator level, but I wasn't sure where the data about the set of format validators should go, given that the information needs to be available to both the parser (to check that the format is a valid one) and the validator (to know how to actually validate the data).

Yeah ... what we might be able to do is if this becomes a problem is allow configuration objects to be instantiated individually and injected into validator instances. We could just leave the global version around as well for backwards compatibility.

This probably isn't necessary quite yet though; I bet most users will only need a single configuration.

nornagon commented 8 years ago

Done & rebased :)

brandur commented 8 years ago

@nornagon Thanks again!

brandur commented 8 years ago

Released as 0.10.0.