1+ ( function ( root , factory ) {
2+ if ( typeof define === 'function' && define . amd ) {
3+ define ( [ 'angular' , 'ObjectPath' , 'tv4' ] , factory ) ;
4+ } else if ( typeof exports === 'object' ) {
5+ module . exports = factory ( require ( 'angular' ) , require ( 'ObjectPath' ) , require ( 'tv4' ) ) ;
6+ } else {
7+ root . schemaForm = factory ( root . angular , root . ObjectPath , root . tv4 ) ;
8+ }
9+ } ( this , function ( angular , ObjectPath , tv4 ) {
110// Deps is sort of a problem for us, maybe in the future we will ask the user to depend
211// on modules for add-ons
312
2029 deps . push ( 'angularSpectrumColorpicker' ) ;
2130} catch ( e ) { }
2231
23- angular . module ( 'schemaForm' , deps ) ;
32+ var schemaForm = angular . module ( 'schemaForm' , deps ) ;
2433
2534angular . module ( 'schemaForm' ) . provider ( 'sfPath' ,
2635[ function ( ) {
@@ -159,8 +168,8 @@ angular.module('schemaForm').provider('schemaFormDecorators',
159168
160169 var createDirective = function ( name ) {
161170 $compileProvider . directive ( name ,
162- [ '$parse' , '$compile' , '$http' , '$templateCache' , '$interpolate' , '$q' , 'sfErrorMessage' ,
163- function ( $parse , $compile , $http , $templateCache , $interpolate , $q , sfErrorMessage ) {
171+ [ '$parse' , '$compile' , '$http' , '$templateCache' , '$interpolate' , '$q' , 'sfErrorMessage' , 'sfPath' ,
172+ function ( $parse , $compile , $http , $templateCache , $interpolate , $q , sfErrorMessage , sfPath ) {
164173
165174 return {
166175 restrict : 'AE' ,
@@ -287,6 +296,7 @@ angular.module('schemaForm').provider('schemaFormDecorators',
287296 return sfErrorMessage . interpolate (
288297 ( schemaError && schemaError . code + '' ) || 'default' ,
289298 ( scope . ngModel && scope . ngModel . $modelValue ) || '' ,
299+ ( scope . ngModel && scope . ngModel . $viewValue ) || '' ,
290300 scope . form ,
291301 scope . options && scope . options . validationMessage
292302 ) ;
@@ -331,18 +341,23 @@ angular.module('schemaForm').provider('schemaFormDecorators',
331341 // Do we have a condition? Then we slap on an ng-if on all children,
332342 // but be nice to existing ng-if.
333343 if ( form . condition ) {
344+
345+ var evalExpr = 'evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex})' ;
346+ if ( form . key ) {
347+ evalExpr = 'evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex, "modelValue": model' + sfPath . stringify ( form . key ) + '})' ;
348+ }
349+
334350 angular . forEach ( element . children ( ) , function ( child ) {
335351 var ngIf = child . getAttribute ( 'ng-if' ) ;
336352 child . setAttribute (
337353 'ng-if' ,
338354 ngIf ?
339355 '(' + ngIf +
340- ') || (evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex }) )'
341- : ' evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex })'
356+ ') || (' + evalExpr + ' )'
357+ : evalExpr
342358 ) ;
343359 } ) ;
344360 }
345-
346361 $compile ( element . contents ( ) ) ( scope ) ;
347362 } ) ;
348363
@@ -372,15 +387,16 @@ angular.module('schemaForm').provider('schemaFormDecorators',
372387 if ( ! form . validationMessage ) {
373388 form . validationMessage = { } ;
374389 }
375- console . log ( 'settings validationMessage' , validationMessage )
376390 form . validationMessage [ error ] = validationMessage ;
377391 }
378392
379393 scope . ngModel . $setValidity ( error , validity === true ) ;
380394
381- // Setting or removing a validity can change the field to believe its valid
382- // but its not. So lets trigger its validation as well.
383- scope . $broadcast ( 'schemaFormValidate' ) ;
395+ if ( validity === true ) {
396+ // Setting or removing a validity can change the field to believe its valid
397+ // but its not. So lets trigger its validation as well.
398+ scope . $broadcast ( 'schemaFormValidate' ) ;
399+ }
384400 }
385401 } )
386402 }
@@ -548,32 +564,32 @@ angular.module('schemaForm').provider('sfErrorMessage', function() {
548564 // TODO: Humanize these.
549565 var defaultMessages = {
550566 'default' : 'Field does not validate' ,
551- 0 : 'Invalid type, expected {{schema.type}}) ' ,
567+ 0 : 'Invalid type, expected {{schema.type}}' ,
552568 1 : 'No enum match for: {{value}}' ,
553569 10 : 'Data does not match any schemas from "anyOf"' ,
554570 11 : 'Data does not match any schemas from "oneOf"' ,
555571 12 : 'Data is valid against more than one schema from "oneOf"' ,
556572 13 : 'Data matches schema from "not"' ,
557573 // Numeric errors
558- 100 : 'Value {{value}} is not a multiple of {{schema.multipleOf }}' ,
559- 101 : 'Value {{value }} is less than minimum {{schema.minimum}}' ,
560- 102 : 'Value {{value }} is equal to exclusive minimum {{schema.minimum}}' ,
561- 103 : 'Value {{value }} is greater than maximum {{schema.maximum}}' ,
562- 104 : 'Value {{value }} is equal to exclusive maximum {{schema.maximum}}' ,
563- 105 : 'Value {{value}} is not a valid number' ,
574+ 100 : 'Value is not a multiple of {{schema.divisibleBy }}' ,
575+ 101 : '{{viewValue }} is less than the allowed minimum of {{schema.minimum}}' ,
576+ 102 : '{{viewValue }} is equal to the exclusive minimum {{schema.minimum}}' ,
577+ 103 : '{{viewValue }} is greater than the allowed maximum of {{schema.maximum}}' ,
578+ 104 : '{{viewValue }} is equal to the exclusive maximum {{schema.maximum}}' ,
579+ 105 : 'Value is not a valid number' ,
564580 // String errors
565- 200 : 'String is too short ({{value .length}} chars), minimum {{schema.minimum }}' ,
566- 201 : 'String is too long ({{value .length}} chars), maximum {{schema.maximum }}' ,
581+ 200 : 'String is too short ({{viewValue .length}} chars), minimum {{schema.minLength }}' ,
582+ 201 : 'String is too long ({{viewValue .length}} chars), maximum {{schema.maxLength }}' ,
567583 202 : 'String does not match pattern: {{schema.pattern}}' ,
568584 // Object errors
569- 300 : 'Too few properties defined, minimum {{schema.minimum }}' ,
570- 301 : 'Too many properties defined, maximum {{schema.maximum }}' ,
585+ 300 : 'Too few properties defined, minimum {{schema.minProperties }}' ,
586+ 301 : 'Too many properties defined, maximum {{schema.maxProperties }}' ,
571587 302 : 'Required' ,
572588 303 : 'Additional properties not allowed' ,
573589 304 : 'Dependency failed - key must exist' ,
574590 // Array errors
575- 400 : 'Array is too short ({{value.length}}), minimum {{schema.minimum }}' ,
576- 401 : 'Array is too long ({{value.length}}), maximum {{schema.maximum }}' ,
591+ 400 : 'Array is too short ({{value.length}}), minimum {{schema.maxItems }}' ,
592+ 401 : 'Array is too long ({{value.length}}), maximum {{schema.minItems }}' ,
577593 402 : 'Array items are not unique' ,
578594 403 : 'Additional items not allowed' ,
579595 // Format errors
@@ -585,6 +601,15 @@ angular.module('schemaForm').provider('sfErrorMessage', function() {
585601 1000 : 'Unknown property (not in schema)'
586602 } ;
587603
604+ // In some cases we get hit with an angular validation error
605+ defaultMessages . number = defaultMessages [ 105 ] ;
606+ defaultMessages . required = defaultMessages [ 302 ] ;
607+ defaultMessages . min = defaultMessages [ 101 ] ;
608+ defaultMessages . max = defaultMessages [ 103 ] ;
609+ defaultMessages . maxlength = defaultMessages [ 201 ] ;
610+ defaultMessages . minlength = defaultMessages [ 200 ] ;
611+ defaultMessages . pattern = defaultMessages [ 202 ] ;
612+
588613 this . setDefaultMessages = function ( messages ) {
589614 defaultMessages = messages ;
590615 } ;
@@ -609,12 +634,13 @@ angular.module('schemaForm').provider('sfErrorMessage', function() {
609634 * @param {string } error the error code, i.e. tv4-xxx for tv4 errors, otherwise it's whats on
610635 * ngModel.$error for custom errors.
611636 * @param {Any } value the actual model value.
637+ * @param {Any } viewValue the viewValue
612638 * @param {Object } form a form definition object for this field
613639 * @param {Object } global the global validation messages object (even though its called global
614640 * its actually just shared in one instance of sf-schema)
615641 * @return {string } The error message.
616642 */
617- service . interpolate = function ( error , value , form , global ) {
643+ service . interpolate = function ( error , value , viewValue , form , global ) {
618644 global = global || { } ;
619645 var validationMessage = form . validationMessage || { } ;
620646
@@ -640,6 +666,7 @@ angular.module('schemaForm').provider('sfErrorMessage', function() {
640666 var context = {
641667 error : error ,
642668 value : value ,
669+ viewValue : viewValue ,
643670 form : form ,
644671 schema : form . schema ,
645672 title : form . title || ( form . schema && form . schema . title )
@@ -1495,7 +1522,7 @@ angular.module('schemaForm').directive('sfMessage',
14951522 }
14961523
14971524 var update = function ( valid ) {
1498- if ( valid && scope . hasError ( ) ) {
1525+ if ( valid && ! scope . hasError ( ) ) {
14991526 element . html ( msg ) ;
15001527 } else {
15011528
@@ -1505,24 +1532,13 @@ angular.module('schemaForm').directive('sfMessage',
15051532
15061533 // We only show one error.
15071534 // TODO: Make that optional
1508- // tv4- errors take precedence
15091535 var error = errors [ 0 ] ;
1510- if ( errors . length > 1 ) {
1511-
1512- error = errors . reduce ( function ( prev , value ) {
1513- if ( prev && prev . indexOf ( 'tv4-' ) === 0 ) {
1514- return prev ;
1515- }
1516- return value ;
1517- } ) ;
1518- console . log ( 'reduced' , errors , error )
1519-
1520- }
15211536
15221537 if ( error ) {
15231538 element . html ( sfErrorMessage . interpolate (
15241539 error ,
15251540 scope . ngModel . $modelValue ,
1541+ scope . ngModel . $viewValue ,
15261542 scope . form ,
15271543 scope . options && scope . options . validationMessage
15281544 ) ) ;
@@ -1752,6 +1768,11 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSele
17521768 return viewValue ;
17531769 }
17541770
1771+ // Omit TV4 validation
1772+ if ( scope . options && scope . options . tv4Validation === false ) {
1773+ return viewValue ;
1774+ }
1775+
17551776 var result = sfValidator . validate ( form , viewValue ) ;
17561777 // Since we might have different tv4 errors we must clear all
17571778 // errors that start with tv4-
@@ -1814,3 +1835,6 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSele
18141835 }
18151836 } ;
18161837} ] ) ;
1838+
1839+ return schemaForm ;
1840+ } ) ) ;
0 commit comments