Skip to content

Commit d1b9909

Browse files
author
Christian Compton
authored
feat: exclude common inconsistent property names (#230)
1 parent e47ea91 commit d1b9909

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ The default values for each rule are described below.
483483
| no_property_description | warning |
484484
| description_mentions_json | warning |
485485
| array_of_arrays | warning |
486-
| inconsistent_property_type | warning |
486+
| inconsistent_property_type | warning, [code, default, type, value]]<br>(list of property names to exclude)|
487487
| property_case_convention | error, lower_snake_case |
488488
| property_case_collision | error |
489489
| enum_case_convention | warning, lower_snake_case |

src/.defaultsForValidator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const defaults = {
6565
'no_property_description': 'warning',
6666
'description_mentions_json': 'warning',
6767
'array_of_arrays': 'warning',
68-
'inconsistent_property_type': 'warning',
68+
'inconsistent_property_type': ['warning', ['code', 'default', 'type', 'value']],
6969
'property_case_convention': [ 'error', 'lower_snake_case'],
7070
'property_case_collision': 'error',
7171
'enum_case_convention': [ 'warning', 'lower_snake_case'],

src/plugins/validation/2and3/semantic-validators/schema-ibm.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -617,23 +617,26 @@ function checkProperties(
617617
messages.addMessage(
618618
propertiesToCompare[key].path,
619619
`Property has inconsistent type: ${key}.`,
620-
configOption,
620+
configOption[0],
621621
'inconsistent_property_type'
622622
);
623623
}
624624
messages.addMessage(
625625
contextPath.concat(['properties', key]).join('.'),
626626
`Property has inconsistent type: ${key}.`,
627-
configOption,
627+
configOption[0],
628628
'inconsistent_property_type'
629629
);
630630
}
631631
} else {
632-
propertiesToCompare[key] = {
633-
type: value.type,
634-
path: contextPath.concat(['properties', key]).join('.'),
635-
printed: false
636-
};
632+
if (configOption && configOption[1] && !configOption[1].includes(key)) {
633+
// add property if the name is not excluded
634+
propertiesToCompare[key] = {
635+
type: value.type,
636+
path: contextPath.concat(['properties', key]).join('.'),
637+
printed: false
638+
};
639+
}
637640
}
638641
}
639642
}

test/plugins/validation/2and3/schema-ibm.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,4 +1759,45 @@ describe('validation plugin - semantic - schema-ibm - OpenAPI 3', () => {
17591759
'components.schemas.kid.properties.name'
17601760
);
17611761
});
1762+
1763+
it('should not produce a warning for properties with duplicate common names', () => {
1764+
const spec = {
1765+
components: {
1766+
schemas: {
1767+
person: {
1768+
description: 'Produce warnings',
1769+
properties: {
1770+
name: {
1771+
description: 'type integer',
1772+
type: 'integer'
1773+
}
1774+
}
1775+
},
1776+
adult: {
1777+
description: 'Causes first warnings',
1778+
properties: {
1779+
code: {
1780+
description: 'different type',
1781+
type: 'number'
1782+
}
1783+
}
1784+
},
1785+
kid: {
1786+
description: 'Causes second warning',
1787+
properties: {
1788+
code: {
1789+
type: 'string',
1790+
description: 'differnt type'
1791+
}
1792+
}
1793+
}
1794+
}
1795+
}
1796+
};
1797+
1798+
const res = validate({ jsSpec: spec }, config);
1799+
1800+
expect(res.warnings.length).toEqual(0);
1801+
expect(res.errors.length).toEqual(0);
1802+
});
17621803
});

0 commit comments

Comments
 (0)