Skip to content

Commit

Permalink
ignore and if not required
Browse files Browse the repository at this point in the history
  • Loading branch information
metakeule committed Apr 3, 2012
1 parent 9c6b948 commit 3db6933
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lib/and.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ class type._and extends type.Base

afterValue: ->
@validate()
for sc in @_valid_schemas
@_value = sc.val(@_value).val()
if @_valid_schemas
for sc in @_valid_schemas
@_value = sc.val(@_value).val()
@

validateChild: (err, callback) ->
Expand Down
10 changes: 6 additions & 4 deletions lib/and.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions test/and.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ describe("type", function() {
ok( !schema.validate() );
});

it("should validate required if required and embedded in object", function() {
var schema = type.object( { test: type.and(
[ type.string().len(5), type.string().email()]
).required() } );

var errs = schema.val( { test2: ["a"] }).validate( function(errs) {
ok( errs );
equal( errs.messages().length, 1 );
} );
ok( errs );
equal( errs.messages().length, 1 );
});

it("should not validate required if not required and embedded in object", function() {
var schema = type.object( { test: type.and(
[ type.string().len(5), type.string().email()]
) } );

var errs = schema.val( { test2: ["a"] }).validate( function(errs) {
ok( !errs );
} );
ok( !errs );
});

it("should process values for clones", function() {
var sc = schema.clone();
var val = sc.val(" Test@g.com ").val();
Expand Down

0 comments on commit 3db6933

Please sign in to comment.