Skip to content

Commit

Permalink
Merge pull request #56 from snyk/fix/properties-naming
Browse files Browse the repository at this point in the history
fix: schema properties should use snake case names
  • Loading branch information
cmars authored Nov 9, 2021
2 parents d3c1096 + af035f3 commit a7b1169
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/standards.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Entities will be commonly represented as types or classes when generating code.

### Schema properties

Schema properties use **camel case** names.
Schema properties use **snake case** names.

### Operation IDs

Expand Down
10 changes: 10 additions & 0 deletions naming.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ rules:
functionOptions:
type: snake

property-names-snake-case:
description: Property names must be snake_case.
message: '{{description}}'
severity: error
given: $..properties[*]~
then:
function: casing
functionOptions:
type: snake

component-names-pascal-case:
description: Component names must be PascalCase (except responses).
message: '{{description}}'
Expand Down
158 changes: 158 additions & 0 deletions tests/fixtures/propCasing.fail.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions tests/fixtures/valid.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions tests/propCasing.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const { Spectral } = require('@stoplight/spectral-core');
const { loadRules, loadSpec } = require('./utils');

let rules;

beforeAll(async () => {
rules = await loadRules('naming.yaml');
});

it('fails on snake case violation', async () => {
const spectral = new Spectral();
spectral.setRuleset(rules);
const result = await spectral.run(loadSpec('fixtures/propCasing.fail.yaml'));
expect(result).toHaveLength(8);
expect(result).toEqual(
expect.arrayContaining([
expect.objectContaining({
code: 'property-names-snake-case',
path: [
'components',
'schemas',
'HelloWorld',
'properties',
'attributes',
'properties',
'deprecatedField',
],
}),
expect.objectContaining({
code: 'property-names-snake-case',
path: [
'components',
'schemas',
'HelloWorld',
'properties',
'attributes',
'properties',
'requestSubject',
],
}),
expect.objectContaining({
code: 'property-names-snake-case',
path: [
'components',
'schemas',
'HelloWorld',
'properties',
'attributes',
'properties',
'requestSubject',
'properties',
'clientId',
],
}),
expect.objectContaining({
code: 'property-names-snake-case',
path: [
'components',
'schemas',
'HelloWorld',
'properties',
'attributes',
'properties',
'requestSubject',
'properties',
'publicId',
],
}),
]),
);
});

0 comments on commit a7b1169

Please sign in to comment.