-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathstring.js
More file actions
146 lines (124 loc) · 3.74 KB
/
string.js
File metadata and controls
146 lines (124 loc) · 3.74 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
'use strict';
import { ensureMinLength, toRFCDateTime, uuid } from '../utils';
import * as faker from './string-regex';
const passwordSymbols = 'qwerty!@#$%^123456';
function emailSample() {
return 'user@example.com';
}
function idnEmailSample() {
return 'пошта@укр.нет';
}
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, omitDate }) {
let res = toRFCDateTime(new Date('2019-08-24T14:15:22.123Z'), omitTime, omitDate, 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, omitTime: false, omitDate: false });
}
function dateSample(min, max) {
return commonDateTimeSample({ min, max, omitTime: true, omitDate: false });
}
function timeSample(min, max) {
return commonDateTimeSample({ min, max, omitTime: false, omitDate: true }).slice(1);
}
function defaultSample(min, max, _propertyName, pattern, enablePatterns = false) {
if (pattern && enablePatterns) {
return faker.regexSample(pattern);
}
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 idnHostnameSample() {
return 'приклад.укр';
}
function uriSample() {
return 'http://example.com';
}
function uriReferenceSample() {
return '../dictionary';
}
function uriTemplateSample() {
return 'http://example.com/{endpoint}';
}
function iriSample() {
return 'http://example.com/entity/1';
}
function iriReferenceSample() {
return '/entity/1';
}
function uuidSample(_min, _max, propertyName) {
return uuid(propertyName || 'id');
}
function jsonPointerSample() {
return '/json/pointer';
}
function relativeJsonPointerSample() {
return '1/relative/json/pointer';
}
function regexSample() {
return '/regex/';
}
function binarySample() {
return [116, 101, 115, 116];
}
const stringFormats = {
'email': emailSample,
'idn-email': idnEmailSample, // https://tools.ietf.org/html/rfc6531#section-3.3
'password': passwordSample,
'date-time': dateTimeSample,
'date': dateSample,
'time': timeSample, // full-time in https://tools.ietf.org/html/rfc3339#section-5.6
'ipv4': ipv4Sample,
'ipv6': ipv6Sample,
'hostname': hostnameSample,
'idn-hostname': idnHostnameSample, // https://tools.ietf.org/html/rfc5890#section-2.3.2.3
'iri': iriSample, // https://tools.ietf.org/html/rfc3987
'iri-reference': iriReferenceSample,
'uri': uriSample,
'uri-reference': uriReferenceSample, // either a URI or relative-reference https://tools.ietf.org/html/rfc3986#section-4.1
'uri-template': uriTemplateSample,
'uuid': uuidSample,
'default': defaultSample,
'json-pointer': jsonPointerSample,
'relative-json-pointer': relativeJsonPointerSample, // https://tools.ietf.org/html/draft-handrews-relative-json-pointer-01
'regex': regexSample,
'binary': binarySample,
};
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,
schema.pattern,
options?.enablePatterns
);
}