forked from thetutlage/angular-validation-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-validation-schema.js
182 lines (133 loc) · 4.88 KB
/
angular-validation-schema.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
/*
***************************************
SCHEMA VALIDATOR
***************************************
Schema validator for angular-valiation, good
if you want to keep your backend validation rules
in sync or your hate making your html too declarative.
@author - Harminder Virk
@github - https://github.com/thetutlage/angular-validation-schema
*/
(function() {
var app = angular.module('validation.schema',[])
// Tiny provider to save and fetch schemas
.provider('validationSchema',function(){
// List of schemas are stored here
var schemas = {};
// Adding schema to object
this.set = function(name,hash){
schemas[name] = hash;
};
// fetching from object
this.get = function(name){
return schemas[name];
}
// Exposing them here
this.$get = function(){
return{
set: this.set,
get: this.get
}
}
})
// Required directive
.directive('validationSchema',['validationSchema','$injector',function(validationSchema, $injector){
return{
restrict: 'AE',
compile: function(tElem, tAttrs){
// Get Validation Provider
var $validationProvider = $injector.get('$validation');
// Default schema to extend upon
var defaultSchema = {
// default validation is set to required
'validations':'required',
'validate-on': $validationProvider.getValidMethod() || 'watch'
};
var globalMessages = {};
// Grabbing schema the user wants
var schema = validationSchema.get(tAttrs.schema);
if(schema){
// If it is an valid schema , then setFixtures
setFixtures(schema);
}else{
// Otherwise show warning of non-existing schema
warnDeveloper(tAttrs.schema);
}
// Warn developer method
function warnDeveloper(schema){
// Error message
var error_message = schema + ' Schema not found';
// Outputting to console
console.warn('VALIDATION SCHEMA :',error_message);
}
function setFixtures(schema){
// If schema has globals
if(schema.globals){
// Remove defaultSchema validations with globals
defaultSchema.validations = schema.globals.validations || defaultSchema.validations;
// Remove validate-on validations with globals
defaultSchema["validate-on"] = schema.globals["validate-on"] || defaultSchema["validate-on"];
// Set globalMessages if globals have one or empty object
globalMessages = schema.globals.messages || {};
// Delete schema globals as not required anymore
delete schema.globals;
}
// Deep extending objects
function extendDeep(dst) {
// Looping through all the values
angular.forEach(arguments, function(obj) {
if (obj !== dst) {
angular.forEach(obj, function(value, key) {
if (dst[key] && dst[key].constructor && dst[key].constructor === Object) {
// Rerun if above key is an Object
extendDeep(dst[key], value);
} else {
// Extend here
dst[key] = dst[key] || value;
}
});
}
});
return dst;
}
// schema.firstname = extendDeep(schema.firstname,defaultSchema);
// Grabbing all form elements inside the tElem
var formElements = tElem[0].querySelectorAll('[name]');
// Looping through all form Elements
angular.forEach(formElements,function(input){
// Getting instance of angular element
var i = angular.element(input);
// Grabbing name
// @description - Name is required to match keys on schema
// @example - firstname key on schema will be matched with firstname as form
// element name
var input_name = i.attr('name');
// If schema defination exists
if(schema[input_name]){
// Deep extend rules to save undefined stuff
var i_schema = extendDeep(schema[input_name],defaultSchema);
// Setting validator on field
i.attr('validator',i_schema.validations);
// Setting valid-method on field
i.attr('valid-method',i_schema['validate-on']);
// Extends messages using global and field values
i_schema.messages = i_schema.messages || {};
i_schema.messages = extendDeep(i_schema.messages,globalMessages);
// Looping through messages object
angular.forEach(i_schema.messages,function(vals,key){
// Grabbing error message and replace %field% with input name
var error_message = vals.error ? vals.error.replace('%field%',input_name) : '';
// Grabbing success message and replace %field% with input name
var success_message = vals.success ? vals.success.replace('%field%',input_name) : '';
// Setting error message for validation type
i.attr(key+'-error-message',error_message);
// Setting success message for validation type
i.attr(key+'-success-message',success_message);
});
}
});
}
}
}
}]);
}).call(this);