CLI to validate that a directory matches a set of file and folder rules.
$ npm install directory-validator
Generate a .directoryvalidator.json config:
$ directory-validator --init
Validate a directory:
$ directory-validator .
The process exits with code 0 when the directory is valid and 1 when validation fails, the configuration is invalid, or the configuration file was not found.
| Option | Description |
|---|---|
<dirPath> |
Directory to validate (needed unless using --init) |
-i, --init |
Create a .directoryvalidator.json file in the current directory |
-p, --print |
Print the validated directory structure |
-f, --ignore-files <files> |
Ignore files by glob, eg: -f "*.js" |
-d, --ignore-dirs <dirs> |
Ignore directories by glob, eg: -d "**/tests" |
-g, --gitignore |
Respect .gitignore files |
-c, --config-file <path> |
Path to the configuration file |
-V, --version |
Print the version |
-h, --help |
Show help |
--init writes a .directoryvalidator.json like this:
{
"ignoreFiles": [".gitignore"],
"ignoreDirs": ["node_modules", ".git"],
"useGitIgnore": false,
"commonRules": {
"rule_indexfile": {
"type": "file",
"name": "index.js"
}
},
"rules": [
{
"type": "file",
"name": "package.json"
},
{
"type": "common",
"key": "rule_indexfile"
},
{
"type": "directory",
"name": "src",
"isOptional": true,
"rules": [
{
"type": "common",
"key": "rule_indexfile"
}
]
}
]
}If -c / --config-file is omitted, the tool looks for .directoryvalidator.json in the target directory, then in each parent directory up to the home directory.
rules is an array of file, directory, or common rules. Every file and directory must match at least one rule. If several rules match the same path, they all pass.
type(required):"file"name(required): matcher for the file name (see Name patterns)extension(optional): extension without the dot. If set,nameis matched against the name without the extension (logoforlogo.png). If omitted,nameis matched against the full filename.isOptional(optional, defaultfalse): allow the file to be absent
type(required):"directory"name(required): same matchers as file namesisOptional(optional, defaultfalse): allow the directory to be absentisRecursive(optional, defaultfalse): apply this directory'srulesto nested directories with the same namerules(optional): nested file and directory rules. If empty or omitted, any contents are allowed.
With isRecursive, both src/index.js and src/src/index.js pass:
{
"type": "directory",
"name": "src",
"isRecursive": true,
"rules": [{ "type": "file", "name": "index.js" }]
}Reusable file or directory rules, defined in commonRules. Keys must start with rule_.
type(required):"common"key(required): a key incommonRulesisOptional(optional, defaultfalse): allow the referenced rule to be absent
name (and extension) can be a string or a RegExp. In JSON, wrap a RegExp in /.../.
Special string tokens: [camelCase], [UPPERCASE], [dash-case], [snake_case], *.
extension examples: "js", "png", "/(png|jpg|gif)/".
For example, { "name": "index.js", "type": "file" } and { "name": "[camelCase].js", "type": "file" } both match index.js.
ignoreFiles and ignoreDirs take glob strings:
{
"ignoreFiles": [".gitignore", "**/*.test.js", ".*"],
"ignoreDirs": ["node_modules", ".git", "src/**/tests"]
}The same globs can be passed on the CLI with -f / -d.
When useGitIgnore is true (or -g is passed), .gitignore rules at or below the target directory are applied: matching files are skipped and matching directories are not traversed. Disabled by default.
Requires Node.js 24+. npm test, npm run lint, npm run build. Publishing is automated on push to main. To release: npm version patch|minor|major, then git push --follow-tags.