Skip to content

Commit e962e25

Browse files
committed
feat(build): extract all constraints to OAS3
1 parent 0a5d083 commit e962e25

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

packages/build/src/lib/spec-generator/oas3/oas3SpecGenerator.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ import { SpecGenerator } from '../specGenerator';
22
import * as oas3 from 'openapi3-ts';
33
import {
44
APISchema,
5+
EnumConstraint,
56
ErrorInfo,
67
FieldDataType,
78
FieldInType,
9+
MaxLengthConstraint,
10+
MaxValueConstraint,
11+
MinLengthConstraint,
812
MinValueConstraint,
13+
RegexConstraint,
914
RequestParameter,
1015
RequiredConstraint,
1116
ResponseProperty,
@@ -105,10 +110,22 @@ export class OAS3SpecGenerator extends SpecGenerator<oas3.OpenAPIObject> {
105110
const schema: oas3.SchemaObject = {
106111
type: this.convertFieldDataTypeToOASType(parameter.type),
107112
};
108-
const minValueConstraint = parameter.constraints.find(
109-
(constraint) => constraint instanceof MinValueConstraint
110-
) as MinValueConstraint;
111-
if (minValueConstraint) schema.minimum = minValueConstraint.getMinValue();
113+
114+
for (const constraint of parameter.constraints) {
115+
if (constraint instanceof MinValueConstraint) {
116+
schema.minimum = constraint.getMinValue();
117+
} else if (constraint instanceof MaxValueConstraint) {
118+
schema.maximum = constraint.getMaxValue();
119+
} else if (constraint instanceof MinLengthConstraint) {
120+
schema.minLength = constraint.getMinLength();
121+
} else if (constraint instanceof MaxLengthConstraint) {
122+
schema.maxLength = constraint.getMaxLength();
123+
} else if (constraint instanceof RegexConstraint) {
124+
schema.pattern = constraint.getRegex();
125+
} else if (constraint instanceof EnumConstraint) {
126+
schema.enum = constraint.getList();
127+
}
128+
}
112129

113130
return schema;
114131
}

packages/build/test/spec-generator/oas3.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ it('Should extract the correct parameters', async () => {
4646
in: 'path',
4747
schema: expect.objectContaining({
4848
type: 'string',
49+
minLength: 3,
50+
maxLength: 10,
51+
pattern: '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}',
4952
}),
5053
required: true,
5154
})
@@ -57,6 +60,7 @@ it('Should extract the correct parameters', async () => {
5760
schema: expect.objectContaining({
5861
type: 'number',
5962
minimum: 10,
63+
maximum: 15,
6064
}),
6165
required: false,
6266
})
@@ -71,6 +75,17 @@ it('Should extract the correct parameters', async () => {
7175
required: false,
7276
})
7377
);
78+
expect(spec.paths['/user/{id}']?.get.parameters[3]).toEqual(
79+
expect.objectContaining({
80+
name: 'username',
81+
in: 'query',
82+
schema: expect.objectContaining({
83+
type: 'string',
84+
enum: ['ivan', 'eason', 'freda'],
85+
}),
86+
required: false,
87+
})
88+
);
7489
});
7590

7691
it('Should extract the correct response', async () => {

packages/build/test/spec-generator/schema.ts

+35
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,41 @@ const getStubLoader = () => {
5050
validateData: () => true,
5151
getConstraints: (args) => [Constraint.MinValue(args.value)],
5252
};
53+
case 'maxValue':
54+
return {
55+
name: 'maxValue',
56+
validateSchema: () => true,
57+
validateData: () => true,
58+
getConstraints: (args) => [Constraint.MaxValue(args.value)],
59+
};
60+
case 'minLength':
61+
return {
62+
name: 'minLength',
63+
validateSchema: () => true,
64+
validateData: () => true,
65+
getConstraints: (args) => [Constraint.MinLength(args.value)],
66+
};
67+
case 'maxLength':
68+
return {
69+
name: 'maxLength',
70+
validateSchema: () => true,
71+
validateData: () => true,
72+
getConstraints: (args) => [Constraint.MaxLength(args.value)],
73+
};
74+
case 'regex':
75+
return {
76+
name: 'regex',
77+
validateSchema: () => true,
78+
validateData: () => true,
79+
getConstraints: (args) => [Constraint.Regex(args.value)],
80+
};
81+
case 'enum':
82+
return {
83+
name: 'enum',
84+
validateSchema: () => true,
85+
validateData: () => true,
86+
getConstraints: (args) => [Constraint.Enum(args.value)],
87+
};
5388
default:
5489
throw new Error(`Validator ${name} is not implemented in test bed.`);
5590
}

packages/build/test/spec-generator/schemas/0-user.yaml

+22
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,38 @@ request:
55
fieldIn: path
66
validators:
77
- required
8+
- name: minLength
9+
args:
10+
value: 3
11+
- name: maxLength
12+
args:
13+
value: 10
14+
- name: regex
15+
args:
16+
value: '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
817
- fieldName: agent
918
fieldIn: header
1019
type: number
1120
validators:
1221
- name: minValue
1322
args:
1423
value: 10
24+
- name: maxValue
25+
args:
26+
value: 15
1527
- fieldName: force
1628
fieldIn: query
1729
type: boolean
30+
- fieldName: username
31+
fieldIn: query
32+
type: string
33+
validators:
34+
- name: enum
35+
args:
36+
value:
37+
- ivan
38+
- eason
39+
- freda
1840
description: Get user information
1941
response:
2042
- name: id

0 commit comments

Comments
 (0)