🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

directory-schema-validator

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

directory-schema-validator

Validate file system structure with an extension of JSON schema.

latest
Source
npmnpm
Version
1.0.17
Version published
Weekly downloads
266
-26.72%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

validation

FAQs

Package last updated on 22 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts