@@ -544,41 +544,83 @@ describe('manipulation', function() {
544544 } ) ;
545545 } ) ;
546546
547- it ( 'should ignore unknown attributes when strict: true' , function ( done ) {
548- // Using {foo: 'bar'} only causes dependent test failures due to the
549- // stripping of object properties when in strict mode (ie. {foo: 'bar'}
550- // changes to '{}' and breaks other tests
551- person . updateAttributes ( { name : 'John' , foo : 'bar' } ,
552- function ( err , p ) {
553- if ( err ) return done ( err ) ;
554- should . not . exist ( p . foo ) ;
555- Person . findById ( p . id , function ( e , p ) {
556- if ( e ) return done ( e ) ;
557- should . not . exist ( p . foo ) ;
558- done ( ) ;
559- } ) ;
547+ it ( 'should discard undefined values before strict validation' ,
548+ function ( done ) {
549+ Person . definition . settings . strict = true ;
550+ Person . findById ( person . id , function ( err , p ) {
551+ p . updateAttributes ( { name : 'John' , unknownVar : undefined } ,
552+ function ( err , p ) {
553+ // if uknownVar was defined, it would return validationError
554+ if ( err ) return done ( err ) ;
555+ Person . findById ( p . id , function ( e , p ) {
556+ if ( e ) return done ( e ) ;
557+ p . name . should . equal ( 'John' ) ;
558+ p . should . not . have . property ( 'unknownVar' ) ;
559+ done ( ) ;
560+ } ) ;
561+ } ) ;
560562 } ) ;
561- } ) ;
563+ } ) ;
564+
565+ it ( 'should allow unknown attributes when strict: false' ,
566+ function ( done ) {
567+ Person . definition . settings . strict = false ;
568+ Person . findById ( person . id , function ( err , p ) {
569+ p . updateAttributes ( { name : 'John' , foo : 'bar' } ,
570+ function ( err , p ) {
571+ if ( err ) return done ( err ) ;
572+ p . should . have . property ( 'foo' ) ;
573+ done ( ) ;
574+ } ) ;
575+ } ) ;
576+ } ) ;
562577
563- it ( 'should throw error on unknown attributes when strict: throw' , function ( done ) {
578+ // Prior to version 3.0 `strict: true` used to silently remove unknown properties,
579+ // now return validationError upon unknown properties
580+ it ( 'should return error on unknown attributes when strict: true' ,
581+ function ( done ) {
582+ // Using {foo: 'bar'} only causes dependent test failures due to the
583+ // stripping of object properties when in strict mode (ie. {foo: 'bar'}
584+ // changes to '{}' and breaks other tests
585+ Person . definition . settings . strict = true ;
586+ Person . findById ( person . id , function ( err , p ) {
587+ p . updateAttributes ( { name : 'John' , foo : 'bar' } ,
588+ function ( err , p ) {
589+ should . exist ( err ) ;
590+ err . name . should . equal ( 'ValidationError' ) ;
591+ err . message . should . containEql ( '`foo` is not defined in the model' ) ;
592+ p . should . not . have . property ( 'foo' ) ;
593+ Person . findById ( p . id , function ( e , p ) {
594+ if ( e ) return done ( e ) ;
595+ p . should . not . have . property ( 'foo' ) ;
596+ done ( ) ;
597+ } ) ;
598+ } ) ;
599+ } ) ;
600+ } ) ;
601+
602+ // strict: throw is deprecated, use strict: true instead
603+ // which returns Validation Error for unknown properties
604+ it ( 'should fallback to strict:true when using strict: throw' , function ( done ) {
564605 Person . definition . settings . strict = 'throw' ;
565606 Person . findById ( person . id , function ( err , p ) {
566607 p . updateAttributes ( { foo : 'bar' } ,
567608 function ( err , p ) {
568609 should . exist ( err ) ;
569- err . name . should . equal ( 'Error' ) ;
570- err . message . should . equal ( 'Unknown property: foo' ) ;
571- should . not . exist ( p ) ;
610+ err . name . should . equal ( 'ValidationError' ) ;
611+ err . message . should . containEql ( '`foo` is not defined in the model' ) ;
572612 Person . findById ( person . id , function ( e , p ) {
573613 if ( e ) return done ( e ) ;
574- should . not . exist ( p . foo ) ;
614+ p . should . not . have . property ( ' foo' ) ;
575615 done ( ) ;
576616 } ) ;
577617 } ) ;
578618 } ) ;
579619 } ) ;
580620
581- it ( 'should throw error on unknown attributes when strict: throw' , function ( done ) {
621+ // strict: validate is deprecated, use strict: true instead
622+ // behavior remains the same as before, because validate is now default behavior
623+ it ( 'should fallback to strict:true when using strict:validate' , function ( done ) {
582624 Person . definition . settings . strict = 'validate' ;
583625 Person . findById ( person . id , function ( err , p ) {
584626 p . updateAttributes ( { foo : 'bar' } ,
@@ -588,7 +630,7 @@ describe('manipulation', function() {
588630 err . message . should . containEql ( '`foo` is not defined in the model' ) ;
589631 Person . findById ( person . id , function ( e , p ) {
590632 if ( e ) return done ( e ) ;
591- should . not . exist ( p . foo ) ;
633+ p . should . not . have . property ( ' foo' ) ;
592634 done ( ) ;
593635 } ) ;
594636 } ) ;
0 commit comments