Properties
A PropertyRule
allows you to write rules about the properties of your request and response schemas.
new PropertyRule({
name: "must be camel case",
rule: (propertyAssertions) => {
propertyAssertions.addedOrChanged((property) => {
if (!isCamelCase(property.value.key)) {
throw new RuleError({
message: `property names must be camelCase. ${property.value.key} is not`,
});
}
});
},
});
new PropertyRule(options)
The following table describes the options object.
property | description | required | type |
---|---|---|---|
name | the name of the rule | yes | string |
matches | A function used to determine when this Rule should be applied. Return true to indicate this Rule should run. | no | (property: Property, ruleContext: RuleContext) => boolean |
docsLink | A link to the documentation for this ruleset. This will be used to show the user on a rule error. If there is a more specific docsLink (e.g. on a nested Rule), the more specific docsLink will be shown) | no | string |
rule | A function to define assertions for a property. | yes | (propertyAssertions: PropertyAssertions, ruleContext: RuleContext) => void |
matches
matches
is invoked with a Property
and RuleContext
.
Example:
new PropertyRule({
...,
// only runs on properties that have type 'number'
matches: (property, ruleContext) => property.value.flatSchema.type === 'number',
...
});
propertyAssertions
propertyAssertions is used to define property rules.
propertyAssertions[lifecycle](assertion)
new PropertyRule({
...,
rule: (propertyAssertions) => {
// lifecycle rules that are available are added, changed, addedOrChanged, requirement and removed
propertyAssertions.added('contains a description', (property) => {
if (!property.value.description) {
throw new RuleError({
message: 'properties must contain a description',
});
}
});
},
});