This repository was archived by the owner on Aug 15, 2020. It is now read-only.
forked from json-schema-form/angular-schema-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema-validate.js
190 lines (160 loc) · 7.11 KB
/
schema-validate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSelect', 'sfUnselect', '$parse', 'sfRetainModel',
function(sfValidator, sfSelect, sfUnselect, $parse, sfRetainModel) {
return {
restrict: 'A',
scope: false,
// We want the link function to be *after* the input directives link function so we get access
// the parsed value, ex. a number instead of a string
priority: 500,
require: 'ngModel',
link: function(scope, element, attrs, ngModel) {
// We need the ngModelController on several places,
// most notably for errors.
// So we emit it up to the decorator directive so it can put it on scope.
scope.$emit('schemaFormPropagateNgModelController', ngModel);
var error = null;
var getForm = function() {
if (!form) {
form = scope.$eval(attrs.schemaValidate);
}
return form;
};
var form = getForm();
if (form.copyValueTo) {
ngModel.$viewChangeListeners.push(function() {
var paths = form.copyValueTo;
angular.forEach(paths, function(path) {
sfSelect(path, scope.model, ngModel.$modelValue);
});
});
}
// Validate against the schema.
var validate = function(viewValue) {
form = getForm();
//Still might be undefined
if (!form) {
return viewValue;
}
// Omit TV4 validation
if (scope.options && scope.options.tv4Validation === false) {
return viewValue;
}
var result = sfValidator.validate(form, viewValue);
// Since we might have different tv4 errors we must clear all
// errors that start with tv4-
Object.keys(ngModel.$error)
.filter(function(k) { return k.indexOf('tv4-') === 0; })
.forEach(function(k) { ngModel.$setValidity(k, true); });
if (!result.valid) {
// it is invalid, return undefined (no model update)
ngModel.$setValidity('tv4-' + result.error.code, false);
error = result.error;
return undefined;
}
return viewValue;
};
// Custom validators, parsers, formatters etc
if (typeof form.ngModel === 'function') {
form.ngModel(ngModel);
}
['$parsers', '$viewChangeListeners', '$formatters'].forEach(function(attr) {
if (form[attr] && ngModel[attr]) {
form[attr].forEach(function(fn) {
ngModel[attr].push(fn);
});
}
});
['$validators', '$asyncValidators'].forEach(function(attr) {
// Check if our version of angular has i, i.e. 1.3+
if (form[attr] && ngModel[attr]) {
angular.forEach(form[attr], function(fn, name) {
ngModel[attr][name] = fn;
});
}
});
// Get in last of the parses so the parsed value has the correct type.
// We don't use $validators since we like to set different errors depeding tv4 error codes
ngModel.$parsers.push(validate);
// Listen to an event so we can validate the input on request
scope.$on('schemaFormValidate', function() {
if (ngModel.$setDirty) {
// Angular 1.3+
ngModel.$setDirty();
validate(ngModel.$modelValue);
} else {
// Angular 1.2
ngModel.$setViewValue(ngModel.$viewValue);
}
});
var DEFAULT_DESTROY_STRATEGY = getGlobalOptionsDestroyStrategy();
function getGlobalOptionsDestroyStrategy() {
var defaultStrategy = undefined;
if (scope.options && scope.options.hasOwnProperty('destroyStrategy')) {
var globalOptionsDestroyStrategy = scope.options.destroyStrategy;
var isValidFormDefaultDestroyStrategy = (globalOptionsDestroyStrategy === undefined ||
globalOptionsDestroyStrategy === '' ||
globalOptionsDestroyStrategy === null ||
globalOptionsDestroyStrategy === 'retain');
if (isValidFormDefaultDestroyStrategy) {
defaultStrategy = globalOptionsDestroyStrategy;
}
else {
console.warn('Unrecognized globalOptions destroyStrategy: %s \'%s\'. Used undefined instead.',
typeof globalOptionsDestroyStrategy, globalOptionsDestroyStrategy);
}
}
return defaultStrategy;
}
// Clean up the model when the corresponding form field is $destroy-ed.
// Default behavior can be supplied as a globalOption, and behavior can be overridden in the form definition.
scope.$on('$destroy', function() {
var form = getForm();
var conditionResult = $parse(form.condition);
var formModelNotRetained = !sfRetainModel.getFlag();
// If condition is defined and not satisfied and the sfSchema model should not be retained.
if (form.hasOwnProperty('condition') && !conditionResult(scope) && formModelNotRetained) {
// Either set in form definition, or as part of globalOptions.
var destroyStrategy =
!form.hasOwnProperty('destroyStrategy') ? DEFAULT_DESTROY_STRATEGY : form.destroyStrategy;
var schemaType = getSchemaType();
if (destroyStrategy && destroyStrategy !== 'retain') {
// Don't recognize the strategy, so give a warning.
console.warn('%s has defined unrecognized destroyStrategy: \'%s\'. Used default instead.',
attrs.name, destroyStrategy);
destroyStrategy = DEFAULT_DESTROY_STRATEGY;
}
else if (schemaType !== 'string' && destroyStrategy === '') {
// Only 'string' type fields can have an empty string value as a valid option.
console.warn('%s attempted to use empty string destroyStrategy on non-string form type. ' +
'Used default instead.', attrs.name);
destroyStrategy = DEFAULT_DESTROY_STRATEGY;
}
if (destroyStrategy === 'retain') {
return; // Valid option to avoid destroying data in the model.
}
destroyUsingStrategy(destroyStrategy);
function destroyUsingStrategy(strategy) {
var strategyIsDefined = (strategy === null || strategy === '' || strategy === undefined);
if (!strategyIsDefined) {
strategy = DEFAULT_DESTROY_STRATEGY;
}
sfUnselect(scope.form.key, scope.model, strategy);
}
function getSchemaType() {
var sType;
if (form.schema) {
sType = form.schema.type;
}
else {
sType = null;
}
return sType;
}
}
});
scope.schemaError = function() {
return error;
};
}
};
}]);