Enforce Rules
Use rules to create consistency in your service design.
Rules​
You can use rules to enforce naming conventions, check for required fields, and more. Basketry comes with a set of built-in rules, or you can create your own custom ones.
Let's start by installing the @basketry/rules
package:
- npm
- yarn
- pnpm
npm install @basketry/rules
yarn add @basketry/rules
pnpm add @basketry/rules
Update the configuration​
Rules can be specified as strings or objects. Use the object notation when you need to pass options.
Update the rules
array in your basketry.config.json
file to include a few of the standard rules provided by the @basketry/rules
package:
{
"source": "petstore.json",
"parser": "@basketry/openapi-3",
"rules": [
"@basketry/rules/lib/http-get-status",
{
"rule": "@basketry/rules/lib/casing",
"options": {
"property": "camel"
}
}
],
"generators": ["@basketry/typescript"],
"output": "src",
"options": {
"basketry": {}
}
}
- npm
- yarn
- pnpm
npm run basketry
yarn basketry
pnpm run basketry
The console output will show if any rules failed while parsing petstore.json
. You can use this information to update your service definition to meet the rules you've set for your API. In the future, if you add or change the rules that govern your service definition, you can easily run the basketry
script again to check for compliance.
Learn more about the rules that Basketry provides by checking out the rule components.
You can use the Basketry validate
command to only evaluate the configured rules without generating any code. This is useful for Continuous Integration (CI) pipelines to ensure that your service definition is compliant prior to merging changes.
- npm
- yarn
- pnpm
npx basketry validate
yarn dlx basketry validate
pnpm dlx basketry validate