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

Make ajv chainable #625

Closed
pithu opened this issue Nov 21, 2017 · 8 comments
Closed

Make ajv chainable #625

pithu opened this issue Nov 21, 2017 · 8 comments

Comments

@pithu
Copy link
Contributor

pithu commented Nov 21, 2017

Thank you for providing this great and awesome library.

I would like to ask for the following improvement.

What do you think about making some ajv method returning this, that would allow to write more compact code by chaining methods. For example the following code would be possible:

const validator = new Ajv()
    .addSchema(someSchema)
    .addMetaSchema(someMetaSchema)
    .addFormat('customeType', customValidator)
    .getSchema('customUri')

if (!validator(someJsonObject)) { // fail }

As far as i can see candidates for chaining would be addSchema, addMetaSchema, addFormat, addKeyword.

@epoberezkin
Copy link
Member

Looks good. And drop “add”? :)

@epoberezkin
Copy link
Member

Also removeSchema and removeKeyword can be chained, although they aren’t used too often.
@pithu if you do the PR please update .d.ts file as well

pithu added a commit to pithu/ajv that referenced this issue Nov 22, 2017
According to issue ajv-validator#625 this enables crazy things like
`new Ajv().addSchema(mySchema).validate(schema, data)`
@epoberezkin
Copy link
Member

I am now thinking whether renaming all methods (maybe leaving old names as aliases) is a good idea:

addSchema -> schema
addMetaSchema -> metaSchema
addFormat -> format
addKeyword -> keyword
getSchema -> get
removeSchema -> remove

@pithu
Copy link
Contributor Author

pithu commented Nov 22, 2017

I am fine with the current naming prefix add* and remove*, because imo it's more explicit and better readable.

@epoberezkin
Copy link
Member

in 5.5.0

@pithu
Copy link
Contributor Author

pithu commented Nov 26, 2017

cool, thx for merging

@isTravis
Copy link
Contributor

isTravis commented Dec 2, 2017

Forgive me if I've misunderstood the implementation of this, but it seems that addSchema is not chainable if an array of schemas is submitted.

I made a quick pull-request: #640.

@pithu
Copy link
Contributor Author

pithu commented Dec 2, 2017

You are right, i forgot the this at that place

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

No branches or pull requests

3 participants