Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

feat: start exposing pattern attributes in schemas #1181

Merged
merged 1 commit into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions example/swagger-files/types.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@
"type": "string",
"format": "date"
},
"string (format: date, with pattern)": {
"type": "string",
"format": "date",
"description": "This accepts a pattern of matching `(\\d{4})-(\\d{2})-(\\d{2})`",
"pattern": "(\\d{4})-(\\d{2})-(\\d{2})"
},
"string (format: date-time)": {
"description": "Unsupported due to the varying ways that `date-time` is utilized in API definitions for representing dates, the [lack of wide browser support for the input](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local#Browser_compatibility), and that it's not [RFC 3339](https://tools.ietf.org/html/rfc3339) compliant.",
"type": "string",
Expand Down
31 changes: 20 additions & 11 deletions packages/api-explorer/__tests__/Params.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,27 @@ describe('oneOf/anyOf', () => {
});

describe('schema handling', () => {
describe('minLength / maxLength', () => {
it('should put a `minLength` and `maxLength` attribute on an input', () => {
const params = mount(
<div>
<Params {...props} operation={stringOas.operation('/format-password', 'get')} />
</div>
);
it('should put a `minLength` and `maxLength` attribute on an input', () => {
const params = mount(
<div>
<Params {...props} operation={stringOas.operation('/format-password', 'get')} />
</div>
);

expect(params.find('input')).toHaveLength(1);
expect(params.find('input').props()).toHaveProperty('minLength', 5);
expect(params.find('input').props()).toHaveProperty('maxLength', 20);
});
expect(params.find('input')).toHaveLength(1);
expect(params.find('input').props()).toHaveProperty('minLength', 5);
expect(params.find('input').props()).toHaveProperty('maxLength', 20);
});

it('should put a `pattern` on an input', () => {
const params = mount(
<div>
<Params {...props} operation={stringOas.operation('/format-string-with-pattern', 'get')} />
</div>
);

expect(params.find('input')).toHaveLength(1);
expect(params.find('input').props()).toHaveProperty('pattern', '(\\d{4})-(\\d{2})-(\\d{2})');
});

describe('format', () => {
Expand Down
20 changes: 20 additions & 0 deletions packages/api-explorer/__tests__/__fixtures__/string/oas.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@
}
}
},
"/format-string-with-pattern": {
"get": {
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"string pattern": {
"type": "string",
"format": "date",
"pattern": "(\\d{4})-(\\d{2})-(\\d{2})"
}
}
}
}
}
}
}
},
"/format-unknown": {
"get": {
"requestBody": {
Expand Down
4 changes: 4 additions & 0 deletions packages/oas-form/src/components/widgets/BaseInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ function BaseInput(props) {
inputProps.maxLength = schema.maxLength;
}

if (typeof schema.pattern !== 'undefined') {
inputProps.pattern = schema.pattern;
}

const _onChange = ({ target: { value } }) => {
return props.onChange(value === '' ? options.emptyValue : value);
};
Expand Down