-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate-string.html
More file actions
163 lines (155 loc) · 4.51 KB
/
validate-string.html
File metadata and controls
163 lines (155 loc) · 4.51 KB
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
<link rel="import" href="../polymer/polymer-element.html" />
<dom-module id="validate-string"></dom-module>
<script>
/**
* This component will validate the `input` attribute against either the `acceptedValues`
* array or a regular expression. When validation succeeds it will return the `input`
* value via an `ouput` attribute for further databinding. Also has an `isValid` boolean
* attribute for use when the actual value is not required.
*
* Example:
*
* ```
* <validate-string input='[[myValue]]'
* output='{{validatedValue}}'
* is-valid='{{saveThisSomewhereElse}}'
* acceptedValues='["possible match 1", "possible match 2", ...]'>
* </validate-string>
* ```
*
* @demo demo/index.html
* @test test/index.html
*/
class ValidateString extends Polymer.Element {
static get is() { return 'validate-string'; }
static get properties() {
return {
/**
* sets an optional array of acceptible values for `input`
*/
acceptedValues: {
type: Array,
value: function() { return []; }
},
/**
* stores a filtered version of the specified array of acceptable values
*/
_filteredValues: {
type: Array,
value: function() { return []; },
computed: '_filter(acceptedValues)'
},
/**
* specifies an optional regular expression the `input` should be validated against
*/
acceptedRegex: {
type: String,
value: ''
},
/**
* takes in the string to validate
*/
input: {
type: String,
value: ''
},
/**
* outputs either the `input` value when validation succeeds, or otherwise null
*/
output: {
type: String,
value: '',
notifies: true,
computed: '_validateIt(input, _filteredValues, acceptedRegex)'
},
/**
* indicates whether the most-recent validation attempt succeeded or not
*/
isValid: {
type: Boolean,
value: false,
readOnly: true,
notifies: true
},
/**
* indicates for internal use whether the array option has been specified and is valid
*/
_haveArrayValues: {
type: Boolean,
computed: '_notEmptyZeroOrNull(_filteredValues)'
}
}
}
/**
* Iterates over the input array values creating a new array with empty values removed
*
* @param {array} array the array of values to filter empty values from
* @return {array} returns a copy of the input array with empty values removed
*/
_filter(array) {
var host = this;
var newArray = [];
if (host._notEmptyZeroOrNull(array)) {
array.forEach(function(item) {
if (host._notEmptyZeroOrNull(item)) {
newArray.push(item);
}
});
}
return newArray;
}
/**
* Helper function to check for non-empty values. Used by `_filter`.
*
* @param {object} value an arbitrary value to check
* @return {boolean} return value is `true` if the input value is non-empty
*/
_notEmptyZeroOrNull(value) {
if (value === null || value === false) {
return false;
}
if (typeof(value['length']) === 'number') {
return (value.length > 0) ? true : false;
}
if (typeof(value === 'string')) {
return (value !== '') ? true : false;
}
return true;
}
/**
* The meat of the component.
*
* This function does the actual validation. When validation succeeds,
* this function will return the `input`, and will also set the `isValid`
* property of the component to `true`. If validation fails the `isValid`
* will be switched to `false` and the function will return `null`.
*
* @param {string} input the string to validate
* @param {array} values the optional array of strings to compare `input` against. set to null or false to bypass
* @param {string} regex the regular expression as a string to test against the `input` string
* @return {string} output returns the `input` value when validation succeeds or otherwise null
*/
_validateIt(input, values, regex) {
var valid = false;
if (typeof input === 'string' && input != '') {
if (values !== null && values.length > 0) {
valid = values.some(function(value, index, values) {
return value !== null && value !== '' && input === value;
});
} else if (regex != '') {
var re = new RegExp(regex);
valid = re.test(input);
} else {
valid = true;
}
}
if (valid) {
this._setIsValid(true);
return input;
}
this._setIsValid(false);
return null;
}
}
customElements.define(ValidateString.is, ValidateString);
</script>