directory-schema-validator

directory-schema-validator

npm Build Release Docs

Description

Validate directory structure and file contents with an extension of JSON schema.

Install

Install using NPM or similar.

npm i directory-schema-validator

Usage

import {Validator, shorthandToJSONSchema} from 'directory-schema-validator';

// Helper function to generate JSON Schema from shorthand notation
const schema = shorthandToJSONSchema(['README.md']);

// the schema is the following object
{
"type": "object",
"properties": {
"files": {
"properties": {
"README.md": {
"type": "object"
}
},
"type": "object",
"required": [
"README.md"
]
}
}
}

// instantiate validator and validate schema against a path
const validator = new Validator();
const valid = validator.validate(schema, '.');

if (!valid) {
console.log(validator.errors);
}

This works by operating on a JSON object parsed from the file structure.

import { parse } from 'directory-schema-validator';

parse('.');

{
"path": "/workspaces/directory-schema-validator",
"name": "directory-schema-validator",
"size": 873,
"type": "directory",
"directories": {},
"files": {
"README.md": {
"path": "/workspaces/directory-schema-validator/README.md",
"name": "README.md",
"size": 873,
"type": "file",
"extension": ".md"
}
}
}

See the reference documentation for more information about the structure of the JSON and signatures of each method.

Note: Because this is JSONSchema, composition is allowed through keywords such as allOf or oneOf.

Custom keywords

This library makes the keyword contents available. This keyword validates the contents of a file against an array of regex patterns.

{
"type": "object",
"properties": {
"files": {
"properties": {
"README.md": {
// START CUSTOM KEYWORD
"contents": [
{
"pattern": "directory-schema-validator",
"flags": "i"
}
],
// END CUSTOM KEYWORD
"type": "object"
}
},
"type": "object",
"required": [
"README.md"
]
}
}
}

Command line

Command line usage is available with the following commands: validate, shorthand, and parse. These commands correspond to the underlying interface.

npm install -g directory-schema-validator
directory-schema-validator --help

Note: The shorthand command uses glob and looks for actual files unlike the programmatic interface which only uses the strings.

Generated using TypeDoc