Skip to content

Commit

Permalink
feat: add support to schema format
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-sierra-sngular authored and jorgecasar committed Dec 22, 2023
1 parent 0391803 commit fefe0a5
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions lib/response-generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,34 @@ const getFakerLocale = require('../utils/get-faker-locale');

const faker = getFakerLocale();

const FORMAT_EXAMPLES = {
date: '2023-11-17',
time: '14:30:00Z',
'date-time': '2023-11-17T14:30:00Z',
'iso-time': '14:30:00Z',
'iso-date-time': '2023-11-17T14:30:00Z',
duration: 'PT2H30M',
uri: 'https://www.example.com',
'uri-reference': 'https://www.example.com/resource',
'uri-template': 'https://www.example.com/{id}',
url: 'https://www.example.com',
email: 'example@email.com',
hostname: 'www.example.com',
ipv4: '192.168.0.1',
ipv6: '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
regex: '^\\d{3}-\\d{2}-\\d{4}$',
uuid: '550e8400-e29b-41d4-a716-446655440000',
'json-pointer': '/path/to/resource',
'relative-json-pointer': '2/foo',
byte: 'SGVsbG8gd29ybGQ=',
int32: 123,
int64: 9223372036854776000,
float: 3.14,
double: 2.718281828459045,
password: 'P@ssw0rd',
binary: '01001000 01100101 01101100 01101100 01101111'
};

class ResponseGenerator {

static generate(schemaResponse, preferredExampleName) {
Expand Down Expand Up @@ -134,16 +162,16 @@ class ResponseGenerator {
}), {});
}

static generateString() {
return 'string';
static generateString({ format }) {
return FORMAT_EXAMPLES[format] || 'string';
}

static generateNumber() {
return 1;
static generateNumber({ format }) {
return FORMAT_EXAMPLES[format] || 1;
}

static generateInteger() {
return 1;
static generateInteger({ format }) {
return FORMAT_EXAMPLES[format] || 1;
}

static generateBoolean() {
Expand Down

0 comments on commit fefe0a5

Please sign in to comment.