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

coerce arguments order is incorrect #2152

Closed
NikhilNanjappa-zz opened this issue Sep 26, 2019 · 4 comments
Closed

coerce arguments order is incorrect #2152

NikhilNanjappa-zz opened this issue Sep 26, 2019 · 4 comments
Assignees
Labels
documentation Non-code related changes

Comments

@NikhilNanjappa-zz
Copy link

NikhilNanjappa-zz commented Sep 26, 2019

Context

  • node version: 10.4.0
  • joi version: 16.1.1
  • environment (node, browser): node
  • used with (hapi, standalone, ...): hapi
  • any other relevant information: No

What are you trying to achieve or the steps to reproduce ?

Following the new doc for extensions and some examples in the release notes, I'm trying to coerce values as shown in the below example.

The issue is that, the order of the arguments seems to be wrong. It says its supposed to coerce(schema, value, helpers) but what I actually get is coerce(value, schema, ...). Mentioned below the actual value it returns.

Itseems to behave as per previous version. Maybe I'm doing something wrong ?

const baseJoi = require('@hapi/joi16');
const Joi = baseJoi.extend(require('./friendly-number'));

const schema = Joi.friendlyNumber().required();
schema.validate('1,000');

The friendly-number code looks like:

module.exports = function(joi) {
    return {
        type: 'friendlyNumber',
        base: joi.number(),
        coerce(schema, value, helpers) {
            console.log(schema); // this returns the value => 1,000
            console.log(value); // this returns the schema => "value { original: '1,000', prefs: ..."
            console.log(helpers); // this returns undefined
        }
    };
};

Which result you had ?

Logging the schema argument gave me the value and value gave me schema and helpers gave me undefined.

What did you expect ?

schema, value, helpers to log its own values.

@Marsup Marsup self-assigned this Sep 26, 2019
@Marsup Marsup added the documentation Non-code related changes label Sep 26, 2019
@Marsup Marsup closed this as completed in d07ca05 Sep 26, 2019
@Marsup
Copy link
Collaborator

Marsup commented Sep 26, 2019

I'm not sure I understand the relationship with v15 but documentation is now fixed.

@NikhilNanjappa-zz
Copy link
Author

NikhilNanjappa-zz commented Sep 26, 2019

Thanks. Edited the title to avoid ambiguity.

I'm also a bit confused relating to another issue with same coerce function. The result of schema.validate('1,000').value returns a string instead of converting it to a number. My code looks like (I used the syntax from the release notes instead of the API doc):

        ...
        coerce: {
            from: 'string',
            method(value) {
                if (isString(value)) {
                    // Strip out any non-numeric characters (eg. ,) but keep decimal points
                    return { value: value.replace(/[^0-9.]/g, '') };
                }
                return { value };
            }
        }

Am I missing something @Marsup ?

@NikhilNanjappa-zz NikhilNanjappa-zz changed the title coerce arguments order is according to v15 ? coerce arguments order is incorrect Sep 26, 2019
@Marsup
Copy link
Collaborator

Marsup commented Sep 26, 2019

You should use prepare for that. Same as your 1st try but coerce ➡️ prepare. It's not well documented currently but it'll come.

@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
documentation Non-code related changes
Projects
None yet
Development

No branches or pull requests

2 participants