Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,147 @@ testRule('xgen-IPA-112-boolean-field-names-avoid-is-prefix', [
},
errors: [],
},
{
name: 'schema with referenced property types',
document: {
components: {
schemas: {
BooleanProperties: {
type: 'object',
properties: {
active: { type: 'boolean' },
isEnabled: { type: 'boolean' },
disabled: { type: 'boolean' },
},
},
User: {
type: 'object',
properties: {
userId: { type: 'string' },
name: { type: 'string' },
status: { $ref: '#/components/schemas/BooleanProperties' },
isAdmin: { type: 'boolean' },
preferences: {
type: 'object',
properties: {
isEmailNotificationsEnabled: { type: 'boolean' },
darkMode: { type: 'boolean' },
},
},
},
},
},
},
paths: {
'/users/{userId}': {
get: {
responses: {
200: {
content: {
'application/vnd.atlas.2024-01-01+json': {
schema: {
type: 'object',
properties: {
user: { $ref: '#/components/schemas/User' },
isCached: { type: 'boolean' },
},
},
},
},
},
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-112-boolean-field-names-avoid-is-prefix',
message: 'Boolean field "isEnabled" should not use the "is" prefix. Use "enabled" instead.',
path: ['components', 'schemas', 'BooleanProperties', 'properties', 'isEnabled'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-112-boolean-field-names-avoid-is-prefix',
message: 'Boolean field "isAdmin" should not use the "is" prefix. Use "admin" instead.',
path: ['components', 'schemas', 'User', 'properties', 'isAdmin'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-112-boolean-field-names-avoid-is-prefix',
message:
'Boolean field "isEmailNotificationsEnabled" should not use the "is" prefix. Use "emailNotificationsEnabled" instead.',
path: [
'components',
'schemas',
'User',
'properties',
'preferences',
'properties',
'isEmailNotificationsEnabled',
],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-112-boolean-field-names-avoid-is-prefix',
message: 'Boolean field "isCached" should not use the "is" prefix. Use "cached" instead.',
path: [
'paths',
'/users/{userId}',
'get',
'responses',
'200',
'content',
'application/vnd.atlas.2024-01-01+json',
'schema',
'properties',
'isCached',
],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'schema with referenced property and exceptions',
document: {
components: {
schemas: {
UserSettings: {
type: 'object',
properties: {
isVerified: {
type: 'boolean',
'x-xgen-IPA-exception': {
'xgen-IPA-112-boolean-field-names-avoid-is-prefix': 'Reason',
},
},
isMfaEnabled: { type: 'boolean' },
},
},
UserProfile: {
type: 'object',
properties: {
name: { type: 'string' },
email: { type: 'string' },
settings: { $ref: '#/components/schemas/UserSettings' },
isPremiumUser: {
type: 'boolean',
'x-xgen-IPA-exception': {
'xgen-IPA-112-boolean-field-names-avoid-is-prefix': 'Reason',
},
},
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-112-boolean-field-names-avoid-is-prefix',
message: 'Boolean field "isMfaEnabled" should not use the "is" prefix. Use "mfaEnabled" instead.',
path: ['components', 'schemas', 'UserSettings', 'properties', 'isMfaEnabled'],
severity: DiagnosticSeverity.Warning,
},
],
},
]);
148 changes: 148 additions & 0 deletions tools/spectral/ipa/__tests__/IPA112FieldNamesAreCamelCase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,152 @@ testRule('xgen-IPA-112-field-names-are-camel-case', [
},
errors: [],
},
{
name: 'schema with referenced property and nested objects',
document: {
components: {
schemas: {
Address: {
type: 'object',
properties: {
State_Name: { type: 'string' },
zipCode: { type: 'string' },
},
},
User: {
type: 'object',
properties: {
userId: { type: 'string' },
firstName: { type: 'string' },
Last_Name: { type: 'string' },
primary_address: { $ref: '#/components/schemas/Address' },
contactInfo: {
type: 'object',
properties: {
email_address: { type: 'string' },
phoneNumber: { type: 'string' },
},
},
},
},
},
},
paths: {
'/users/{userId}': {
get: {
responses: {
200: {
content: {
'application/vnd.atlas.2024-01-01+json': {
schema: {
type: 'object',
properties: {
primary_user: { $ref: '#/components/schemas/User' },
REQUEST_ID: { type: 'string' },
},
},
},
},
},
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-112-field-names-are-camel-case',
message: 'Property "State_Name" must use camelCase format.',
path: ['components', 'schemas', 'Address', 'properties', 'State_Name'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-112-field-names-are-camel-case',
message: 'Property "Last_Name" must use camelCase format.',
path: ['components', 'schemas', 'User', 'properties', 'Last_Name'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-112-field-names-are-camel-case',
message: 'Property "primary_address" must use camelCase format.',
path: ['components', 'schemas', 'User', 'properties', 'primary_address'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-112-field-names-are-camel-case',
message: 'Property "email_address" must use camelCase format.',
path: ['components', 'schemas', 'User', 'properties', 'contactInfo', 'properties', 'email_address'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-112-field-names-are-camel-case',
message: 'Property "primary_user" must use camelCase format.',
path: [
'paths',
'/users/{userId}',
'get',
'responses',
'200',
'content',
'application/vnd.atlas.2024-01-01+json',
'schema',
'properties',
'primary_user',
],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-112-field-names-are-camel-case',
message: 'Property "REQUEST_ID" must use camelCase format.',
path: [
'paths',
'/users/{userId}',
'get',
'responses',
'200',
'content',
'application/vnd.atlas.2024-01-01+json',
'schema',
'properties',
'REQUEST_ID',
],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'schema with referenced property and exceptions',
document: {
components: {
schemas: {
ApiSettings: {
type: 'object',
properties: {
API_KEY: {
type: 'string',
'x-xgen-IPA-exception': {
'xgen-IPA-112-field-names-are-camel-case': 'Reason',
},
},
},
},
UserProfile: {
type: 'object',
properties: {
name: { type: 'string' },
email: { type: 'string' },
apiSettings: { $ref: '#/components/schemas/ApiSettings' },
User_Status: {
type: 'string',
'x-xgen-IPA-exception': {
'xgen-IPA-112-field-names-are-camel-case': 'Reason',
},
},
},
},
},
},
},
errors: [],
},
]);
21 changes: 12 additions & 9 deletions tools/spectral/ipa/rulesets/IPA-112.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ rules:
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-112-avoid-project-field-names'
severity: warn
given:
- '$.components.schemas..properties[*]~'
- '$.paths..requestBody.content[?(@property.match(/json$/i))].schema..properties[*]~'
- '$.paths..responses..content[?(@property.match(/json$/i))].schema..properties[*]~'
- '$.components.schemas..properties'
- '$.paths..requestBody.content[?(@property.match(/json$/i))].schema..properties'
- '$.paths..responses..content[?(@property.match(/json$/i))].schema..properties'
then:
field: '@key'
function: 'IPA112AvoidProjectFieldNames'
functionOptions:
prohibitedFieldNames:
Expand All @@ -46,10 +47,11 @@ rules:
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-112-field-names-are-camel-case'
severity: warn
given:
- '$.components.schemas..properties[*]~'
- '$.paths..requestBody.content[?(@property.match(/json$/i))].schema..properties[*]~'
- '$.paths..responses..content[?(@property.match(/json$/i))].schema..properties[*]~'
- '$.components.schemas..properties'
- '$.paths..requestBody.content[?(@property.match(/json$/i))].schema..properties'
- '$.paths..responses..content[?(@property.match(/json$/i))].schema..properties'
then:
field: '@key'
function: 'IPA112FieldNamesAreCamelCase'
xgen-IPA-112-boolean-field-names-avoid-is-prefix:
description: |
Expand All @@ -63,8 +65,9 @@ rules:
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-112-boolean-field-names-avoid-is-prefix'
severity: warn
given:
- '$.components.schemas..properties[*]~'
- '$.paths..requestBody.content[?(@property.match(/json$/i))].schema..properties[*]~'
- '$.paths..responses..content[?(@property.match(/json$/i))].schema..properties[*]~'
- '$.components.schemas..properties'
- '$.paths..requestBody.content[?(@property.match(/json$/i))].schema..properties'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] JSON filtering can be done as option for rule configuration and checked in code.

- '$.paths..responses..content[?(@property.match(/json$/i))].schema..properties'
then:
field: '@key'
function: 'IPA112BooleanFieldNamesAvoidIsPrefix'
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ import { splitCamelCase } from './utils/schemaUtils.js';
const RULE_NAME = 'xgen-IPA-112-avoid-project-field-names';

export default (input, options, { path, documentInventory }) => {
const oas = documentInventory.resolved;
const oas = documentInventory.unresolved;
const property = resolveObject(oas, path);

// Skip schema references ($ref):
// Referenced schemas are validated separately to prevent duplicate violations
if (!property) {
return;
}

const ignoreList = options?.ignore || [];
if (ignoreList.some((ignoreTerm) => input.toLowerCase().includes(ignoreTerm))) {
return;
Expand Down
Loading
Loading