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

Allow custom types instead string when format is specified #418

Closed
remcohaszing opened this issue Feb 17, 2017 · 2 comments
Closed

Allow custom types instead string when format is specified #418

remcohaszing opened this issue Feb 17, 2017 · 2 comments

Comments

@remcohaszing
Copy link

What version of Ajv you are you using?
4.11.3

What problem do you want to solve?
I would like to be able to validate custom types using the format option. I'd like to create a JavaScript API where developers can use data types as defined by the Swagger specification.

Because the API is a JavaScript API, I'd like to also accept more types than just JSON values. I want to be able to accept any type, and convert it to a representing basic value.

For example, I would like these to work:

// Convert the date to iso8606 before validation, or even simply allow Date objects.
ajv.validate({type: 'string', format: 'date-time'}, new Date())
ajv.validate({type: 'string', format: 'date'}, new Date())

// Convert the blob to base64 or a binary string before validation, or even simply allow Blob objects.
ajv.validate({type: 'string', format: 'byte'}, new Blob())
ajv.validate({type: 'string', format: 'binary'}, new Blob())

I'm able to accomplish this using Python jsonschema,but not using any JavaScript library I could find.

What do you think is the correct solution to problem?
The most efficient way would be to allow alternative types (other than string) when a format is specified.

Will you be able to implement it?
I might be, I'll have to dive into the library code.

@epoberezkin
Copy link
Member

epoberezkin commented Feb 17, 2017

You already can define custom formats for "number" type in version 5.0.2-beta, also see migration guide. Not sure what you mean by "custom types", but for all other custom validation scenarios, including asynchronous, you can use custom keywords. See docs and also ajv-keywords package.

@epoberezkin
Copy link
Member

epoberezkin commented Feb 17, 2017

It's the same as #291

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants