gregsdennis / Manatee.Json

A fully object-oriented approach to JSON manipulation, validation, and serialization that focuses on modeling the JSON structure rather than mere string parsing and conversion.
MIT License
198 stars 32 forks source link

Date format validation #241

Closed lmenzel closed 4 years ago

lmenzel commented 4 years ago

Describe the bug

  1. Date format validation accepts invalid dates, e.g. 2019-99-99. (Format.cs, line 131)
  2. Validation is not overridable, as stated in the documentation, because trying to do so ends up with "System.ArgumentException : An item with the same key has already been added. Key: date" as the key will not be replaced in _lookup but instead added. (Format.cs, line 172 and 186)

Expected behavior Invalid dates are rejected Overriding Formats replaces existing format validations

Idea It would be nice, if it was possible to override validations in a non-static way, e.g.

var format = new Format() {....}
schema.Validate(jsonValue, format); 
gregsdennis commented 4 years ago
  1. You ~can~ should be able to provide your own implementation, but can't because of (2).
  2. This is definitely a bug. Look for an update to fix. Honestly, I'm not sure that the format should self-register like that. I should probably have an explicit Register(Format) method.

I'm not going to make Format a separate parameter on the Validate() call. I probably need to make the JsonSchemaOptions into an instance class an pass that around with the context. I can then put a format dictionary on the context as well. I'll consider the implications around this.

gregsdennis commented 4 years ago

In the interest of unblocking you, I'm going to release v12.0.1 that is just a fix for overriding the format in its existing state.

Another thing I noticed is that unless you actually set Formats.Date that property will hold onto the old format, though your override will be used because it registers in the constructor. This is all just wrong, and I need to rework it.

gregsdennis commented 4 years ago

@lmenzel I've reworked how format validation works. This should allow you to override any format quite easily. Have a look at #268 to see the changes. I haven't done any docs on it yet.