Skip to content

Commit

Permalink
feat(openapi-sampler): add support for extra format options
Browse files Browse the repository at this point in the history
closes #185
  • Loading branch information
derevnjuk committed Feb 21, 2023
1 parent 4567474 commit ec1c4c7
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/openapi-sampler/src/samplers/StringSampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,31 @@ import RandExp from 'randexp';
export class StringSampler implements Sampler {
private readonly stringFormats = {
'email': () => 'jon.snow@targaryen.com',
'idn-email': () => 'джон.сноу@таргариен.укр',
'password': (min: number, max: number) =>
this.adjustLength('p@$$w0rd', min, max),
'date-time': () => '2021-12-31T23:34:00Z',
'date': () => '2021-12-31',
'time': () => '23:34:00Z',
'duration': () => 'P3D',
'ipv4': () => '208.67.222.222',
'ipv6': () => '0000:0000:0000:0000:0000:ffff:d043:dcdc',
'hostname': () => 'brokencrystals.com',
'idn-hostname': () => 'сломанные-кристаллы.бел',
'iri': () =>
'https://be.wikipedia.org/wiki/%D0%9A%D1%80%D1%8B%D1%88%D1%82%D0%B0%D0%BB%D1%96',
'iri-reference': () =>
'/wiki/%D0%9A%D1%80%D1%8B%D1%88%D1%82%D0%B0%D0%BB%D1%96',
'uri': () => 'https://github.com/NeuraLegion/brokencrystals',
'uri-reference': () => '../brokencrystals',
'uri-template': () => 'https://brokencrystals.com/api/file/{provider}',
'byte': () => 'ZHVtbXkgYmluYXJ5IHNhbXBsZQA=',
'binary': () => '\\x01\\x02\\x03\\x04\\x05',
'base64': () => 'ZHVtbXkgYmluYXJ5IHNhbXBsZQA=',
'uuid': () => 'fbdf5a53-161e-4460-98ad-0e39408d8689',
'json-pointer': () => '/json/pointer',
'relative-json-pointer': () => '1/relative/json/pointer',
'regex': () => '/regex/',
'pattern': (
_min: number,
_max: number,
Expand Down Expand Up @@ -73,7 +86,7 @@ export class StringSampler implements Sampler {
? sample
.repeat(Math.trunc(minLength / sample.length) + 1)
.substring(0, minLength)
: sample.substr(
: sample.substring(
0,
Math.min(Math.max(sample.length, minLength), maxLength)
);
Expand Down
78 changes: 78 additions & 0 deletions packages/openapi-sampler/tests/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ describe('StringSampler', () => {
},
expected: 'jon.snow@targaryen.com'
},
{
input: {
type: 'string',
format: 'idn-email'
},
expected: 'джон.сноу@таргариен.укр'
},
{
input: {
type: 'string',
Expand Down Expand Up @@ -54,6 +61,20 @@ describe('StringSampler', () => {
},
expected: '2021-12-31T23:34:00Z'
},
{
input: {
type: 'string',
format: 'time'
},
expected: '23:34:00Z'
},
{
input: {
type: 'string',
format: 'duration'
},
expected: 'P3D'
},
{
input: {
type: 'string',
Expand All @@ -78,13 +99,70 @@ describe('StringSampler', () => {
},
expected: 'brokencrystals.com'
},
{
input: {
type: 'string',
format: 'idn-hostname'
},
expected: 'сломанные-кристаллы.бел'
},
{
input: {
type: 'string',
format: 'uri'
},
expected: 'https://github.com/NeuraLegion/brokencrystals'
},
{
input: {
type: 'string',
format: 'uri-reference'
},
expected: '../brokencrystals'
},
{
input: {
type: 'string',
format: 'uri-template'
},
expected: 'https://brokencrystals.com/api/file/{provider}'
},
{
input: {
type: 'string',
format: 'iri'
},
expected:
'https://be.wikipedia.org/wiki/%D0%9A%D1%80%D1%8B%D1%88%D1%82%D0%B0%D0%BB%D1%96'
},
{
input: {
type: 'string',
format: 'iri-reference'
},
expected: '/wiki/%D0%9A%D1%80%D1%8B%D1%88%D1%82%D0%B0%D0%BB%D1%96'
},
{
input: {
type: 'string',
format: 'json-pointer'
},
expected: '/json/pointer'
},
{
input: {
type: 'string',
format: 'relative-json-pointer'
},
expected: '1/relative/json/pointer'
},
{
input: {
type: 'string',
format: 'regex'
},
expected: '/regex/'
},
{
input: {
type: 'string',
Expand Down

0 comments on commit ec1c4c7

Please sign in to comment.