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

duration #395

Open
NathanHazout opened this issue Oct 11, 2020 · 7 comments
Open

duration #395

NathanHazout opened this issue Oct 11, 2020 · 7 comments
Labels
enhancement New feature or request

Comments

@NathanHazout
Copy link

NathanHazout commented Oct 11, 2020

Would it be possible to validate string format duration?

See discussion: OAI/OpenAPI-Specification#359

Definition: https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.7.3.1

@cdimascio cdimascio added the enhancement New feature or request label Oct 14, 2020
@cdimascio
Copy link
Owner

@nasht00 i believe you can solve this by using the new formats option

e.g.

formats: [
  {
    name: 'my-three-digit-format',
    type: 'number',
    // validate returns true the number has 3 digits, false otherwise
    validate: (v) => /^\d{3}$/.test(v.toString()),
  },
  {
    name: 'my-three-letter-format',
    type: 'string',
    // validate returns true the string has 3 letters, false otherwise
    validate: (v) => /^[A-Za-z]{3}$/.test(v),
  },
];

Then use it in a spec e.g.

my_property:
  type: string
  format: my-three-letter-format'

Create a format with a validator for duration

@cdimascio
Copy link
Owner

going to close this out. please re-open if this solution does not meet your needs

@NathanHazout
Copy link
Author

I'm sorry I'm confused as to how it would validate an RFC3339 duration ...

What's this 3-letter-format?

@cdimascio
Copy link
Owner

cdimascio commented Jan 3, 2021

3-letter-format is just an example.
You can use the example above as a guide to create your own custom format that validates duration. Basically, make up a name for the duration format e.g duration, then associate it with a custom validate function

@NathanHazout
Copy link
Author

Ah I see. Then yes I know I can write my own custom validators.
However, since duration is now officially supported by the specs of both json-schema and OpenAPI, my feature request was to support it built-in, just like you support date-time today.

@cdimascio
Copy link
Owner

@nasht00 i see. this makes sense. i'll reopen the ticket. ideally ajv will handle this for us, however until then (havne't checked) we could embed a customer validator to handle this by default. if you are up for creating a PR, i'll gladly review.

@cdimascio cdimascio reopened this Jan 5, 2021
@gaston-sureify
Copy link

@cdimascio ajv supports duration validation using the ajv-formats package.
You can import the validation function by doing this:
const valDuration = ajv.compile({type: 'string', format: 'duration'});

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

3 participants