karenetheridge / JSON-Schema-Tiny

Validate data against a schema, minimally
https://metacpan.org/release/JSON-Schema-Tiny/
Other
3 stars 1 forks source link

Feature request: relaxed type validation #7

Closed mjcarman closed 11 months ago

mjcarman commented 2 years ago

It would be very useful to have a "relaxed" mode for type validation that isn't strictly tied to perl's POK/IOK/NOK flags. Users shouldn't be required to have that level of understanding about the Perl internals. Even for users who do have that depth of knowledge, managing those flags is difficult, error prone, and unlikely to be necessary for consuming data. (Using those flags may be necessary for producing data, though in that case I'd rather be able to use the schema to set the flags so it gets serialized properly.)

Desired behavior:

Context: I'm attempting to use JSON Schema with YAML documents, since YAML doesn't have a way to define and validate schemas that's standardized, widespread, and/or actively maintained. As JSON is a subset of YAML, it should be possible to use JSON schema for (typical) YAML documents. YAML documents and parsers, like Perl, don't generally distinguish between scalar types. For example, when parsing { foo: 1 } YAML::XS sets the POK flag, while JSON::XS does not. This impedance mismatch makes it difficult to use this module, as I have to walk the parsed data and do $x += 0 or $value .= '' (with a lot of surrounding guard checks) to set the flags the way the validator wants them. Even then, simply printing a value will set the POK flag and can cause validation to fail.

karenetheridge commented 2 years ago

YAML documents and parsers, like Perl, don't generally distinguish between scalar types. For example, when parsing { foo: 1 } YAML::XS sets the POK flag, while JSON::XS does not.

This is a known shortcoming of that parser which I believe is actively being looked into and fixed. YAML::PP is actually type-correct, which is why I use it in all my applications that use JSON Schema with YAML documents:

my $schema = YAML::PP->new(boolean => 'JSON::PP')->load_string($yaml_text);
# or:
my $schema = YAML::PP->new(boolean => 'JSON::PP')->load_file($filename);
karenetheridge commented 2 years ago

reference: https://github.com/ingydotnet/yaml-libyaml-pm/issues/68

karenetheridge commented 2 years ago

Also good news -- the "number used as string no longer appears to be a string" problem is fixed in the latest perl release, version 5.36.0.

mjcarman commented 2 years ago

Using YAML::PP instead of YAML::XS is a good work-around, thanks. I wish I'd known about that sooner.

the "number used as string no longer appears to be a string" problem is fixed in the latest perl release, version 5.36.0.

Version 5.36 hasn't been released yet. Is that a typo or an upcoming change? At any rate, I've experienced the opposite problem: "number used as a string no longer appears to be a number" (as of v5.32.1).

karenetheridge commented 2 years ago

Version 5.36 hasn't been released yet. Is that a typo or an upcoming change? At any rate, I've experienced the opposite problem: "number used as a string no longer appears to be a number" (as of v5.32.1).

Yes, it will be fixed in the upcoming 5.36.0 release (May 2022), but development releases from 5.35.7 onwards also contain the fix. It fixes things both ways - things that started off as numbers will stay numbers, and things that started off as numbers will stay numbers.

karenetheridge commented 11 months ago

version 0.022 also supports numeric validation of strings that look like numbers.