Skip to content

Instantly share code, notes, and snippets.

@swannodette
Forked from nijikokun/example-user.js
Created May 4, 2012 11:39
Show Gist options
  • Save swannodette/2594296 to your computer and use it in GitHub Desktop.
Save swannodette/2594296 to your computer and use it in GitHub Desktop.
Beautiful Validation... Why have I never thought of this before?!

At a certain point you just want pattern matching.

(defn validates-credentials [username password]
(let [uc (count username)
pc (count password)]
(match [username uc password pc]
[(:or nil "") _ _ _] {:error "No username given" :field "name"}
[_ _ (:or nil "") _] {:error "No password given" :field "pass"}
[_ (_ :guard #(< % 3)) _ _] {:error "Username less than 3 characters" :field "pass"}
[_ _ _ (_ :guard #(< % 4))] {:error "Password less than 4 characters" :field "pass"}
[#"^([a-z0-9-_]+)$" _ _ _] {:error "Username contains invalid characters" :field "name"}
:else true)))
var user = {
validateCredentials: function (username, password) {
return (
(!(username += '') || username === '') ? { error: "No Username Given.", field: 'name' }
: (!(username += '') || password === '') ? { error: "No Password Given.", field: 'pass' }
: (username.length < 3) ? { error: "Username is less than 3 Characters.", field: 'name' }
: (password.length < 4) ? { error: "Password is less than 4 Characters.", field: 'pass' }
: (!/^([a-z0-9-_]+)$/i.test(username)) ? { error: "Username contains invalid characters.", field: 'name' }
: false
);
}
};
var results = user.validateCredentials('Nijikokun','somepassword');
console.log(results);
@mattdeboard
Copy link

What does the JS look like that the ClojureScript version compiles down to?

@swannodette
Copy link
Author

ClojureScript is meant to be compiled via GClosure. So the kind of code that GClosure outputs, a bunch of ternary expressions last time I checked.

@rodrigoalvesvieira
Copy link

Why is that about GClosure? Only because ifs are more verbose?

@swannodette
Copy link
Author

Probably.

@apg
Copy link

apg commented May 4, 2012

I'm not sure I like pattern matching for this use case. Suppose you add another parameter, say, because the requirements change such that you need to verify the password. You can create a guard to ensure that both passwords match, but now I have to maintain all the other patterns by adding _ and keeping track of those. At some point, you just want composable functions.

@swannodette
Copy link
Author

I don't see how verifying a password requires another another pattern. You test the password, and it's either equal to the thing you want to verify or not.

At some point you just want composable rules - core.logic.

@apg
Copy link

apg commented May 5, 2012

Sure, but now you have error handling outside of the match statement, and you've added an if statement, etc. Validation is tricky. Pattern matching doesn't make it any less so.

@swannodette
Copy link
Author

Don't disagree. But for simple cases like this pattern matching is fine. For something more sophisticated ... yes, you need something more sophisticated :)

@nijikokun
Copy link

this is pretty gouda :3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment