Examples
Basic usage
The file .gitignore must have a line with the content /dist/
.
style = "style.json"
/dist/
{
"rules": [
{
"files": [".gitignore"],
"includeLines": ["/dist/"]
}
]
}
Project config self configuration
project-config is defining a valid configuration,
forcing the definition of styles
as an array for styles and
a valid cache
value.
style = ["style.json5"]
cache = "5 minutes"
{
rules: [
{
files: [".project-config.toml"],
JMESPathsMatch: [
// `style` must be defined in the file
["contains(keys(@), 'style')", true],
// `style` must be an array
["type(style)", "array"],
// at least one style configured
["op(length(style), '>', `0`)", true],
// configure cache explicitly
["contains(keys(@), 'cache')", true],
// cache must have a valid value
[
"regex_match('^(\\d+ ((seconds?)|(minutes?)|(hours?)|(days?)|(weeks?)))|(never)$', cache)",
true,
],
],
},
],
}
Files absence
The files readme.md and index.md must not exist.
style = "style.yaml"
rules:
- files:
not:
readme.md: Users are more used to seeing README.md file name in uppercase
hint: Rename 'readme.md' to 'README.md'
- files:
not:
- index.md
Conditionals
If .gitignore includes the line __pycache__/
a pyproject.toml
file must be present.
style = "style.json"
__pycache__/
{
"rules": [
{
"ifIncludeLines": {
".gitignore": ["__pycache__/"]
},
"files": ["pyproject.toml"]
}
]
}
Conditionals files existence
1st rule: if the directory src/ exists, the file pyproject.toml must exists too.
2nd rule: if the file pyproject.toml exists, a Python file must be present in the root directory.
style = "style.json5"
{
rules: [
{
files: ["pyproject.toml"],
ifFilesExist: ["src/"],
},
{
files: ["*.py"],
ifFilesExist: ["pyproject.toml"],
},
],
}
Compare values between serializable files
The version defined in __version__
inside a Python script must match
the metadata defined in pyproject.toml file.
style = "style.toml"
[tool.poetry]
version = "1.0.0"
[[rules]]
files = ["pyproject.toml"]
crossJMESPathsMatch = [
[
"tool.poetry.version",
["script.py", "__version__"],
"op([0], '==', [1])",
true,
],
]
"""Simple script."""
__version__ = "1.0.0"
JMESPath against online sources
Check that the license
field of package.json file is defined with
a valid OSI approved SPDX license identifier.
style = "style.json5"
{
rules: [
{
files: ["package.json"],
crossJMESPathsMatch: [
[
"license",
[
"gh://spdx/license-list-data@v3.17/json/licenses.json",
"licenses[?isOsiApproved] | [?isDeprecatedLicenseId == `false`].licenseId",
],
"contains([1], [0])",
true,
],
],
},
],
}
{
"license": "BSD-3-Clause"
}
Tip
For more complex examples check my own styles at mondeja/project-config-styles.