Skip to content

Commit

Permalink
[added] meta() and describe()
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Apr 7, 2016
1 parent 9095d4c commit 83c0656
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,16 @@ Creates a deep copy of the schema. Clone is used internally to return a new sche

Overrides the key name which is used in error messages.

#### `mixed.meta(Object metadata)`

Adds to a metadata object, useful for storing data with a schema, that doesn't belong
the cast object itself.

#### `mixed.describe() => Object description`

Collects schema details (like meta, labels, and active tests) into a serializable
description object.

#### `mixed.concat(Schema schema)`

Creates a new instance of the schema by combining two schemas. Only schemas of the same type can be concatenated.
Expand Down
29 changes: 28 additions & 1 deletion src/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var Promise = require('promise/lib/es6-extensions')

let notEmpty = value => !isAbsent(value);


function runValidations(validations, endEarly, value, path) {
return endEarly
? Promise.all(validations)
Expand Down Expand Up @@ -56,12 +57,21 @@ SchemaType.prototype = {
return cloneDeep(this);
},

label: function(label){
label(label) {
var next = this.clone();
next._label = label;
return next;
},

meta(obj) {
if (arguments.length === 0)
return this._meta;

var next = this.clone();
next._meta = Object.assign(next._meta || {}, { ...obj })
return next;
},

withMutation(fn) {
this._mutate = true
let result = fn(this)
Expand Down Expand Up @@ -149,8 +159,10 @@ SchemaType.prototype = {

if (schema._typeError)
initialTests.push(schema._typeError(validationParams));

if (schema._whitelistError)
initialTests.push(schema._whitelistError(validationParams));

if (schema._blacklistError)
initialTests.push(schema._blacklistError(validationParams));

Expand Down Expand Up @@ -186,6 +198,10 @@ SchemaType.prototype = {
}), cb)
},

getDefault({ context, parent }) {
return this._resolve(context, parent).default()
},

default(def) {
if (arguments.length === 0) {
var dflt = _.has(this, '_default') ? this._default : this._defaultDefault
Expand Down Expand Up @@ -369,6 +385,17 @@ SchemaType.prototype = {
_option(key, overrides){
return _.has(overrides, key)
? overrides[key] : this._options[key]
},

describe() {
let next = this.clone();

return {
type: next._type,
meta: next._meta,
label: next._label,
tests: next.tests.map((fn) => fn.TEST_NAME, {})
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions test/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,4 +484,28 @@ describe( 'Mixed Types ', function(){
])
})

it('should add meta() data', () => {
string()
.meta({ input: 'foo' })
.meta({ foo: 'bar' })
.meta().should.eql({
input: 'foo',
foo: 'bar'
})
})

it('should describe', () => {
string().max(2)
.meta({ input: 'foo' })
.label('str!')
.describe().should.eql({
type: 'string',
label: 'str!',
tests: ['max'],
meta: {
input: 'foo'
}
})
})

})

0 comments on commit 83c0656

Please sign in to comment.