Skip to content

Commit 83c0656

Browse files
committed
[added] meta() and describe()
1 parent 9095d4c commit 83c0656

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,16 @@ Creates a deep copy of the schema. Clone is used internally to return a new sche
228228

229229
Overrides the key name which is used in error messages.
230230

231+
#### `mixed.meta(Object metadata)`
232+
233+
Adds to a metadata object, useful for storing data with a schema, that doesn't belong
234+
the cast object itself.
235+
236+
#### `mixed.describe() => Object description`
237+
238+
Collects schema details (like meta, labels, and active tests) into a serializable
239+
description object.
240+
231241
#### `mixed.concat(Schema schema)`
232242

233243
Creates a new instance of the schema by combining two schemas. Only schemas of the same type can be concatenated.

src/mixed.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var Promise = require('promise/lib/es6-extensions')
1212

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

15+
1516
function runValidations(validations, endEarly, value, path) {
1617
return endEarly
1718
? Promise.all(validations)
@@ -56,12 +57,21 @@ SchemaType.prototype = {
5657
return cloneDeep(this);
5758
},
5859

59-
label: function(label){
60+
label(label) {
6061
var next = this.clone();
6162
next._label = label;
6263
return next;
6364
},
6465

66+
meta(obj) {
67+
if (arguments.length === 0)
68+
return this._meta;
69+
70+
var next = this.clone();
71+
next._meta = Object.assign(next._meta || {}, { ...obj })
72+
return next;
73+
},
74+
6575
withMutation(fn) {
6676
this._mutate = true
6777
let result = fn(this)
@@ -149,8 +159,10 @@ SchemaType.prototype = {
149159

150160
if (schema._typeError)
151161
initialTests.push(schema._typeError(validationParams));
162+
152163
if (schema._whitelistError)
153164
initialTests.push(schema._whitelistError(validationParams));
165+
154166
if (schema._blacklistError)
155167
initialTests.push(schema._blacklistError(validationParams));
156168

@@ -186,6 +198,10 @@ SchemaType.prototype = {
186198
}), cb)
187199
},
188200

201+
getDefault({ context, parent }) {
202+
return this._resolve(context, parent).default()
203+
},
204+
189205
default(def) {
190206
if (arguments.length === 0) {
191207
var dflt = _.has(this, '_default') ? this._default : this._defaultDefault
@@ -369,6 +385,17 @@ SchemaType.prototype = {
369385
_option(key, overrides){
370386
return _.has(overrides, key)
371387
? overrides[key] : this._options[key]
388+
},
389+
390+
describe() {
391+
let next = this.clone();
392+
393+
return {
394+
type: next._type,
395+
meta: next._meta,
396+
label: next._label,
397+
tests: next.tests.map((fn) => fn.TEST_NAME, {})
398+
}
372399
}
373400
}
374401

test/mixed.js

+24
Original file line numberDiff line numberDiff line change
@@ -484,4 +484,28 @@ describe( 'Mixed Types ', function(){
484484
])
485485
})
486486

487+
it('should add meta() data', () => {
488+
string()
489+
.meta({ input: 'foo' })
490+
.meta({ foo: 'bar' })
491+
.meta().should.eql({
492+
input: 'foo',
493+
foo: 'bar'
494+
})
495+
})
496+
497+
it('should describe', () => {
498+
string().max(2)
499+
.meta({ input: 'foo' })
500+
.label('str!')
501+
.describe().should.eql({
502+
type: 'string',
503+
label: 'str!',
504+
tests: ['max'],
505+
meta: {
506+
input: 'foo'
507+
}
508+
})
509+
})
510+
487511
})

0 commit comments

Comments
 (0)