Skip to content

Thoughts on something like any.meta(obj)? #241

@joshuafcole

Description

@joshuafcole

This is conceptually similar to any.tags(), any.notes(), and any.description(), but allows the storage of an object rather than a string. This is valuable because, besides annotating the schemas, it allows us to build modules on Joi that provide additional functionality. Example use case:

var schema = {
  _id: Joi.number().integer().meta({index: {unique: true, sparse: true}}),
  name: 'Margaret'
};

// Registers schema as the validator for person models, and
// extracts indexing information for each field from the metadata on each field.
myDBAdapter.model('Person', schema);

This can currently be accomplished using tags(), but it requires string parsing and complicates the logic of actually retrieving the metadata. E.g.

var schema = {
  _id: Joi.number().integer().tags(['index:unique', 'index:sparse']),
  name: 'Margaret'
};

The benefit of an any.meta() is similar to that of the existing description functions -- the metadata is stored consistently and doesn't need to worry about clobbering / being clobbered by Joi's moving parts.

Cheers,
Josh

Metadata

Metadata

Assignees

Labels

featureNew functionality or improvement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions