Skip to content

Commit

Permalink
WIP: Support constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendan Abbott committed May 26, 2017
1 parent c774d99 commit a7ff1e2
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/components/Property/Property.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,15 @@ Property.propTypes = {
name: PropTypes.string.isRequired,
type: PropTypes.arrayOf(PropTypes.string).isRequired,
subtype: PropTypes.string,
title: PropTypes.string,
description: PropTypes.string,
constraints: PropTypes.shape({
format: PropTypes.string,
minLength: PropTypes.number,
exclusiveMinimum: PropTypes.bool,
maxLength: PropTypes.number,
exclusiveMaximum: PropTypes.bool
}),
enumValues: PropTypes.array,
defaultValue: PropTypes.any,
isRequired: PropTypes.bool,
Expand Down
35 changes: 35 additions & 0 deletions src/parser/open-api/constraintsParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// https://github.com/OAI/OpenAPI-Specification/blob/OpenAPI.next/versions/3.0.md#schema-object
const VALIDATION_KEYWORDS = [
'format',
'exclusiveMaximum',
'exclusiveMinimum',
'maximum',
'maxItems',
'maxLength',
'maxProperties',
'minimum',
'minItems',
'minLength',
'minProperties',
'multipleOf',
'pattern',
'uniqueItems'
];

/**
* Determines if the given property contains any validation keywords
*
* @param {Object} property
* @return {Boolean}
*/
export function hasConstraints(property) {
return Object.keys(property).some(
(key) => VALIDATION_KEYWORDS.includes(key)
);
}

export function getConstraints(property) {
return {

};
}
11 changes: 10 additions & 1 deletion src/parser/open-api/schemaParser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { resolveAllOf } from './allOfResolver';
import { hasConstraints, getConstraints } from './constraintsParser';

const literalTypes = ['string', 'integer', 'number', 'boolean'];

Expand All @@ -24,14 +25,22 @@ function getPropertyNode(nodeName, propertyNode, required = false) {
required
};

if (propertyNode.title) {
outputNode.title = propertyNode.title;
}

if (propertyNode.description) {
outputNode.description = propertyNode.description;
}

if (propertyNode.default !== undefined) {
if (propertyNode.default) {
outputNode.defaultValue = propertyNode.default;
}

if (hasConstraints(propertyNode)) {
outputNode.constraints = getConstraints(propertyNode);
}

// Are all the possible types for this property literals?
// TODO: Currently do not handle multiple types that are not all literals
if (nodeType.every((type) => literalTypes.includes(type))) {
Expand Down
6 changes: 5 additions & 1 deletion test/parser/open-api/data/schemaParser/inputSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,17 @@
"type": "string"
},
"email": {
"title": "Email Address",
"description": "Email of the default user",
"type": "string",
"format": "email"
},
"password": {
"description": "User password",
"type": "string"
"type": "string",
"format": "password",
"minLength": 6,
"maxLength": 13
},
"ipAddress": {
"description": "IP address where the request came from",
Expand Down
13 changes: 11 additions & 2 deletions test/parser/open-api/data/schemaParser/outputSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,29 @@
]
},
{
"title": "Email Address",
"description": "Email of the default user",
"name": "email",
"required": true,
"type": [
"string"
]
],
"constraints": {
"format": "email"
}
},
{
"description": "User password",
"name": "password",
"required": false,
"type": [
"string"
]
],
"constraints": {
"format": "password",
"minLength": 6,
"maxLength": 13
}
},
{
"description": "IP address where the request came from",
Expand Down

0 comments on commit a7ff1e2

Please sign in to comment.