-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* can specify default for when key unspecified * can test for existence or for lack thereof * can validate against type or array of types using [check-types](https://github.com/philbooth/check-types.js) * can match against pattern as RegExp, string, or callback(value) returning boolean
- Loading branch information
0 parents
commit 60f6aef
Showing
43 changed files
with
539 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
0.1.0 - August 30, 2014 | ||
----------------------- | ||
* 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 [check-types](https://github.com/philbooth/check-types.js) | ||
* can match against pattern as RegExp, string, or callback(value) returning boolean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
node_modules: package.json | ||
@npm install | ||
|
||
test: node_modules | ||
@./node_modules/.bin/mocha --reporter spec | ||
|
||
.PHONY: test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# metalsmith-validate | ||
|
||
A [Metalsmith](https://github.com/segmentio/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 [check-types](https://github.com/philbooth/check-types.js) | ||
- 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. | ||
|
||
```js | ||
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: | ||
|
||
```js | ||
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: 'default.jade' | ||
pattern: function (value) { | ||
return value.match(/\.jade$/); | ||
} | ||
} | ||
} | ||
} | ||
)); | ||
``` | ||
|
||
## CLI Usage | ||
|
||
All of the same options apply, just add them to the `"plugins"` key in your `metalsmith.json` configuration: | ||
|
||
```json | ||
{ | ||
"plugins": { | ||
"metalsmith-validate": [ | ||
{ | ||
"pattern": "page/*", | ||
"metadata": { | ||
"title": { | ||
"exists": true, | ||
"type": "String" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
'use strict'; | ||
|
||
var debug = require('debug')('metalsmith-validate'); | ||
var Matcher = require('minimatch').Minimatch; | ||
var typeCheck = require('type-check').typeCheck; | ||
|
||
/** | ||
* Expose `plugin`. | ||
*/ | ||
|
||
module.exports = plugin; | ||
|
||
/** | ||
* Metalsmith plugin that allows you to easily validate file metadata, checking existence, type, and regex pattern. Can also supply default value. | ||
* | ||
* @param {Array|Object} matchers array of rules applied to pattern-matched files, or hash of metadata validators to apply to all files | ||
* @option {*} matchers[key].default set key to value if unset | ||
* @option {Boolean} matchers[key].exists whether the key should exist | ||
* @option {Array|string} matchers[key].type array of strings (or single string) of types which key must match at least one of | ||
* @option {RegExp|string|Function} matchers[key].pattern regex pattern as RegExp or string to compare against key, or callback(value) returning Boolean | ||
* @return {Function} | ||
*/ | ||
|
||
function plugin(matchers) { | ||
if (!Array.isArray(matchers)) { | ||
matchers = [ | ||
{ | ||
metadata: matchers, | ||
} | ||
]; | ||
} | ||
|
||
matchers = matchers.map(function (rule) { | ||
rule.pattern = rule.pattern || '**/*'; | ||
debug('Normalize rule with pattern %s', rule.pattern); | ||
rule.matcher = new Matcher(rule.pattern); | ||
rule.metadata = rule.metadata || {}; | ||
|
||
Object.keys(rule.metadata).forEach(function (key) { | ||
rule.metadata[key] = typeof rule.metadata[key] === 'object' ? rule.metadata[key] : { | ||
exists: rule.metadata[key], | ||
}; | ||
|
||
var validator = rule.metadata[key]; | ||
|
||
if (validator.type && !Array.isArray(validator.type)) { | ||
validator.type = [validator.type]; | ||
} | ||
|
||
if (typeof validator.pattern === 'string') { | ||
validator.pattern = new RegExp(validator.pattern); | ||
} | ||
}); | ||
|
||
return rule; | ||
}); | ||
|
||
return function (files, metalsmith, done) { | ||
var err; | ||
|
||
Object.keys(files).forEach(function (file) { | ||
debug('Validate file %s', file); | ||
var data = files[file]; | ||
|
||
matchers.forEach(function (rule) { | ||
if (rule.matcher.match(file)) { | ||
debug('Validate against rule with matching pattern %s', rule.pattern); | ||
Object.keys(rule.metadata).every(function (key) { | ||
var validator = rule.metadata[key]; | ||
|
||
if ('default' in validator && !(key in data)) { | ||
return data[key] = validator.default; | ||
} | ||
|
||
if ('exists' in validator && validator.exists != key in data) { | ||
err = new Error('File "' + file + '" ' + (validator.exists ? 'does not have' : 'has') + ' value "' + key + '".'); | ||
return false; | ||
} | ||
|
||
if ('type' in validator && validator.type.indexOf(typeof data[key]) === -1) { | ||
var valid = false; | ||
|
||
validator.type.some(function (type) { | ||
return valid = typeCheck(type, data[key]); | ||
}); | ||
|
||
if (!valid) { | ||
err = new Error('File "' + file + '" value "' + key + '" must be of type [' + validator.type.join(', ') + '].'); | ||
return false; | ||
} | ||
} | ||
|
||
if ('pattern' in validator && key in data && ((typeof validator.pattern === 'function' && !validator.pattern(data[key])) || !data[key].match(validator.pattern))) { | ||
err = new Error('File "' + file + '" value "' + key + '" does not match ' + validator.pattern + '.'); | ||
return false; | ||
} | ||
|
||
return true; | ||
}); | ||
} | ||
|
||
if (err) { | ||
return false; | ||
} | ||
}); | ||
}); | ||
|
||
done(err); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "metalsmith-validate", | ||
"description": "A Metalsmith plugin that allows you to easily validate file metadata, checking existence, type, and regex pattern.", | ||
"repository": "git://github.com/mikestopcontinues/metalsmith-validate.git", | ||
"version": "0.1.0", | ||
"author": "Mike Stop Continues <mikestopcontinues@gmail.com>", | ||
"license": "MIT", | ||
"main": "lib/index.js", | ||
"dependencies": { | ||
"debug": "~0.7.4", | ||
"minimatch": "^0.3.0", | ||
"type-check": "^0.3.1" | ||
}, | ||
"devDependencies": { | ||
"metalsmith": "0.x", | ||
"mocha": "1.x" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
pattern: match | ||
--- | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
pattern: unmatched | ||
--- | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
default: specified | ||
--- | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
--- | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
exists: true | ||
--- | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
--- | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
default: specified | ||
--- | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
--- | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
exists: true | ||
--- | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
--- | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
pattern: match | ||
--- | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
pattern: unmatched | ||
--- | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
pattern: match | ||
--- | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
pattern: unmatched | ||
--- | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
type: string | ||
--- | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
type: | ||
- string | ||
- array | ||
--- | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
one |
Oops, something went wrong.