Skip to content

Instantly share code, notes, and snippets.

@tbarn
Created March 27, 2019 22:14
Show Gist options
  • Save tbarn/dceba0274e7eee60213c0d9dc14730db to your computer and use it in GitHub Desktop.
Save tbarn/dceba0274e7eee60213c0d9dc14730db to your computer and use it in GitHub Desktop.
const { RuleFunction, Spectral } = require('@stoplight/spectral');
const spectral = new Spectral();
spectral.addRules({
snake_case: {
summary: ‘Checks for snake case pattern’,
// evaluate every property
given: '$..*',
then: {
function: RuleFunction.PATTERN,
functionOptions: {
match: '^[a-z]+[a-z0-9_]*[a-z0-9]+$',
},
},
},
});
// run!
spectral.run({name: ‘helloWorld’,}).then(results => {
console.log(JSON.stringify(results, null, 4));
});
// => outputs a single result since helloWorld is not snake_case
// [
// {
// “name”: “snake_case”,
// “message”: “must match the pattern ‘^[a-z]+[a-z0-9_]*[a-z0-9]+$’”,
// “severity”: 1,
// “path”: [
// “name”
// ]
// }
// ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment