UNPKG

7.18 kBJavaScriptView Raw
1import inherits from './util/inherits';
2import MixedSchema from './mixed';
3import { string as locale } from './locale';
4import isAbsent from './util/isAbsent'; // eslint-disable-next-line
5
6var rEmail = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i; // eslint-disable-next-line
7
8var rUrl = /^((https?|ftp):)?\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da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line
9
10var rUUID = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;
11
12var isTrimmed = function isTrimmed(value) {
13 return isAbsent(value) || value === value.trim();
14};
15
16var objStringTag = {}.toString();
17export default function StringSchema() {
18 var _this = this;
19
20 if (!(this instanceof StringSchema)) return new StringSchema();
21 MixedSchema.call(this, {
22 type: 'string'
23 });
24 this.withMutation(function () {
25 _this.transform(function (value) {
26 if (this.isType(value)) return value;
27 if (Array.isArray(value)) return value;
28 var strValue = value != null && value.toString ? value.toString() : value;
29 if (strValue === objStringTag) return value;
30 return strValue;
31 });
32 });
33}
34inherits(StringSchema, MixedSchema, {
35 _typeCheck: function _typeCheck(value) {
36 if (value instanceof String) value = value.valueOf();
37 return typeof value === 'string';
38 },
39 _isPresent: function _isPresent(value) {
40 return MixedSchema.prototype._isPresent.call(this, value) && !!value.length;
41 },
42 length: function length(_length, message) {
43 if (message === void 0) {
44 message = locale.length;
45 }
46
47 return this.test({
48 message: message,
49 name: 'length',
50 exclusive: true,
51 params: {
52 length: _length
53 },
54 test: function test(value) {
55 return isAbsent(value) || value.length === this.resolve(_length);
56 }
57 });
58 },
59 min: function min(_min, message) {
60 if (message === void 0) {
61 message = locale.min;
62 }
63
64 return this.test({
65 message: message,
66 name: 'min',
67 exclusive: true,
68 params: {
69 min: _min
70 },
71 test: function test(value) {
72 return isAbsent(value) || value.length >= this.resolve(_min);
73 }
74 });
75 },
76 max: function max(_max, message) {
77 if (message === void 0) {
78 message = locale.max;
79 }
80
81 return this.test({
82 name: 'max',
83 exclusive: true,
84 message: message,
85 params: {
86 max: _max
87 },
88 test: function test(value) {
89 return isAbsent(value) || value.length <= this.resolve(_max);
90 }
91 });
92 },
93 matches: function matches(regex, options) {
94 var excludeEmptyString = false;
95 var message;
96 var name;
97
98 if (options) {
99 if (typeof options === 'object') {
100 excludeEmptyString = options.excludeEmptyString;
101 message = options.message;
102 name = options.name;
103 } else {
104 message = options;
105 }
106 }
107
108 return this.test({
109 name: name || 'matches',
110 message: message || locale.matches,
111 params: {
112 regex: regex
113 },
114 test: function test(value) {
115 return isAbsent(value) || value === '' && excludeEmptyString || value.search(regex) !== -1;
116 }
117 });
118 },
119 email: function email(message) {
120 if (message === void 0) {
121 message = locale.email;
122 }
123
124 return this.matches(rEmail, {
125 name: 'email',
126 message: message,
127 excludeEmptyString: true
128 });
129 },
130 url: function url(message) {
131 if (message === void 0) {
132 message = locale.url;
133 }
134
135 return this.matches(rUrl, {
136 name: 'url',
137 message: message,
138 excludeEmptyString: true
139 });
140 },
141 uuid: function uuid(message) {
142 if (message === void 0) {
143 message = locale.uuid;
144 }
145
146 return this.matches(rUUID, {
147 name: 'uuid',
148 message: message,
149 excludeEmptyString: false
150 });
151 },
152 //-- transforms --
153 ensure: function ensure() {
154 return this.default('').transform(function (val) {
155 return val === null ? '' : val;
156 });
157 },
158 trim: function trim(message) {
159 if (message === void 0) {
160 message = locale.trim;
161 }
162
163 return this.transform(function (val) {
164 return val != null ? val.trim() : val;
165 }).test({
166 message: message,
167 name: 'trim',
168 test: isTrimmed
169 });
170 },
171 lowercase: function lowercase(message) {
172 if (message === void 0) {
173 message = locale.lowercase;
174 }
175
176 return this.transform(function (value) {
177 return !isAbsent(value) ? value.toLowerCase() : value;
178 }).test({
179 message: message,
180 name: 'string_case',
181 exclusive: true,
182 test: function test(value) {
183 return isAbsent(value) || value === value.toLowerCase();
184 }
185 });
186 },
187 uppercase: function uppercase(message) {
188 if (message === void 0) {
189 message = locale.uppercase;
190 }
191
192 return this.transform(function (value) {
193 return !isAbsent(value) ? value.toUpperCase() : value;
194 }).test({
195 message: message,
196 name: 'string_case',
197 exclusive: true,
198 test: function test(value) {
199 return isAbsent(value) || value === value.toUpperCase();
200 }
201 });
202 }
203});
\No newline at end of file