Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

presence, custom and email validators

  • Loading branch information...
commit 2923dcafe4541bbb462f3227dcaca1080b1c3612 1 parent 9497e2e
@dnagir authored
View
8 README.md
@@ -105,11 +105,11 @@ class @Page extends ko.Model
@presence 'name', {message: 'give me a name, yo!'}
# Conditional validation - use the `page` model passed in as argument
- @presence 'name' unless page.id?
+ @presence 'name', if: -> @persisted?
# Custom inline validation
- @custom 'name', (valueAccessor, options) ->
- if (valueAccessor() || '').indexOf('funky') < 0 then "should be funky" else null
+ @custom 'name', (page) ->
+ if (page.name() || '').indexOf('funky') < 0 then "should be funky" else null
```
It is recommended to avoid custom inline validations and create your own validators instead:
@@ -125,7 +125,7 @@ ko.Validations.validators.funky = (model, field, options) ->
so that you can use it like so:
```coffee
-validates: (page) ->
+validates: ->
funky 'name', {word: 'yakk'}
```
View
1  lib/assets/javascripts/knockout/model.js.coffee
@@ -1,5 +1,6 @@
#=require jquery
#=require knockout/validations
+#=require knockout/validators
# Module is taken from Spine.js
moduleKeywords = ['included', 'extended']
View
17 lib/assets/javascripts/knockout/validations.js.coffee
@@ -58,20 +58,3 @@ Validations =
true
ko.Validations = Validations
-
-ko.Validations.validators =
-
- presence: (model, field, options) ->
- val = model[field]()
- isBlank = !val or val.toString().isBlank()
-
- if isBlank then options.message || "can't be blank" else null
-
- email: (model, field, options) ->
- if model[field]()? then "should be valid email" else null
-
- custom: (model, field, options) ->
- # Treat options as a mandatory callback
- options.call(model, model)
-
-
View
20 lib/assets/javascripts/knockout/validators.js.coffee
@@ -0,0 +1,20 @@
+#= require knockout/validations
+
+ko.Validations.validators =
+
+ presence: (model, field, options) ->
+ val = model[field]()
+ isBlank = !val or val.toString().isBlank()
+
+ if isBlank then options.message || "can't be blank" else null
+
+ email: (model, field, options) ->
+ val = model[field]()
+ isValid = !val or val.toString().match /.+@.+\..+/
+ unless isValid then options.message or "should be a valid email" else null
+
+ custom: (model, field, options) ->
+ # Treat options as a mandatory callback
+ options.call(model, model)
+
+
View
1  spec/javascripts/knockout/validations_spec.js.coffee
@@ -35,4 +35,3 @@ describe "Validations", ->
it "should join all the errors", ->
@subject.multiple ''
expect(@subject.errors.multiple()).toBe "xxx, xxx, xxx"
-
View
70 spec/javascripts/knockout/validators_spec.js.coffee
@@ -0,0 +1,70 @@
+describe "ko.Validations.validators", ->
+ isErrorFor = (validator, val, options={}) ->
+ model =
+ name: ko.observable(val)
+ ko.Validations.validators[validator](model, 'name', options)
+
+
+
+ describe "presence", ->
+ isError = (val, options={}) -> isErrorFor('presence', val, options)
+
+ it "should not allow all kinds of bllanks", ->
+ expect(isError null).toBeTruthy()
+ expect(isError undefined).toBeTruthy()
+ expect(isError undefined).toBeTruthy()
+ expect(isError "").toBeTruthy()
+ expect(isError " ").toBeTruthy()
+ expect(isError " ").toBeTruthy()
+
+ it "should allow non-blanks", ->
+ expect(isError '123').toBeFalsy()
+ expect(isError ' x ').toBeFalsy()
+ expect(isError 123).toBeFalsy()
+
+ it "should have default message", ->
+ expect(isError '').toBe "can't be blank"
+
+ it "should use custom message", ->
+ expect(isError '', message: 'xxx').toBe 'xxx'
+
+
+
+ describe "email", ->
+ isError = (val, options={}) -> isErrorFor('email', val, options)
+
+ it "shold allow blanks", ->
+ expect(isError null).toBeFalsy()
+ expect(isError undefined).toBeFalsy()
+ expect(isError '').toBeFalsy()
+
+ it "shold not allow non email-ish values", ->
+ expect(isError ' ').toBeTruthy()
+ expect(isError 123).toBeTruthy()
+ expect(isError "xyz").toBeTruthy()
+ expect(isError "abc.com").toBeTruthy()
+ expect(isError "@").toBeTruthy()
+ expect(isError "a@b").toBeTruthy()
+
+ it "should allow email-ish values", ->
+ expect(isError 'a@b.c').toBeFalsy()
+ expect(isError 'a.b@c.d').toBeFalsy()
+
+ it "should have default message", ->
+ expect(isError "xx").toBe "should be a valid email"
+
+ it "should use custom message", ->
+ expect(isError 'xx', message: 'xxx').toBe 'xxx'
+
+
+
+ describe "custom", ->
+ isError = (fn) -> isErrorFor('custom', 'val', fn)
+
+ it "should return the custom result", ->
+ expect(isError -> "hi").toBe "hi"
+
+ it "should accept args", ->
+ isError (model) ->
+ expect(model.name()).toBe 'val'
+
Please sign in to comment.
Something went wrong with that request. Please try again.