nette/schema

Custom error message mapping

Closed this issue · 5 comments

The error messages created by the library are quite nice (they contain path, expected type and actual values). However in usability in real world APIs (at least at our company) we need programatically to map these error messages to either our error codes (numbers or strings) or custom error messages for example to provide more details, links or just keep consistent with our documentation language.

dg commented

There is also codes in error messages.

Thanks for pushing it forward...

I have tested today and attempted to use it to reimplement our validation structure these our my findings:

  • Better granularity of Nette\Schema\Message::code would be nice as invalid values (such as "12" on int) and min/max violations have the same code schema.unexpectedValue.
  • DateTime (including range) validations would be really nice (though I understand that this is a pain in current PHP). It is however solvable by custom validators.
  • In Nette\Schema\Message the variable assertion has " around its value, it this a purpose? (I see `string(5) ""foo"" in the dump).

Not any of this is a blocker for our purpose, though they make the nice validation structure a bit harder to read due to high number of custom validators.

Feel free to utiilize this feedback or close the issue :-)

My note about readability could be addressed by creating custom Scheme implementations (with custom rules), however from docs I am not sure if it is a supported way we could potentially rely on.

dg commented

@repli2dev I added two new errors Message::OUT_OF_RANGE & Message::LENGTH_OUT_OF_RANGE for min/max violations

ad DateTime: try to create a separate issue on it, so that it is not forgotten

@dg created #36 for the DateTime, thanks for the new codes.