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

Breaking: Return undefined or null to signal failed validation #10

Merged
merged 1 commit into from Jul 21, 2017

Conversation

phated
Copy link
Member

@phated phated commented Jul 20, 2017

@erikkemperman I've made the null or undefined change I was talking about in #8 but I'm just now noticing that we don't actually do any null/undefined validation upon getting the result of a coercer. Was this intentional?

@@ -49,7 +49,9 @@ var isEnabled = normalize(['string', 'boolean'], true);
// Provide a function as first argument to do custom coercion
var now = new Date();
var enabledSince = normalize(function(value) {
return value.constructor === Date ? value : null;
if (value.constructor === Date) {
Copy link
Member

Choose a reason for hiding this comment

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

Why not just return value.constructor === Date ? value : undefined; ? And come to think of it, wouldn't instanceof Date be better in case someone extends that class?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't like ternaries and undefined is the return value if we don't return anything. The only reason the docs had the ternary was due to requiring a null return.

Copy link
Member

Choose a reason for hiding this comment

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

Huh, that's not something I've heard about ternaries before? Anyway, returning undefined explicitly is slightly odd. In documentation I think it might help to point out what's expected though, but nitpicking (again).

Copy link
Member Author

Choose a reason for hiding this comment

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

@erikkemperman sorry, I didn't mean it about ternaries; I meant that the result of a function call is undefined if it doesn't return anything explicitly. I just don't like the look/mental parse-ability of ternaries (especially ones that return undefined as the "else" case.

@@ -10,39 +10,39 @@ describe('normalize', function() {
var type = 'string';
var value = 'test string';
var result = normalize(type, value);
expect(result).toEqual(value);
expect(result).toBe(value);
Copy link
Member

Choose a reason for hiding this comment

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

I had been wondering about this as well, we definitely want to do strict comparisons here.

Copy link
Member Author

Choose a reason for hiding this comment

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

I prefer equals always but wasn't going to nitpick (this is from your changeset).

Copy link
Member

Choose a reason for hiding this comment

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

Ah, sorry, yes, that's what got me wondering about this -- sorry, I made this a while back :-)

I respectfully disagree about equals, preferring strict comparison where applicable -- I guess that touches at the core of our differing ideas here, in a way.

In any event, for the change I was going for it was quite essential (testing that the rejection signal was properly changed from null to defined). But if the contract will be that null and undefined should be treated interchangably by the users of this lib, equals would be fine of course.

@phated
Copy link
Member Author

phated commented Jul 21, 2017

@erikkemperman what remains to be discussed here?

@erikkemperman
Copy link
Member

Not much I guess!

Sorry for being so verbose, I felt I hadn't been explaining myself properly and might have overcompensated slightly :-)

@phated
Copy link
Member Author

phated commented Jul 21, 2017

No worries, I like that we can discuss the changes in-depth.

I'll get this merged and released as 3.0

@phated phated merged commit 4c4ff01 into master Jul 21, 2017
@phated phated deleted the undefined branch July 21, 2017 20:36
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.

None yet

2 participants