Skip to content

Commit

Permalink
allow default value to take callback function(file, data) returning d…
Browse files Browse the repository at this point in the history
…efault value
  • Loading branch information
mikestopcontinues committed Aug 30, 2014
1 parent 0c2b094 commit adacf42
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
/*.log
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.1.2 - August 30, 2014
-----------------------
* allow default value to take callback function(file, data) returning default value

0.1.1 - August 30, 2014
-----------------------
* only test type against meta keys that exist
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ metalsmith.use(validate([
// pattern defaults to '**/*'
metadata: {
template: {
default: 'default.jade'
default: function (file, data) {
return file + '.jade';
},
pattern: function (value) {
return value.match(/\.jade$/);
}
Expand All @@ -71,6 +73,8 @@ metalsmith.use(validate([
));
```

Notice, both `default` and `pattern` accept callbacks.

## CLI Usage

All of the same options apply, just add them to the `"plugins"` key in your `metalsmith.json` configuration:
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function plugin(matchers) {
var validator = rule.metadata[key];

if ('default' in validator && !(key in data)) {
return data[key] = validator.default;
return data[key] = typeof validator.default === 'function' ? validator.default(data[key]) : validator.default;
}

if ('exists' in validator && validator.exists != key in data) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"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.1",
"version": "0.1.2",
"author": "Mike Stop Continues <mikestopcontinues@gmail.com>",
"license": "MIT",
"main": "lib/index.js",
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/default-cb/build/one.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

one
2 changes: 2 additions & 0 deletions test/fixtures/default-cb/build/two.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

one
5 changes: 5 additions & 0 deletions test/fixtures/default-cb/src/one.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
default: specified
---

one
4 changes: 4 additions & 0 deletions test/fixtures/default-cb/src/two.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

one
24 changes: 24 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ describe('metalsmith-validate', function () {
});
});

it('should apply value returned by default callback if key unspecified', function (done) {
Metalsmith('test/fixtures/default-cb')
.use(validate([
{
metadata: {
default: {
default: function (file, data) {
return 'unspecified';
}
}
}
}
]))
.build(function (err, files) {
if (err) {
return done(err);
}

assert.equal(files['one.md'].default, 'specified');
assert.equal(files['two.md'].default, 'unspecified');
done();
});
});

it('should fail if exists = true and key unspecified', function (done) {
Metalsmith('test/fixtures/exists')
.use(validate([
Expand Down

0 comments on commit adacf42

Please sign in to comment.