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

Rework method signature to allow just argumentName as an option #47

Closed
wants to merge 7 commits into from

Conversation

terodox
Copy link
Contributor

@terodox terodox commented Aug 11, 2020

Addresses #46

I took a stab at an implementation that would address the desire for a default error message without breaking the existing function contracts.

  • Change is tested locally
  • Demo is updated to exercise change (if applicable)
  • [WIP] flag is removed from the title

@terodox terodox marked this pull request as ready for review August 11, 2020 19:47
src/index.spec.js Outdated Show resolved Hide resolved
@terodox terodox requested a review from mattquinlan440 August 18, 2020 14:16
mattquinlan440
mattquinlan440 previously approved these changes Aug 18, 2020
Copy link

@mattquinlan440 mattquinlan440 left a comment

Choose a reason for hiding this comment

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

This looks good to me :shipit:

@terodox terodox changed the title Add two new methods to allow a logical default error message based on parameterName Rework method signature to allow just argumentName as an option Aug 20, 2020
@terodox
Copy link
Contributor Author

terodox commented Aug 20, 2020

@jtheriault Do you have any additional feedback? Or can this be merged?

src/index.js Outdated Show resolved Hide resolved
Co-authored-by: Joe Theriault <[email protected]>
export function coerce(value, Type, messageOrOptions) {
let errorMessage = messageOrOptions;
if (typeof messageOrOptions !== 'string') {
errorMessage = `${messageOrOptions.parameterName} should be coerceable to ${Type.name}. Provided value: ${JSON.stringify(value, null, 2)}`;
Copy link
Member

Choose a reason for hiding this comment

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

I think I'd rather flip this so that the default value is the parameter name and it can be overridden with an options object.

Despite technically maintaining the same signature, it would constitute a breaking change -- but I think for the better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure I completely follow. Can you show me example of the two method signatures your thinking of?

Copy link
Member

@jtheriault jtheriault Oct 20, 2021

Choose a reason for hiding this comment

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

Picking this back up...

On deeper reflection, I think this is almost perfect as a pre-major change. Specifically I think this should be followed immediately by a new major version where the third parameter:

  • is an optional object
  • by default, implies an errorMessage of Could not coerce a valid ${Type.name} from provided value: ${JSON.stringify(value, null, 2)}
  • can have parameterName property which changes message to Could not coerce ${parameterName} into a valid ${Type.name} from provided value: ${JSON.stringify(value, null, 2)}
  • can haveerrorMessage property overrides the message entirely
  • parameterName and errorMessage are mutually exclusive

If we think typing parameterName and/or errorMessage is too much, we could introduce short aliases of e for errorMessage and p for parameterName.

The only piece missing in the current PR would be to allow errorMessage to be specified as a property on the options object and not just as a backwards-compatability fallback string.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like the approach. I would argue that we should avoid the churn and just move to a new major version though.

I'm not sure we'd be helping a whole lot of people with the minor then major.

What do you think of that approach?

Copy link
Member

Choose a reason for hiding this comment

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

I tend to prefer "ripping off the band-aid" to deprecating an API, but it's a common approach so it seemed responsible to suggest it 😉.

Considering parameter validation on the options object will direct the consumer's migration effort, I'm comfortable with just taking the leap.

That said, I think it would behoove us to survey some of our colleagues for their views to make sure we're not out-of-step.

Choose a reason for hiding this comment

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

I love the optional object as the third parameter.

@terodox terodox closed this Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants