11'use strict' ;
22
3+ var _createClass = function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ( ) ;
4+
35var _extends = Object . assign || function ( target ) { for ( var i = 1 ; i < arguments . length ; i ++ ) { var source = arguments [ i ] ; for ( var key in source ) { if ( Object . prototype . hasOwnProperty . call ( source , key ) ) { target [ key ] = source [ key ] ; } } } return target ; } ;
46
5- var _createClass = function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ( ) ;
7+ var _typeof = typeof Symbol === " function" && typeof Symbol . iterator === "symbol" ? function ( obj ) { return typeof obj ; } : function ( obj ) { return obj && typeof Symbol === " function" && obj . constructor === Symbol && obj !== Symbol . prototype ? "symbol" : typeof obj ; } ;
68
79function _defineProperty ( obj , key , value ) { if ( key in obj ) { Object . defineProperty ( obj , key , { value : value , enumerable : true , configurable : true , writable : true } ) ; } else { obj [ key ] = value ; } return obj ; }
810
@@ -12,26 +14,41 @@ var _require = require('lodash'),
1214 clone = _require . clone ,
1315 get = _require . get ,
1416 isNil = _require . isNil ,
15- isFunction = _require . isFunction ;
17+ isFunction = _require . isFunction ,
18+ noop = _require . noop ;
19+
20+ function applyAfterSetHook ( field , data , afterSetHook ) {
21+ var newData = afterSetHook ( data , field ) ;
22+ ( typeof newData === 'undefined' ? 'undefined' : _typeof ( newData ) ) === 'object' && _extends ( data , newData ) ;
23+ }
24+
25+ function createGetterAndSetter ( instance , field ) {
26+ var schemaField = instance . schema [ field ] ;
27+
28+ var afterSet = get ( schemaField , 'hooks.afterSet' ) ;
29+ var afterSetHook = typeof afterSet === 'function' ? applyAfterSetHook : noop ;
1630
17- var createGetterAndSetter = function createGetterAndSetter ( instance , field ) {
1831 return {
19- set : function set ( value ) {
20- if ( instance . data [ field ] !== value ) {
21- instance . data [ field ] = value ;
32+ set : function set ( newValue ) {
33+ if ( instance . data [ field ] !== newValue ) {
34+ instance . data [ field ] = newValue ;
35+
36+ afterSetHook ( field , instance . data , afterSet ) ;
37+
2238 return instance . _validate ( ) ;
2339 }
2440 } ,
2541 get : function get ( ) {
2642 return instance . data [ field ] ;
2743 } ,
44+
2845 enumerable : true
2946 } ;
30- } ;
47+ }
3148
3249var Speck = function ( ) {
33- function Speck ( data ) {
34- var _this = this ;
50+ function Speck ( ) {
51+ var data = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : { } ;
3552
3653 _classCallCheck ( this , Speck ) ;
3754
@@ -46,22 +63,24 @@ var Speck = function () {
4663 } ) ;
4764
4865 Object . defineProperty ( this , 'childrenEntities' , {
49- value : Object . keys ( this . constructor . SCHEMA ) . filter ( function ( field ) {
50- return ! ! _this . constructor . SCHEMA [ field ] . type ;
51- } ) ,
66+ value : Object . keys ( this . constructor . SCHEMA ) . filter ( this . __fieldHasType . bind ( this ) ) ,
5267 enumerable : false
5368 } ) ;
5469
55- this . errors = { } ;
5670 Object . defineProperty ( this , 'data' , {
57- value : this . _mergeDefault ( data || { } ) ,
71+ value : this . _mergeDefault ( data ) ,
5872 enumerable : false
5973 } ) ;
6074
6175 this . _validate ( ) ;
6276 }
6377
6478 _createClass ( Speck , [ {
79+ key : '__fieldHasType' ,
80+ value : function __fieldHasType ( field ) {
81+ return ! ! this . constructor . SCHEMA [ field ] . type ;
82+ }
83+ } , {
6584 key : '__initFieldValue' ,
6685 value : function __initFieldValue ( field , data ) {
6786 var hasValue = ! isNil ( data [ field ] ) ;
@@ -107,20 +126,19 @@ var Speck = function () {
107126 value : function __validateField ( field ) {
108127 var validator = typeof this . schema [ field ] === 'function' ? this . schema [ field ] : this . schema [ field ] . validator ;
109128
110- var error = validator ( this . data , field , this . constructor . name + 'Entity' ) ;
111-
112- if ( error ) {
113- this . errors [ field ] = { errors : [ error . message || error ] } ;
114- }
129+ return validator ( this . data , field , this . constructor . name + 'Entity' ) ;
115130 }
116131 } , {
117132 key : '_validate' ,
118133 value : function _validate ( ) {
119134 this . errors = { } ;
120135
121- var field = void 0 ;
122- for ( field in this . schema ) {
123- this . __validateField ( field ) ;
136+ for ( var field in this . schema ) {
137+ var error = this . __validateField ( field ) ;
138+
139+ if ( error ) {
140+ this . errors [ field ] = { errors : [ error . message || error ] } ;
141+ }
124142 }
125143 this . valid = Object . keys ( this . errors ) . length === 0 ;
126144
@@ -169,10 +187,10 @@ var Speck = function () {
169187 } , {
170188 key : 'toJSON' ,
171189 value : function toJSON ( ) {
172- var _this2 = this ;
190+ var _this = this ;
173191
174192 var rawData = Object . keys ( this . data ) . reduce ( function ( data , field ) {
175- return _extends ( data , _defineProperty ( { } , field , _this2 . _fetchChild ( _this2 . data [ field ] ) ) ) ;
193+ return _extends ( data , _defineProperty ( { } , field , _this . _fetchChild ( _this . data [ field ] ) ) ) ;
176194 } , { } ) ;
177195
178196 return JSON . parse ( JSON . stringify ( rawData ) ) ;
@@ -187,7 +205,7 @@ var Speck = function () {
187205 } , {
188206 key : 'validateContext' ,
189207 value : function validateContext ( context ) {
190- var _this3 = this ;
208+ var _this2 = this ;
191209
192210 if ( ! get ( this . contexts , context ) ) return this . errors ;
193211
@@ -196,15 +214,15 @@ var Speck = function () {
196214 } ;
197215 if ( this . contexts [ context ] . exclude && Object . keys ( this . contexts [ context ] . exclude ) . length > 0 ) {
198216 validation = function validation ( error ) {
199- return _this3 . contexts [ context ] . exclude . find ( function ( exclude ) {
217+ return _this2 . contexts [ context ] . exclude . find ( function ( exclude ) {
200218 return exclude === error ;
201219 } ) === undefined ;
202220 } ;
203221 }
204222
205223 if ( this . contexts [ context ] . include && Object . keys ( this . contexts [ context ] . include ) . length > 0 ) {
206224 validation = function validation ( error ) {
207- return _this3 . contexts [ context ] . include . find ( function ( include ) {
225+ return _this2 . contexts [ context ] . include . find ( function ( include ) {
208226 return include === error ;
209227 } ) !== undefined ;
210228 } ;
@@ -213,8 +231,8 @@ var Speck = function () {
213231 var contextErrors = _extends ( { } , this . errors ) ;
214232 if ( this . contexts [ context ] . fields ) {
215233 Object . keys ( this . contexts [ context ] . fields ) . forEach ( function ( field ) {
216- var result = _this3 . contexts [ context ] . fields [ field ] ( _this3 , field , _this3 . constructor . name ) ;
217- if ( result ) contextErrors [ field ] = { errors : result } ; else delete contextErrors [ field ] ;
234+ var result = _this2 . contexts [ context ] . fields [ field ] ( _this2 , field , _this2 . constructor . name ) ;
235+ result && ( contextErrors [ field ] = { errors : result } ) ;
218236 } ) ;
219237 }
220238
0 commit comments