This repository has been archived by the owner on Dec 3, 2021. It is now read-only.
forked from Redocly/openapi-sampler
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
162 additions
and
107 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
'use strict'; | ||
|
||
import { ensureMinLength, toRFCDateTime } from '../utils'; | ||
import {ensureMinLength, isBlank, toRFCDateTime} from '../utils'; | ||
import faker from 'faker'; | ||
import randexp from 'randexp'; | ||
|
||
const passwordSymbols = 'qwerty!@#$%^123456'; | ||
|
||
function emailSample() { | ||
return '[email protected]'; | ||
return faker.internet.email().toLowerCase(); | ||
} | ||
|
||
function passwordSample(min, max) { | ||
let res = 'pa$$word'; | ||
let res = faker.internet.password(); | ||
if (min > res.length) { | ||
res += '_'; | ||
res += ensureMinLength(passwordSymbols, min - res.length).substring(0, min - res.length); | ||
|
@@ -37,27 +39,53 @@ function dateSample(min, max) { | |
} | ||
|
||
function defaultSample(min, max) { | ||
let res = ensureMinLength('string', min); | ||
let res = ensureMinLength(faker.lorem.word(), min); | ||
if (max && res.length > max) { | ||
res = res.substring(0, max); | ||
} | ||
return res; | ||
} | ||
|
||
function ipv4Sample() { | ||
return '192.168.0.1'; | ||
return faker.internet.ip(); | ||
} | ||
|
||
function ipv6Sample() { | ||
return '2001:0db8:85a3:0000:0000:8a2e:0370:7334'; | ||
return faker.internet.ipv6(); | ||
} | ||
|
||
function hostnameSample() { | ||
return 'example.com'; | ||
return faker.internet.domainName(); | ||
} | ||
|
||
function uriSample() { | ||
return 'http://example.com'; | ||
return faker.internet.url(); | ||
} | ||
|
||
function byteSample() { | ||
return Buffer.from(faker.lorem.word()).toString('base64'); | ||
} | ||
|
||
function binarySample() { | ||
return Buffer.from(faker.lorem.word()); | ||
} | ||
|
||
function uuidSample() { | ||
return faker.random.uuid(); | ||
} | ||
|
||
function patternSample(min, max, pattern) { | ||
let res = new randexp(pattern).gen(); | ||
|
||
if (res.length < min) { | ||
throw new Error(`Using minLength = ${min} is incorrect with pattern ${pattern}`); | ||
} | ||
|
||
if (max && res.length > max) { | ||
throw new Error(`Using maxLength = ${max} is incorrect with pattern ${pattern}`); | ||
} | ||
|
||
return res; | ||
} | ||
|
||
const stringFormats = { | ||
|
@@ -69,11 +97,15 @@ const stringFormats = { | |
'ipv6': ipv6Sample, | ||
'hostname': hostnameSample, | ||
'uri': uriSample, | ||
'byte': byteSample, | ||
'binary': binarySample, | ||
'uuid': uuidSample, | ||
'pattern': patternSample, | ||
'default': defaultSample | ||
}; | ||
|
||
export function sampleString(schema) { | ||
let format = schema.format || 'default'; | ||
let sampler = stringFormats[format] || defaultSample; | ||
return sampler(schema.minLength | 0, schema.maxLength); | ||
let format = schema.pattern ? 'pattern' : schema.format || 'default'; | ||
let sampler = stringFormats[format] || defaultSample; | ||
return sampler(schema.minLength | 0, schema.maxLength, schema.pattern); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.