-
Notifications
You must be signed in to change notification settings - Fork 46
/
string.js
85 lines (70 loc) · 1.96 KB
/
string.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
'use strict';
import { ensureMinLength, toRFCDateTime, uuid } from '../utils';
const passwordSymbols = 'qwerty!@#$%^123456';
function emailSample() {
return 'user@example.com';
}
function passwordSample(min, max) {
let res = 'pa$$word';
if (min > res.length) {
res += '_';
res += ensureMinLength(passwordSymbols, min - res.length).substring(0, min - res.length);
}
return res;
}
function commonDateTimeSample(min, max, omitTime) {
let res = toRFCDateTime(new Date('2019-08-24T14:15:22.123Z'), omitTime, false);
if (res.length < min) {
console.warn(`Using minLength = ${min} is incorrect with format "date-time"`);
}
if (max && res.length > max) {
console.warn(`Using maxLength = ${max} is incorrect with format "date-time"`);
}
return res;
}
function dateTimeSample(min, max) {
return commonDateTimeSample(min, max);
}
function dateSample(min, max) {
return commonDateTimeSample(min, max, true);
}
function defaultSample(min, max) {
let res = ensureMinLength('string', min);
if (max && res.length > max) {
res = res.substring(0, max);
}
return res;
}
function ipv4Sample() {
return '192.168.0.1';
}
function ipv6Sample() {
return '2001:0db8:85a3:0000:0000:8a2e:0370:7334';
}
function hostnameSample() {
return 'example.com';
}
function uriSample() {
return 'http://example.com';
}
function uuidSample(_min, _max, propertyName) {
return uuid(propertyName || 'id');
}
const stringFormats = {
'email': emailSample,
'password': passwordSample,
'date-time': dateTimeSample,
'date': dateSample,
'ipv4': ipv4Sample,
'ipv6': ipv6Sample,
'hostname': hostnameSample,
'uri': uriSample,
'uuid': uuidSample,
'default': defaultSample
};
export function sampleString(schema, options, spec, context) {
let format = schema.format || 'default';
let sampler = stringFormats[format] || defaultSample;
let propertyName = context && context.propertyName;
return sampler(schema.minLength | 0, schema.maxLength, propertyName);
}