|
| 1 | +import testRule from './__helpers__/testRule'; |
| 2 | +import { DiagnosticSeverity } from '@stoplight/types'; |
| 3 | + |
| 4 | +testRule('xgen-IPA-112-avoid-project-field-names', [ |
| 5 | + { |
| 6 | + name: 'valid schema - no project field names', |
| 7 | + document: { |
| 8 | + components: { |
| 9 | + schemas: { |
| 10 | + SchemaName: { |
| 11 | + properties: { |
| 12 | + group: { type: 'string' }, |
| 13 | + groupId: { type: 'string' }, |
| 14 | + otherField: { type: 'number' }, |
| 15 | + }, |
| 16 | + }, |
| 17 | + }, |
| 18 | + }, |
| 19 | + }, |
| 20 | + errors: [], |
| 21 | + }, |
| 22 | + { |
| 23 | + name: 'invalid schema - with project field name', |
| 24 | + document: { |
| 25 | + components: { |
| 26 | + schemas: { |
| 27 | + SchemaName: { |
| 28 | + properties: { |
| 29 | + project: { type: 'string' }, |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + errors: [ |
| 36 | + { |
| 37 | + code: 'xgen-IPA-112-avoid-project-field-names', |
| 38 | + message: 'Field name "project" should be avoided. Consider using "group" instead.', |
| 39 | + path: ['components', 'schemas', 'SchemaName', 'properties', 'project'], |
| 40 | + severity: DiagnosticSeverity.Warning, |
| 41 | + }, |
| 42 | + ], |
| 43 | + }, |
| 44 | + { |
| 45 | + name: 'invalid schema - with projects field name', |
| 46 | + document: { |
| 47 | + components: { |
| 48 | + schemas: { |
| 49 | + SchemaName: { |
| 50 | + properties: { |
| 51 | + projects: { type: 'array' }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + errors: [ |
| 58 | + { |
| 59 | + code: 'xgen-IPA-112-avoid-project-field-names', |
| 60 | + message: 'Field name "projects" should be avoided. Consider using "group" instead.', |
| 61 | + path: ['components', 'schemas', 'SchemaName', 'properties', 'projects'], |
| 62 | + severity: DiagnosticSeverity.Warning, |
| 63 | + }, |
| 64 | + ], |
| 65 | + }, |
| 66 | + { |
| 67 | + name: 'invalid schema - with projectId field name', |
| 68 | + document: { |
| 69 | + components: { |
| 70 | + schemas: { |
| 71 | + SchemaName: { |
| 72 | + properties: { |
| 73 | + projectId: { type: 'string' }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + errors: [ |
| 80 | + { |
| 81 | + code: 'xgen-IPA-112-avoid-project-field-names', |
| 82 | + message: 'Field name "projectId" should be avoided. Consider using "group" instead.', |
| 83 | + path: ['components', 'schemas', 'SchemaName', 'properties', 'projectId'], |
| 84 | + severity: DiagnosticSeverity.Warning, |
| 85 | + }, |
| 86 | + ], |
| 87 | + }, |
| 88 | + { |
| 89 | + name: 'invalid schema - with different case variants', |
| 90 | + document: { |
| 91 | + components: { |
| 92 | + schemas: { |
| 93 | + SchemaName: { |
| 94 | + properties: { |
| 95 | + Project: { type: 'string' }, |
| 96 | + PROJECTID: { type: 'string' }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + errors: [ |
| 103 | + { |
| 104 | + code: 'xgen-IPA-112-avoid-project-field-names', |
| 105 | + message: 'Field name "Project" should be avoided. Consider using "group" instead.', |
| 106 | + path: ['components', 'schemas', 'SchemaName', 'properties', 'Project'], |
| 107 | + severity: DiagnosticSeverity.Warning, |
| 108 | + }, |
| 109 | + { |
| 110 | + code: 'xgen-IPA-112-avoid-project-field-names', |
| 111 | + message: 'Field name "PROJECTID" should be avoided. Consider using "group" instead.', |
| 112 | + path: ['components', 'schemas', 'SchemaName', 'properties', 'PROJECTID'], |
| 113 | + severity: DiagnosticSeverity.Warning, |
| 114 | + }, |
| 115 | + ], |
| 116 | + }, |
| 117 | + { |
| 118 | + name: 'invalid schema with exception - project field name with exception', |
| 119 | + document: { |
| 120 | + components: { |
| 121 | + schemas: { |
| 122 | + SchemaName: { |
| 123 | + properties: { |
| 124 | + project: { |
| 125 | + type: 'string', |
| 126 | + 'x-xgen-IPA-exception': { |
| 127 | + 'xgen-IPA-112-avoid-project-field-names': 'reason', |
| 128 | + }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + errors: [], |
| 136 | + }, |
| 137 | + { |
| 138 | + name: 'field name containing project substring', |
| 139 | + document: { |
| 140 | + components: { |
| 141 | + schemas: { |
| 142 | + SchemaName: { |
| 143 | + properties: { |
| 144 | + myProjectDetails: { type: 'object' }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + errors: [ |
| 151 | + { |
| 152 | + code: 'xgen-IPA-112-avoid-project-field-names', |
| 153 | + message: 'Field name "myProjectDetails" should be avoided. Consider using "group" instead.', |
| 154 | + path: ['components', 'schemas', 'SchemaName', 'properties', 'myProjectDetails'], |
| 155 | + severity: DiagnosticSeverity.Warning, |
| 156 | + }, |
| 157 | + ], |
| 158 | + }, |
| 159 | + { |
| 160 | + name: 'exception - field with project substring', |
| 161 | + document: { |
| 162 | + components: { |
| 163 | + schemas: { |
| 164 | + SchemaName: { |
| 165 | + properties: { |
| 166 | + myProjectDetails: { |
| 167 | + type: 'object', |
| 168 | + 'x-xgen-IPA-exception': { |
| 169 | + 'xgen-IPA-112-avoid-project-field-names': 'Reason', |
| 170 | + }, |
| 171 | + }, |
| 172 | + }, |
| 173 | + }, |
| 174 | + }, |
| 175 | + }, |
| 176 | + }, |
| 177 | + errors: [], |
| 178 | + }, |
| 179 | + { |
| 180 | + name: 'exception - multiple project fields', |
| 181 | + document: { |
| 182 | + components: { |
| 183 | + schemas: { |
| 184 | + SchemaName: { |
| 185 | + properties: { |
| 186 | + projectId: { |
| 187 | + type: 'string', |
| 188 | + 'x-xgen-IPA-exception': { |
| 189 | + 'xgen-IPA-112-avoid-project-field-names': 'Reason', |
| 190 | + }, |
| 191 | + }, |
| 192 | + projects: { |
| 193 | + type: 'array', |
| 194 | + 'x-xgen-IPA-exception': { |
| 195 | + 'xgen-IPA-112-avoid-project-field-names': 'Reason', |
| 196 | + }, |
| 197 | + }, |
| 198 | + }, |
| 199 | + }, |
| 200 | + }, |
| 201 | + }, |
| 202 | + }, |
| 203 | + errors: [], |
| 204 | + }, |
| 205 | + { |
| 206 | + name: 'mixed valid, invalid, and exception fields', |
| 207 | + document: { |
| 208 | + components: { |
| 209 | + schemas: { |
| 210 | + SchemaName: { |
| 211 | + properties: { |
| 212 | + project: { |
| 213 | + type: 'string', |
| 214 | + 'x-xgen-IPA-exception': { |
| 215 | + 'xgen-IPA-112-avoid-project-field-names': 'Reason', |
| 216 | + }, |
| 217 | + }, |
| 218 | + projectId: { type: 'string' }, |
| 219 | + group: { type: 'string' }, |
| 220 | + }, |
| 221 | + }, |
| 222 | + }, |
| 223 | + }, |
| 224 | + }, |
| 225 | + errors: [ |
| 226 | + { |
| 227 | + code: 'xgen-IPA-112-avoid-project-field-names', |
| 228 | + message: 'Field name "projectId" should be avoided. Consider using "group" instead.', |
| 229 | + path: ['components', 'schemas', 'SchemaName', 'properties', 'projectId'], |
| 230 | + severity: DiagnosticSeverity.Warning, |
| 231 | + }, |
| 232 | + ], |
| 233 | + }, |
| 234 | +]); |
0 commit comments