Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rules section to documentation #27

Merged
merged 10 commits into from
May 27, 2024
50 changes: 50 additions & 0 deletions docs/create_rules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Create rules

In order to lint and score models, `dbt-score` uses a set of rules that is
jochemvandooren marked this conversation as resolved.
Show resolved Hide resolved
applied to each model. A rule can pass or fail when it is applied to a model.
jochemvandooren marked this conversation as resolved.
Show resolved Hide resolved
Based on the severity of the rule models are scored. `dbt-score` has a set of
rules enabled by default, which can be found [here](reference/rules/generic.md).
matthieucan marked this conversation as resolved.
Show resolved Hide resolved

On top of the generic rules, it's possible to add your own rules. By default
`dbt-score` will look for rules in the namespace `dbt_score_rules`.
jochemvandooren marked this conversation as resolved.
Show resolved Hide resolved

Two ways exist to create a new rule:

1. By using the `@rule` decorator, which is the preferred/simple way.
2. By inheriting from the `Rule` class, which is more advanced.
jochemvandooren marked this conversation as resolved.
Show resolved Hide resolved

### Using the `@rule` decorator

The `@rule` decorator can be used to easily create a new rule:

```python
matthieucan marked this conversation as resolved.
Show resolved Hide resolved
@rule
def has_description(model: Model) -> RuleViolation | None:
"""A model should have a description."""
if not model.description:
return RuleViolation(message="Model lacks a description.")
jochemvandooren marked this conversation as resolved.
Show resolved Hide resolved
```

The severity of a rule can be set using the `severity` argument:

```python
@rule(severity=Severity.HIGH)
```

jochemvandooren marked this conversation as resolved.
Show resolved Hide resolved
### Using the `Rule` class

For more advanced use cases, a rule can be created by inheriting from the `Rule`
class:

```python
class HasDescription(Rule):
description = "A model should have a description."

def evaluate(self, model: Model) -> RuleViolation | None:
"""Evaluate the rule."""
if not model.description:
return RuleViolation(message="Model lacks a description.")
```

Using the rule class can be useful if the rule needs to keep state between
evaluations.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins:
- mkdocstrings
nav:
- Home: index.md
- Create rules: create_rules.md
- Reference:
- reference/cli.md
- reference/config.md
Expand Down