Skip to content

Commit 12c6a73

Browse files
committed
email,url,ip format are now recognized
1 parent f8b0860 commit 12c6a73

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

dist/browser.js

+24-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

+20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
import isEqual from 'lodash.isequal';
44

5+
// Regex used here are all from
6+
// http://code.tutsplus.com/tutorials/8-regular-expressions-you-should-know--net-6149
7+
const FORMAT_REGEX = {
8+
email : /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/,
9+
url : /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/,
10+
ip : /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
11+
};
12+
513
/*
614
* Converts a JSON object to a JSON Schema
715
* @param {any} json
@@ -21,6 +29,18 @@ function convert (json, options = {}) {
2129
// primitives
2230
if (typeof json === 'string') {
2331

32+
if (FORMAT_REGEX.url.test(json)){
33+
return {type: 'string', format: 'url'};
34+
}
35+
36+
if (FORMAT_REGEX.email.test(json)){
37+
return {type:'string', format: 'email'};
38+
}
39+
40+
if (FORMAT_REGEX.ip.test(json)){
41+
return {type:'string', format: 'ip'};
42+
}
43+
2444
// TODO: date format
2545
return {type: 'string'};
2646
}

test/primitives.js

+15
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,19 @@ describe('primitives', ()=> {
100100
}
101101
});
102102
});
103+
});
104+
105+
describe('Custom formats', ()=> {
106+
107+
describe('email', ()=> {
108+
expect('test@gmail.com', {type:'string', format: 'email'});
109+
});
110+
111+
describe('ip', ()=> {
112+
expect('127.0.0.1', {type:'string', format: 'ip'});
113+
});
114+
115+
describe('url', ()=> {
116+
expect('https://google.com', {type:'string', format: 'url'});
117+
});
103118
});

0 commit comments

Comments
 (0)