Skip to content

Commit 0089093

Browse files
committed
Add field names and OpenAPI 3.1 support to AEP-126 nullable enum rules
1 parent 6b72155 commit 0089093

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

functions/aep-126-enum-null-first.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@
44
* Based on AEP-126 specification (https://aep.dev/126).
55
*
66
* @param {object} field - The field object containing the enum
7+
* @param {object} _opts - Options (unused)
8+
* @param {object} context - Spectral context containing the path
79
* @returns {Array<object>} Array of error objects, or empty array if valid
810
*/
9-
module.exports = (field) => {
11+
module.exports = (field, _opts, context) => {
1012
if (!field || typeof field !== 'object') {
1113
return [];
1214
}
1315

1416
// Check if field is nullable
15-
if (field.nullable !== true) {
17+
// OpenAPI 3.0: nullable: true
18+
// OpenAPI 3.1: type: ['string', 'null'] or type: 'null'
19+
const isNullable = field.nullable === true ||
20+
(Array.isArray(field.type) && field.type.includes('null'));
21+
22+
if (!isNullable) {
1623
return [];
1724
}
1825

@@ -30,9 +37,10 @@ module.exports = (field) => {
3037

3138
// Check if null is the first value
3239
if (enumValues[0] !== null) {
40+
const fieldName = context.path[context.path.length - 1];
3341
return [
3442
{
35-
message: 'When enum contains "null" and field is nullable, "null" must be the first value.',
43+
message: `Enum field "${fieldName}" contains "null" and is nullable, but "null" must be the first value.`,
3644
},
3745
];
3846
}

functions/aep-126-enum-nullable-declaration.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
* Based on AEP-126 specification (https://aep.dev/126).
55
*
66
* @param {object} field - The field object containing the enum
7+
* @param {object} _opts - Options (unused)
8+
* @param {object} context - Spectral context containing the path
79
* @returns {Array<object>} Array of error objects, or empty array if valid
810
*/
9-
module.exports = (field) => {
11+
module.exports = (field, _opts, context) => {
1012
if (!field || typeof field !== 'object') {
1113
return [];
1214
}
@@ -23,11 +25,17 @@ module.exports = (field) => {
2325
return [];
2426
}
2527

26-
// Check if nullable is declared as true
27-
if (field.nullable !== true) {
28+
// Check if nullable is declared
29+
// OpenAPI 3.0: nullable: true
30+
// OpenAPI 3.1: type: ['string', 'null'] or type: 'null'
31+
const isNullable = field.nullable === true ||
32+
(Array.isArray(field.type) && field.type.includes('null'));
33+
34+
if (!isNullable) {
35+
const fieldName = context.path[context.path.length - 1];
2836
return [
2937
{
30-
message: 'Enum contains "null" value but field does not declare "nullable: true".',
38+
message: `Enum field "${fieldName}" contains "null" value but does not declare nullable (use "nullable: true" in OAS 3.0 or "type: ['string', 'null']" in OAS 3.1).`,
3139
},
3240
];
3341
}

0 commit comments

Comments
 (0)