metalsmith-validate

0.1.5 • Public • Published

metalsmith-validate

A Metalsmith plugin that allows you to validate file metadata. You can check existence, type, and regex pattern. Neat.

Features

  • works on files matching pattern
  • can specify default for when key unspecified
  • can test for existence or for lack thereof
  • can validate against type or array of types using type-check
  • can match against pattern as RegExp, string, or callback(value) returning boolean

Installation

$ npm install metalsmith-validate

Usage

Validate all files

Pass hash of file metadata keys and rules.

var validate = require('metalsmith-validate');
 
metalsmith.use(validate({
  title: {
   exists: true,
   type: 'String'
  },
  date: {
   exists: true,
   pattern: /^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/
  },
  tag: {
   default: 'news'
  },
  draft: {
   exists: false
  }
}));

Validate pattern-matching files

Pass array of hashes with file-matching pattern and metadata validation hash:

var validate = require('metalsmith-validate');
 
metalsmith.use(validate([
    {
        pattern: 'post/*',
        metadata: {
            title: true, // shorthand for title: { exists: true }
            date: true,
            invalid: false
        }
    },
    {
        // pattern defaults to '**/*'
        metadata: {
            template: {
                default: function (file, data) {
                    return file + '.jade';
                },
                pattern: function (value) {
                    return value.match(/\.jade$/);
                }
            }
        }
    }
));

Notice, both default and pattern accept callbacks. Also note the file-matching pattern is specified as a path after the metalsmith .source path (e.g. 'post/' and not 'src/post/').

CLI Usage

All of the same options apply, just add them to the "plugins" key in your metalsmith.json configuration:

{
  "plugins": {
    "metalsmith-validate": [
      {
        "pattern": "page/*",
        "metadata": {
         "title": {
         "exists": true,
         "type": "String"
         }
        }
      }
    ]
  }
}

License

MIT

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.1.5
    1
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 0.1.5
    1
  • 0.1.4
    0
  • 0.1.3
    0
  • 0.1.2
    0
  • 0.1.0
    0

Package Sidebar

Install

npm i metalsmith-validate

Weekly Downloads

1

Version

0.1.5

License

MIT

Unpacked Size

11.6 kB

Total Files

46

Last publish

Collaborators

  • mikestopcontinues