Skip to content

Commit ee85aac

Browse files
feat: display numeric constraints for parameters
Add formatNumericConstraints function to display minimum, maximum, exclusiveMinimum, and exclusiveMaximum constraints for path and query parameters in the documentation. - Add constraint formatting with proper mathematical symbols (, , >, <) - Support range notation for combined constraints [min, max], (min, max) - Display constraints in parameter description area - Add comprehensive unit tests for all constraint types - Work in both documentation view and Try it out mode
1 parent 443c011 commit ee85aac

17 files changed

+661
-179
lines changed

demo-constraints.html

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Swagger UI Constraints Demo</title>
5+
<style>
6+
body { font-family: Arial, sans-serif; padding: 20px; }
7+
.parameter__constraint {
8+
color: #7d7d7d;
9+
font-style: italic;
10+
margin: 5px 0;
11+
}
12+
.test-case {
13+
border: 1px solid #ccc;
14+
margin: 10px 0;
15+
padding: 10px;
16+
background: #f9f9f9;
17+
}
18+
</style>
19+
</head>
20+
<body>
21+
<h1>Swagger UI Parameter Constraints Demo</h1>
22+
<p>This demonstrates how numeric constraints will be displayed for OpenAPI parameters.</p>
23+
24+
<div class="test-case">
25+
<h3>Path Parameter: /pets/{id}</h3>
26+
<strong>id</strong> <em>(path parameter)</em><br>
27+
Pet ID - must be a positive integer<br>
28+
<div class="parameter__constraint"><i>Constraints</i> : ≥ 1</div>
29+
</div>
30+
31+
<div class="test-case">
32+
<h3>Query Parameter: limit</h3>
33+
<strong>limit</strong> <em>(query parameter)</em><br>
34+
Maximum number of results<br>
35+
<div class="parameter__constraint"><i>Constraints</i> : [1, 100)</div>
36+
</div>
37+
38+
<div class="test-case">
39+
<h3>Query Parameter: price_min</h3>
40+
<strong>price_min</strong> <em>(query parameter)</em><br>
41+
Minimum price filter<br>
42+
<div class="parameter__constraint"><i>Constraints</i> : > 0.01</div>
43+
</div>
44+
45+
<div class="test-case">
46+
<h3>Query Parameter: price_max</h3>
47+
<strong>price_max</strong> <em>(query parameter)</em><br>
48+
Maximum price filter<br>
49+
<div class="parameter__constraint"><i>Constraints</i> : ≤ 999.99</div>
50+
</div>
51+
52+
<script>
53+
// Test our constraint function
54+
const formatNumericConstraints = (schema) => {
55+
if (!schema) return null
56+
57+
const minimum = schema.minimum
58+
const maximum = schema.maximum
59+
const exclusiveMinimum = schema.exclusiveMinimum
60+
const exclusiveMaximum = schema.exclusiveMaximum
61+
62+
const hasMinimum = typeof minimum === "number"
63+
const hasMaximum = typeof maximum === "number"
64+
const hasExclusiveMinimum = typeof exclusiveMinimum === "number"
65+
const hasExclusiveMaximum = typeof exclusiveMaximum === "number"
66+
67+
if (!hasMinimum && !hasMaximum && !hasExclusiveMinimum && !hasExclusiveMaximum) {
68+
return null
69+
}
70+
71+
const isMinExclusive = hasExclusiveMinimum && (!hasMinimum || minimum < exclusiveMinimum)
72+
const isMaxExclusive = hasExclusiveMaximum && (!hasMaximum || maximum > exclusiveMaximum)
73+
74+
if ((hasMinimum || hasExclusiveMinimum) && (hasMaximum || hasExclusiveMaximum)) {
75+
const minSymbol = isMinExclusive ? "(" : "["
76+
const maxSymbol = isMaxExclusive ? ")" : "]"
77+
const minValue = isMinExclusive ? exclusiveMinimum : minimum
78+
const maxValue = isMaxExclusive ? exclusiveMaximum : maximum
79+
return `${minSymbol}${minValue}, ${maxValue}${maxSymbol}`
80+
}
81+
if (hasMinimum || hasExclusiveMinimum) {
82+
const minSymbol = isMinExclusive ? ">" : "≥"
83+
const minValue = isMinExclusive ? exclusiveMinimum : minimum
84+
return `${minSymbol} ${minValue}`
85+
}
86+
if (hasMaximum || hasExclusiveMaximum) {
87+
const maxSymbol = isMaxExclusive ? "<" : "≤"
88+
const maxValue = isMaxExclusive ? exclusiveMaximum : maximum
89+
return `${maxSymbol} ${maxValue}`
90+
}
91+
92+
return null
93+
}
94+
95+
// Verify our test cases work correctly
96+
console.log("Function test results:")
97+
console.log("Case 1:", formatNumericConstraints({ minimum: 1 })) // Should be "≥ 1"
98+
console.log("Case 2:", formatNumericConstraints({ minimum: 1, maximum: 100, exclusiveMaximum: true })) // Should be "[1, 100)"
99+
console.log("Case 3:", formatNumericConstraints({ exclusiveMinimum: 0.01 })) // Should be "> 0.01"
100+
console.log("Case 4:", formatNumericConstraints({ maximum: 999.99 })) // Should be "≤ 999.99"
101+
</script>
102+
</body>
103+
</html>

dist/swagger-ui-bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*!
2+
Copyright (c) 2018 Jed Watson.
3+
Licensed under the MIT License (MIT), see
4+
http://jedwatson.github.io/classnames
5+
*/
6+
7+
/*!
8+
* @description Recursive object extending
9+
* @author Viacheslav Lotsmanov <[email protected]>
10+
* @license MIT
11+
*
12+
* The MIT License (MIT)
13+
*
14+
* Copyright (c) 2013-2018 Viacheslav Lotsmanov
15+
*
16+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
17+
* this software and associated documentation files (the "Software"), to deal in
18+
* the Software without restriction, including without limitation the rights to
19+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
20+
* the Software, and to permit persons to whom the Software is furnished to do so,
21+
* subject to the following conditions:
22+
*
23+
* The above copyright notice and this permission notice shall be included in all
24+
* copies or substantial portions of the Software.
25+
*
26+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
28+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
29+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
30+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32+
*/
33+
34+
/*!
35+
* The buffer module from node.js, for the browser.
36+
*
37+
* @author Feross Aboukhadijeh <https://feross.org>
38+
* @license MIT
39+
*/
40+
41+
/*!
42+
* https://github.com/Starcounter-Jack/JSON-Patch
43+
* (c) 2017-2021 Joachim Wester
44+
* MIT license
45+
*/
46+
47+
/*!
48+
* https://github.com/Starcounter-Jack/JSON-Patch
49+
* (c) 2017-2022 Joachim Wester
50+
* MIT licensed
51+
*/
52+
53+
/*!
54+
* repeat-string <https://github.com/jonschlinkert/repeat-string>
55+
*
56+
* Copyright (c) 2014-2015, Jon Schlinkert.
57+
* Licensed under the MIT License.
58+
*/
59+
60+
/*! @license DOMPurify 3.2.6 | (c) Cure53 and other contributors | Released under the Apache license 2.0 and Mozilla Public License 2.0 | github.com/cure53/DOMPurify/blob/3.2.6/LICENSE */
61+
62+
/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
63+
64+
/*! js-yaml 4.1.0 https://github.com/nodeca/js-yaml @license MIT */
65+
66+
/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
67+
68+
/**
69+
* @license React
70+
* react-dom.production.min.js
71+
*
72+
* Copyright (c) Facebook, Inc. and its affiliates.
73+
*
74+
* This source code is licensed under the MIT license found in the
75+
* LICENSE file in the root directory of this source tree.
76+
*/
77+
78+
/**
79+
* @license React
80+
* react.production.min.js
81+
*
82+
* Copyright (c) Facebook, Inc. and its affiliates.
83+
*
84+
* This source code is licensed under the MIT license found in the
85+
* LICENSE file in the root directory of this source tree.
86+
*/
87+
88+
/**
89+
* @license React
90+
* scheduler.production.min.js
91+
*
92+
* Copyright (c) Facebook, Inc. and its affiliates.
93+
*
94+
* This source code is licensed under the MIT license found in the
95+
* LICENSE file in the root directory of this source tree.
96+
*/
97+
98+
/**
99+
* @license React
100+
* use-sync-external-store-with-selector.production.js
101+
*
102+
* Copyright (c) Meta Platforms, Inc. and affiliates.
103+
*
104+
* This source code is licensed under the MIT license found in the
105+
* LICENSE file in the root directory of this source tree.
106+
*/

dist/swagger-ui-es-bundle-core.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*!
2+
* @description Recursive object extending
3+
* @author Viacheslav Lotsmanov <[email protected]>
4+
* @license MIT
5+
*
6+
* The MIT License (MIT)
7+
*
8+
* Copyright (c) 2013-2018 Viacheslav Lotsmanov
9+
*
10+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
11+
* this software and associated documentation files (the "Software"), to deal in
12+
* the Software without restriction, including without limitation the rights to
13+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
14+
* the Software, and to permit persons to whom the Software is furnished to do so,
15+
* subject to the following conditions:
16+
*
17+
* The above copyright notice and this permission notice shall be included in all
18+
* copies or substantial portions of the Software.
19+
*
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
22+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
23+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26+
*/
27+
28+
/*!
29+
* The buffer module from node.js, for the browser.
30+
*
31+
* @author Feross Aboukhadijeh <https://feross.org>
32+
* @license MIT
33+
*/
34+
35+
/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */

dist/swagger-ui-es-bundle-core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/swagger-ui-es-bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*!
2+
Copyright (c) 2018 Jed Watson.
3+
Licensed under the MIT License (MIT), see
4+
http://jedwatson.github.io/classnames
5+
*/
6+
7+
/*!
8+
* @description Recursive object extending
9+
* @author Viacheslav Lotsmanov <[email protected]>
10+
* @license MIT
11+
*
12+
* The MIT License (MIT)
13+
*
14+
* Copyright (c) 2013-2018 Viacheslav Lotsmanov
15+
*
16+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
17+
* this software and associated documentation files (the "Software"), to deal in
18+
* the Software without restriction, including without limitation the rights to
19+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
20+
* the Software, and to permit persons to whom the Software is furnished to do so,
21+
* subject to the following conditions:
22+
*
23+
* The above copyright notice and this permission notice shall be included in all
24+
* copies or substantial portions of the Software.
25+
*
26+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
28+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
29+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
30+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32+
*/
33+
34+
/*!
35+
* The buffer module from node.js, for the browser.
36+
*
37+
* @author Feross Aboukhadijeh <https://feross.org>
38+
* @license MIT
39+
*/
40+
41+
/*!
42+
* https://github.com/Starcounter-Jack/JSON-Patch
43+
* (c) 2017-2021 Joachim Wester
44+
* MIT license
45+
*/
46+
47+
/*!
48+
* https://github.com/Starcounter-Jack/JSON-Patch
49+
* (c) 2017-2022 Joachim Wester
50+
* MIT licensed
51+
*/
52+
53+
/*!
54+
* repeat-string <https://github.com/jonschlinkert/repeat-string>
55+
*
56+
* Copyright (c) 2014-2015, Jon Schlinkert.
57+
* Licensed under the MIT License.
58+
*/
59+
60+
/*! @license DOMPurify 3.2.6 | (c) Cure53 and other contributors | Released under the Apache license 2.0 and Mozilla Public License 2.0 | github.com/cure53/DOMPurify/blob/3.2.6/LICENSE */
61+
62+
/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
63+
64+
/*! js-yaml 4.1.0 https://github.com/nodeca/js-yaml @license MIT */
65+
66+
/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
67+
68+
/**
69+
* @license React
70+
* react-dom.production.min.js
71+
*
72+
* Copyright (c) Facebook, Inc. and its affiliates.
73+
*
74+
* This source code is licensed under the MIT license found in the
75+
* LICENSE file in the root directory of this source tree.
76+
*/
77+
78+
/**
79+
* @license React
80+
* react.production.min.js
81+
*
82+
* Copyright (c) Facebook, Inc. and its affiliates.
83+
*
84+
* This source code is licensed under the MIT license found in the
85+
* LICENSE file in the root directory of this source tree.
86+
*/
87+
88+
/**
89+
* @license React
90+
* scheduler.production.min.js
91+
*
92+
* Copyright (c) Facebook, Inc. and its affiliates.
93+
*
94+
* This source code is licensed under the MIT license found in the
95+
* LICENSE file in the root directory of this source tree.
96+
*/
97+
98+
/**
99+
* @license React
100+
* use-sync-external-store-with-selector.production.js
101+
*
102+
* Copyright (c) Meta Platforms, Inc. and affiliates.
103+
*
104+
* This source code is licensed under the MIT license found in the
105+
* LICENSE file in the root directory of this source tree.
106+
*/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*!
2+
* The buffer module from node.js, for the browser.
3+
*
4+
* @author Feross Aboukhadijeh <https://feross.org>
5+
* @license MIT
6+
*/
7+
8+
/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
9+
10+
/*! js-yaml 4.1.0 https://github.com/nodeca/js-yaml @license MIT */
11+
12+
/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
13+
14+
/**
15+
* @license React
16+
* react.production.min.js
17+
*
18+
* Copyright (c) Facebook, Inc. and its affiliates.
19+
*
20+
* This source code is licensed under the MIT license found in the
21+
* LICENSE file in the root directory of this source tree.
22+
*/

dist/swagger-ui.css.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)