Skip to content

Commit c5a5fd9

Browse files
author
Andreas Krummsdorf
committed
feat: add format uuid
1 parent 8b0a1d6 commit c5a5fd9

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

lib/lx-valid.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@
176176

177177
return getResult(null);
178178
};
179+
pub.uuid = function (val) {
180+
if (!revalidator.validate.formats['uuid'].test(val)) {
181+
return getResult(getError('format', 'uuid', val));
182+
}
183+
184+
return getResult(null);
185+
};
179186
pub.numberFloat = function (val) {
180187
if (typeof val !== 'string' || !revalidator.validate.formats['number-float'].test(val)) {
181188
return getResult(getError('format', 'float', val));

lib/revalidator.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@
232232
}
233233
},
234234
'mongo-id': /^[0-9a-fA-F]{8}[0-9a-fA-F]{6}[0-9a-fA-F]{4}[0-9a-fA-F]{6}$/,
235+
'uuid': /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
235236
'number-float': /^[\-\+]?\b(\d+[.]\d+$)$/,
236237
'float': /^[\-\+]?\b(\d+[.]\d+$)$/,
237238
'integer': /^[\-\+]?[0-9]+$/,

test/formats.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,21 @@ describe('Formats', function () {
149149
expect(res3.errors.length).toBe(1);
150150
});
151151

152+
it('should validate an UUID correctly', function () {
153+
var res1 = val.formats.uuid('6648ae51-28f1-ad83-6b8d-f45284d2c173');
154+
var res2 = val.formats.uuid('b5f41c10-a0ef-886a-c5d2-91c32a360f4e');
155+
var res3 = val.formats.uuid('wayne');
156+
157+
expect(res1.valid).toBe(true);
158+
expect(res1.errors.length).toBe(0);
159+
160+
expect(res2.valid).toBe(true);
161+
expect(res2.errors.length).toBe(0);
162+
163+
expect(res3.valid).toBe(false);
164+
expect(res3.errors.length).toBe(1);
165+
});
166+
152167
it('should validate a float number correctly', function () {
153168
var res1 = val.formats.numberFloat(2.66);
154169
var res2 = val.formats.numberFloat(3);

0 commit comments

Comments
 (0)