|
| 1 | +import testRule from './__helpers__/testRule'; |
| 2 | +import { DiagnosticSeverity } from '@stoplight/types'; |
| 3 | + |
| 4 | +const componentSchemas = { |
| 5 | + schemas: { |
| 6 | + SchemaWithReadOnly: { |
| 7 | + type: 'object', |
| 8 | + properties: { |
| 9 | + id: { |
| 10 | + type: 'string', |
| 11 | + readOnly: true, |
| 12 | + }, |
| 13 | + name: { |
| 14 | + type: 'string', |
| 15 | + }, |
| 16 | + }, |
| 17 | + }, |
| 18 | + SchemaWithoutReadOnly: { |
| 19 | + type: 'object', |
| 20 | + properties: { |
| 21 | + name: { |
| 22 | + type: 'string', |
| 23 | + }, |
| 24 | + description: { |
| 25 | + type: 'string', |
| 26 | + }, |
| 27 | + }, |
| 28 | + }, |
| 29 | + NestedSchemaWithReadOnly: { |
| 30 | + type: 'object', |
| 31 | + properties: { |
| 32 | + user: { |
| 33 | + type: 'object', |
| 34 | + properties: { |
| 35 | + userId: { |
| 36 | + type: 'string', |
| 37 | + readOnly: true, |
| 38 | + }, |
| 39 | + username: { |
| 40 | + type: 'string', |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + ArraySchemaWithReadOnly: { |
| 47 | + type: 'object', |
| 48 | + properties: { |
| 49 | + items: { |
| 50 | + type: 'array', |
| 51 | + items: { |
| 52 | + type: 'object', |
| 53 | + properties: { |
| 54 | + itemId: { |
| 55 | + type: 'string', |
| 56 | + readOnly: true, |
| 57 | + }, |
| 58 | + itemName: { |
| 59 | + type: 'string', |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | +}; |
| 68 | + |
| 69 | +testRule('xgen-IPA-106-create-method-request-has-no-readonly-fields', [ |
| 70 | + { |
| 71 | + name: 'valid methods - no readOnly fields', |
| 72 | + document: { |
| 73 | + components: componentSchemas, |
| 74 | + paths: { |
| 75 | + '/valid-resource': { |
| 76 | + post: { |
| 77 | + requestBody: { |
| 78 | + content: { |
| 79 | + 'application/vnd.atlas.2023-01-01+json': { |
| 80 | + schema: { |
| 81 | + $ref: '#/components/schemas/SchemaWithoutReadOnly', |
| 82 | + }, |
| 83 | + }, |
| 84 | + 'application/vnd.atlas.2024-01-01+json': { |
| 85 | + schema: { |
| 86 | + type: 'string', |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + errors: [], |
| 96 | + }, |
| 97 | + { |
| 98 | + name: 'valid methods - custom method can have readOnly fields', |
| 99 | + document: { |
| 100 | + components: componentSchemas, |
| 101 | + paths: { |
| 102 | + '/resource:customAction': { |
| 103 | + post: { |
| 104 | + requestBody: { |
| 105 | + content: { |
| 106 | + 'application/vnd.atlas.2023-01-01+json': { |
| 107 | + schema: { |
| 108 | + $ref: '#/components/schemas/SchemaWithReadOnly', |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + errors: [], |
| 118 | + }, |
| 119 | + { |
| 120 | + name: 'invalid methods - direct readOnly field', |
| 121 | + document: { |
| 122 | + components: componentSchemas, |
| 123 | + paths: { |
| 124 | + '/invalid-resource': { |
| 125 | + post: { |
| 126 | + requestBody: { |
| 127 | + content: { |
| 128 | + 'application/vnd.atlas.2023-01-01+json': { |
| 129 | + schema: { |
| 130 | + $ref: '#/components/schemas/SchemaWithReadOnly', |
| 131 | + }, |
| 132 | + }, |
| 133 | + 'application/vnd.atlas.2024-01-01+json': { |
| 134 | + schema: { |
| 135 | + type: 'string', |
| 136 | + readOnly: true, |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + errors: [ |
| 146 | + { |
| 147 | + code: 'xgen-IPA-106-create-method-request-has-no-readonly-fields', |
| 148 | + message: |
| 149 | + 'The Create method request object must not include input fields (readOnly properties). Found readOnly property at: id. http://go/ipa/106', |
| 150 | + path: ['paths', '/invalid-resource', 'post', 'requestBody', 'content', 'application/vnd.atlas.2023-01-01+json'], |
| 151 | + severity: DiagnosticSeverity.Warning, |
| 152 | + }, |
| 153 | + { |
| 154 | + code: 'xgen-IPA-106-create-method-request-has-no-readonly-fields', |
| 155 | + message: |
| 156 | + 'The Create method request object must not include input fields (readOnly properties). Found readOnly property at one of the inline schemas. http://go/ipa/106', |
| 157 | + path: ['paths', '/invalid-resource', 'post', 'requestBody', 'content', 'application/vnd.atlas.2024-01-01+json'], |
| 158 | + severity: DiagnosticSeverity.Warning, |
| 159 | + }, |
| 160 | + ], |
| 161 | + }, |
| 162 | + { |
| 163 | + name: 'invalid methods - nested readOnly field', |
| 164 | + document: { |
| 165 | + components: componentSchemas, |
| 166 | + paths: { |
| 167 | + '/nested-invalid-resource': { |
| 168 | + post: { |
| 169 | + requestBody: { |
| 170 | + content: { |
| 171 | + 'application/vnd.atlas.2023-01-01+json': { |
| 172 | + schema: { |
| 173 | + $ref: '#/components/schemas/NestedSchemaWithReadOnly', |
| 174 | + }, |
| 175 | + }, |
| 176 | + }, |
| 177 | + }, |
| 178 | + }, |
| 179 | + }, |
| 180 | + }, |
| 181 | + }, |
| 182 | + errors: [ |
| 183 | + { |
| 184 | + code: 'xgen-IPA-106-create-method-request-has-no-readonly-fields', |
| 185 | + message: |
| 186 | + 'The Create method request object must not include input fields (readOnly properties). Found readOnly property at: user.userId. http://go/ipa/106', |
| 187 | + path: [ |
| 188 | + 'paths', |
| 189 | + '/nested-invalid-resource', |
| 190 | + 'post', |
| 191 | + 'requestBody', |
| 192 | + 'content', |
| 193 | + 'application/vnd.atlas.2023-01-01+json', |
| 194 | + ], |
| 195 | + severity: DiagnosticSeverity.Warning, |
| 196 | + }, |
| 197 | + ], |
| 198 | + }, |
| 199 | + { |
| 200 | + name: 'invalid methods - array with readOnly field', |
| 201 | + document: { |
| 202 | + components: componentSchemas, |
| 203 | + paths: { |
| 204 | + '/array-invalid-resource': { |
| 205 | + post: { |
| 206 | + requestBody: { |
| 207 | + content: { |
| 208 | + 'application/vnd.atlas.2023-01-01+json': { |
| 209 | + schema: { |
| 210 | + $ref: '#/components/schemas/ArraySchemaWithReadOnly', |
| 211 | + }, |
| 212 | + }, |
| 213 | + }, |
| 214 | + }, |
| 215 | + }, |
| 216 | + }, |
| 217 | + }, |
| 218 | + }, |
| 219 | + errors: [ |
| 220 | + { |
| 221 | + code: 'xgen-IPA-106-create-method-request-has-no-readonly-fields', |
| 222 | + message: |
| 223 | + 'The Create method request object must not include input fields (readOnly properties). Found readOnly property at: items.items.itemId. http://go/ipa/106', |
| 224 | + path: [ |
| 225 | + 'paths', |
| 226 | + '/array-invalid-resource', |
| 227 | + 'post', |
| 228 | + 'requestBody', |
| 229 | + 'content', |
| 230 | + 'application/vnd.atlas.2023-01-01+json', |
| 231 | + ], |
| 232 | + severity: DiagnosticSeverity.Warning, |
| 233 | + }, |
| 234 | + ], |
| 235 | + }, |
| 236 | + { |
| 237 | + name: 'methods with exceptions', |
| 238 | + document: { |
| 239 | + components: componentSchemas, |
| 240 | + paths: { |
| 241 | + '/excepted-resource': { |
| 242 | + post: { |
| 243 | + requestBody: { |
| 244 | + content: { |
| 245 | + 'application/vnd.atlas.2023-01-01+json': { |
| 246 | + schema: { |
| 247 | + $ref: '#/components/schemas/SchemaWithReadOnly', |
| 248 | + }, |
| 249 | + 'x-xgen-IPA-exception': { |
| 250 | + 'xgen-IPA-106-create-method-request-has-no-readonly-fields': 'Reason', |
| 251 | + }, |
| 252 | + }, |
| 253 | + }, |
| 254 | + }, |
| 255 | + }, |
| 256 | + }, |
| 257 | + }, |
| 258 | + }, |
| 259 | + errors: [], |
| 260 | + }, |
| 261 | +]); |
0 commit comments