|
| 1 | +import testRule from './__helpers__/testRule'; |
| 2 | +import { DiagnosticSeverity } from '@stoplight/types'; |
| 3 | + |
| 4 | +testRule('xgen-IPA-112-boolean-field-names-avoid-is-prefix', [ |
| 5 | + { |
| 6 | + name: 'valid schema - no "is" prefix in boolean fields', |
| 7 | + document: { |
| 8 | + components: { |
| 9 | + schemas: { |
| 10 | + SchemaName: { |
| 11 | + properties: { |
| 12 | + enabled: { type: 'boolean' }, |
| 13 | + active: { type: 'boolean' }, |
| 14 | + disabled: { type: 'boolean' }, |
| 15 | + isString: { type: 'string' }, |
| 16 | + visible: { type: 'boolean' }, |
| 17 | + }, |
| 18 | + }, |
| 19 | + }, |
| 20 | + }, |
| 21 | + paths: { |
| 22 | + '/users': { |
| 23 | + post: { |
| 24 | + requestBody: { |
| 25 | + content: { |
| 26 | + 'application/vnd.atlas.2024-01-01+json': { |
| 27 | + schema: { |
| 28 | + type: 'object', |
| 29 | + properties: { |
| 30 | + available: { type: 'boolean' }, |
| 31 | + paused: { type: 'boolean' }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + responses: { |
| 38 | + 201: { |
| 39 | + content: { |
| 40 | + 'application/vnd.atlas.2024-01-01+json': { |
| 41 | + schema: { |
| 42 | + type: 'object', |
| 43 | + properties: { |
| 44 | + valid: { type: 'boolean' }, |
| 45 | + hidden: { type: 'boolean' }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + errors: [], |
| 57 | + }, |
| 58 | + { |
| 59 | + name: 'invalid schema - with "is" prefix in boolean fields', |
| 60 | + document: { |
| 61 | + components: { |
| 62 | + schemas: { |
| 63 | + SchemaName: { |
| 64 | + properties: { |
| 65 | + isEnabled: { type: 'boolean' }, |
| 66 | + isActive: { type: 'boolean' }, |
| 67 | + isError: { type: 'boolean' }, |
| 68 | + isString: { type: 'string' }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + paths: { |
| 74 | + '/users': { |
| 75 | + post: { |
| 76 | + requestBody: { |
| 77 | + content: { |
| 78 | + 'application/vnd.atlas.2024-01-01+json': { |
| 79 | + schema: { |
| 80 | + type: 'object', |
| 81 | + properties: { |
| 82 | + isAvailable: { type: 'boolean' }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + responses: { |
| 89 | + 201: { |
| 90 | + content: { |
| 91 | + 'application/vnd.atlas.2024-01-01+json': { |
| 92 | + schema: { |
| 93 | + type: 'object', |
| 94 | + properties: { |
| 95 | + isValid: { type: 'boolean' }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + errors: [ |
| 107 | + { |
| 108 | + code: 'xgen-IPA-112-boolean-field-names-avoid-is-prefix', |
| 109 | + message: 'Boolean field "isEnabled" should not use the "is" prefix. Use "enabled" instead.', |
| 110 | + path: ['components', 'schemas', 'SchemaName', 'properties', 'isEnabled'], |
| 111 | + severity: DiagnosticSeverity.Warning, |
| 112 | + }, |
| 113 | + { |
| 114 | + code: 'xgen-IPA-112-boolean-field-names-avoid-is-prefix', |
| 115 | + message: 'Boolean field "isActive" should not use the "is" prefix. Use "active" instead.', |
| 116 | + path: ['components', 'schemas', 'SchemaName', 'properties', 'isActive'], |
| 117 | + severity: DiagnosticSeverity.Warning, |
| 118 | + }, |
| 119 | + { |
| 120 | + code: 'xgen-IPA-112-boolean-field-names-avoid-is-prefix', |
| 121 | + message: 'Boolean field "isError" should not use the "is" prefix. Use "error" instead.', |
| 122 | + path: ['components', 'schemas', 'SchemaName', 'properties', 'isError'], |
| 123 | + severity: DiagnosticSeverity.Warning, |
| 124 | + }, |
| 125 | + { |
| 126 | + code: 'xgen-IPA-112-boolean-field-names-avoid-is-prefix', |
| 127 | + message: 'Boolean field "isAvailable" should not use the "is" prefix. Use "available" instead.', |
| 128 | + path: [ |
| 129 | + 'paths', |
| 130 | + '/users', |
| 131 | + 'post', |
| 132 | + 'requestBody', |
| 133 | + 'content', |
| 134 | + 'application/vnd.atlas.2024-01-01+json', |
| 135 | + 'schema', |
| 136 | + 'properties', |
| 137 | + 'isAvailable', |
| 138 | + ], |
| 139 | + severity: DiagnosticSeverity.Warning, |
| 140 | + }, |
| 141 | + { |
| 142 | + code: 'xgen-IPA-112-boolean-field-names-avoid-is-prefix', |
| 143 | + message: 'Boolean field "isValid" should not use the "is" prefix. Use "valid" instead.', |
| 144 | + path: [ |
| 145 | + 'paths', |
| 146 | + '/users', |
| 147 | + 'post', |
| 148 | + 'responses', |
| 149 | + '201', |
| 150 | + 'content', |
| 151 | + 'application/vnd.atlas.2024-01-01+json', |
| 152 | + 'schema', |
| 153 | + 'properties', |
| 154 | + 'isValid', |
| 155 | + ], |
| 156 | + severity: DiagnosticSeverity.Warning, |
| 157 | + }, |
| 158 | + ], |
| 159 | + }, |
| 160 | + { |
| 161 | + name: 'schema with exception - "is" prefix with exception', |
| 162 | + document: { |
| 163 | + components: { |
| 164 | + schemas: { |
| 165 | + SchemaName: { |
| 166 | + properties: { |
| 167 | + isEnabled: { |
| 168 | + type: 'boolean', |
| 169 | + 'x-xgen-IPA-exception': { |
| 170 | + 'xgen-IPA-112-boolean-field-names-avoid-is-prefix': 'Reason', |
| 171 | + }, |
| 172 | + }, |
| 173 | + }, |
| 174 | + }, |
| 175 | + }, |
| 176 | + }, |
| 177 | + }, |
| 178 | + errors: [], |
| 179 | + }, |
| 180 | +]); |
0 commit comments