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

Creating multiple extensions for the same base type #1047

Closed
tomyam1 opened this issue Nov 24, 2016 · 4 comments
Closed

Creating multiple extensions for the same base type #1047

tomyam1 opened this issue Nov 24, 2016 · 4 comments
Assignees
Labels
feature New functionality or improvement
Milestone

Comments

@tomyam1
Copy link

tomyam1 commented Nov 24, 2016

I created two extensions for the number type. Once for file sizes and one for durations.

The end goal is being able to coherce a configuration file like { ttl: '1 minute', maxSize: '1MB' } to { ttl: 60000, maxSize: 1000000 }

It seems Joi keeps only the last extension for every name.

All the extensions I saw, e.g. 1, 2 and 3, are using a base type such as number or date as the name so I figured this is the correct approach.

So my question is how to create multiple extensions for a single base type?

Here is an example showing my issue:

// npm install joi joi-filesize-extensions joi-duration-extensions
const BaseJoi = require('joi');
const FilesizeExtensions = require('joi-filesize-extensions');
const DurationExtensions = require('joi-duration-extensions');

const FilesizeFirstJoi = BaseJoi.extend([ FilesizeExtensions, DurationExtensions ]);
console.log(`typeof FilesizeFirstJoi.number().filesize: ${typeof FilesizeFirstJoi.number().filesize}`);
console.log(`typeof FilesizeFirstJoi.number().msDuration: ${typeof FilesizeFirstJoi.number().msDuration}`);

const DurationFirstJoi = BaseJoi.extend([ DurationExtensions, FilesizeExtensions ]);
console.log(`typeof DurationFirstJoi.number().filesize: ${typeof DurationFirstJoi.number().filesize}`);
console.log(`typeof DurationFirstJoi.number().msDuration: ${typeof DurationFirstJoi.number().msDuration}`);

Outputs:

typeof FilesizeFirstJoi.number().filesize: undefined
typeof FilesizeFirstJoi.number().msDuration: function

typeof DurationFirstJoi.number().filesize: function
typeof DurationFirstJoi.number().msDuration: undefined
@Marsup Marsup added non issue Issue is not a problem or requires changes and removed non issue Issue is not a problem or requires changes labels Nov 24, 2016
@Marsup
Copy link
Collaborator

Marsup commented Nov 24, 2016

I'll have to think about a way out of this one, this is a classical inheritance model, so declaring the base you indeed inherit from it, not from the one extended by the previous extension. The behavior is expected but probably not what you would expect.

@Marsup Marsup self-assigned this Mar 18, 2017
@Marsup Marsup added the request label Mar 18, 2017
@Marsup Marsup added this to the 10.3.0 milestone Mar 18, 2017
@Marsup Marsup closed this as completed in 49a0c76 Mar 18, 2017
@Marsup
Copy link
Collaborator

Marsup commented Mar 18, 2017

Took some time, hope you're still around ;) I'll release soon-ish.

@tomyam1
Copy link
Author

tomyam1 commented Mar 22, 2017 via email

@tomyam1
Copy link
Author

tomyam1 commented Aug 15, 2017

Hey @Marsup
Finally got to it.
Seems like using a factory function solves the issue.

Made a new extension using this paradigm: joi-phone-number-extensions.

@hueniverse hueniverse added feature New functionality or improvement and removed request labels Sep 19, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Mar 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

3 participants