Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot validate date-time via the CLI tool #378

Closed
mrshll1001 opened this issue Jan 18, 2024 · 4 comments
Closed

Cannot validate date-time via the CLI tool #378

mrshll1001 opened this issue Jan 18, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@mrshll1001
Copy link

mrshll1001 commented Jan 18, 2024

If a schema specifies a string in the format of date-time, the CLI tool will allow any string value to pass validation. The tool should instead declare the value as not a date-time and the instance should fail the validation.

The date-time format is defined as a format in JSONSchema 2020-12 Section 7.3.1.

Validation occurs correctly for the date format but not date-time. For example, the following schema and instance files:

schema.json

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.org/test-date.json",
  "title": "Check Date Validation",
  "description": "A simple schema designed to check datetime and date validation in the check-jsonschema CLI application",
  "type": "object",
  "properties": {
	"thisShouldBeADate": {
		"type": "string",
		"format": "date",
		"title": "This Should Be A Date",
		"description": "This field should contain a date but not a datetime"
	},
	"thisShouldBeADatetime": {
		"type": "string",
		"format": "date-time",
		"title": "This Should Be A Datetime",
		"description": "This field should contain a datetime but dates should not validate."
	}

  },
  "required": [ "thisShouldBeADate","thisShouldBeADatetime" ]
}

instance.json:

{
  "thisShouldBeADate": "not a date",
  "thisShouldBeADatetime": "not a datetime"
}

with the following command:

check-jsonschema --schemafile schema.json instance.json

returns the following results:

Schema validation errors were encountered.
  example.json::$.thisShouldBeADate: 'not a date' is not a 'date'

If I change the value of thisShouldBeADate to a valid date, but leave the value of thisShouldBeADatetime as a random string, the entire file passes validation.

System configuration (if it helps debugging):

  • OS: Devuan 5 (basically Debian 12)
  • Python version: 3.11.2
  • Installation method: pipx (v1.1.0)

EDIT: forgot to mention, https://www.jsonschemavalidator.net/ produces the expected behaviour when given the above schema and instance, hence my raising the issue here.

Thank you for your time and work on this otherwise incredible tool.

@sirosen
Copy link
Member

sirosen commented Jan 18, 2024

Hi, thanks for the report!

This is part of a broader issue which may deserve a rethink.
jsonschema has optional dependencies for format checking, since some formats have nice 3rd party implementations and are complex to check. You can install jsonschema[format] to get those optional dependencies (jsonschema docs on format extras), but today check-jsonschema doesn't pull them in by default.

If you are using pipx, I believe the right magic is...

pipx inject check-jsonschema 'jsonschema[format]'

and you'll have a fuller suite of formats enabled.

I don't really want to change the posture about those package dependencies being optional (because having loosely vetted dependencies increases the chances that we get into dephell issues). However, some formats like date-time are relatively easy to implement, so check-jsonschema could start to include these formats out of the box.

I'm pretty sure that pipx inject will get you what you want, I'm going to leave this open while I think about this.

@sirosen sirosen added the enhancement New feature or request label Jan 18, 2024
@mrshll1001
Copy link
Author

Thanks for the timely and incredibly useful response! You're right, this worked a treat and gets me the desire behaviour.

It'd be great to get some documentation about this on the docs site at https://check-jsonschema.readthedocs.io/en/latest/ if appropriate. Even just signposting to the relevant jsonschema format extras page.

Should I have a go at this and open a merge request or leave it to yourselves as the existing maintainers?

@sirosen
Copy link
Member

sirosen commented Jan 19, 2024

I'd be super-happy to look at a docs contribution. Maybe under the FAQ page? I'm not sure where else would be easy to find.

I still want to think about building in this behavior or otherwise improving on the status-quo too.

@sirosen
Copy link
Member

sirosen commented Feb 6, 2024

As of v0.28.0 (just released), I am now including format support for time and date-time built into check-jsonschema, so no additional library installs are needed in order to verify these formats.

I looked into implementing more formats, and might be open to it in the future, but I'm starting with just these two for now. (Some of the formats require quite a lot of work to parse.)

Please let me know if you see any issues with the new behavior, and thanks again for opening this issue!

@sirosen sirosen closed this as completed Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants