Skip to content

Extend describe() for type#1028

Merged
Marsup merged 4 commits intohapijs:masterfrom
lamchakchan:extend_describe_for_type
Nov 10, 2016
Merged

Extend describe() for type#1028
Marsup merged 4 commits intohapijs:masterfrom
lamchakchan:extend_describe_for_type

Conversation

@lamchakchan
Copy link
Contributor

For the API object.type(). I'm extending the meta data storage of the constructor within the schema to be placed in options. Also extending any.describe() to project it to a new item schema within the rules array. This will help facilitate an object generation of the exact type specified by this API contract. Problem right now is that the type is translated to a name of type String and the ability to reproduce via Object.create() is lost since there is no way to cast it back to the original reference. Support is being added within felicity

Let me know if there is a better way of implementing this.

Using the Joi API

Joi.object().type(RegExp);

Storage within the schema

{
    _test: [{
        name: 'type',
        arg: 'RegExp',
        options: {
            ctor: [Function RegExp]  //This is new
        }
    }]
}

Within the Transform of any.describe()

{
    rules: [{
        name: 'type',
        arg: 'RegExp',
        ctor: [Function RegExp] //This is new
    }]
}

@lamchakchan lamchakchan changed the title Extend describe for type Extend describe() for type Nov 6, 2016
@AdriVanHoudt
Copy link
Contributor

felicity looks really cool 👍

@lamchakchan
Copy link
Contributor Author

Thank you!

lib/object.js Outdated
}

return this.createError('object.type', { type: name }, state, options);
}, {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the place to put it, the 2nd argument of _test holds the describe data.

@Marsup Marsup added the feature New functionality or improvement label Nov 6, 2016
@Marsup Marsup self-assigned this Nov 6, 2016
@Marsup Marsup added this to the 10.0.0 milestone Nov 6, 2016
@lamchakchan
Copy link
Contributor Author

Thank you @Marsup for reviewing this so quickly. I have some follow up questions.

I want to clarify that 2nd argument of _test currently is being passed name [String]. I can do one of 2 things. Replace that usage of name with the value of ctor. Or make it so that the second argument is an object containing both like this:

{
   name : <name value>,
   ctor : [Function <type>]
}

Your advice is greatly appreciated.

@Marsup
Copy link
Collaborator

Marsup commented Nov 6, 2016

The latter should be fine.

@lamchakchan
Copy link
Contributor Author

Just to follow up, I checked in new changes based on the recommendation of changes.

@Marsup Marsup merged commit aff2b4f into hapijs:master Nov 10, 2016
@Marsup
Copy link
Collaborator

Marsup commented Nov 10, 2016

Thanks.

@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 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

Successfully merging this pull request may close these issues.

3 participants