conventional-changelog / conventional-changelog/commitlint

RFC: config validation

Open
#2,408 7 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
18.7k
Forks
970
Avg merge
7h 33m
Merged PRs (30d)
49

Description

## Context

While working on #2398 I noticed that we are not validating if config provided has correct structure at all, in this pr i added basic validation for resolved configuration as doing unsafe type casting is really bad idea

Proposed implementation in not ideal, and requires more refining.

## Proposal

Create new package responsible for validation of configuration and rules `@commitlint/config-validator` and use [ajv](https://ajv.js.org) to validate configurations loaded by `@commitlint/load` and `@commitlint/resolve-extends`

Validation should be done in 2 steps: **config** and **rules**

### Config validation #2412
this should be first step of validation of config structure and type check properties, but it should not validate structure of specific rules as by default we should accept any configuration that matches

```js
'string': [RuleConfigSeverity /* 0, 1, 2 */, RuleConfigCondition /* when */, unknown /* options */]
```

### Rules validation
after resolving and merging all extended configs we should proceed to validate `options` for rules:
this is going to require change in structure of rule definition, we should add support for object and deprecate raw functions

in case if we don't have schema for rule, we should skip validation of `options`

#### Rule as object
schema property is optional, but recommended,
- if provided we should validate `options` against it
- if not provided we should threat `options` as any/unknown
```js
{
schema: { ... },
handler (parsed, when = 'always', options) { } // I'm not sure about this name, but this can be refined/changed
}
```

#### Rule as function - backward compatibility
It should be threated as there is no schema provided and we should accept any `options`
```js
function rule (parsed, when = 'always', options) {}
```

## Why we should change default structure of rules
with moving away of simple function structure it gives us possibility:
- to extend rules to provide more functionality in feature like requested fixing behavior. (#584)
- provide meaningful configuration errors (#2384)
- add ability to validate configurations provided by this project: config-angular-type-enum, config-angular, config-conventional, config-lerna-scopes, config-patternplate (#889)
- ~ability to expose json-schema for IDE to provide syntax autocompletion~
- This feature was requested before: #263, #835

---------------------

this is my general idea for this, its not written in best way, but I'd like to hear your opinion on this

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.