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
I created two extensions for the
numbertype. 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
numberordateas 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:
Outputs: