Skip to content

Instantly share code, notes, and snippets.

@danielo515
Last active June 15, 2017 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielo515/32e78441e900440f7350db81bca8fa0a to your computer and use it in GitHub Desktop.
Save danielo515/32e78441e900440f7350db81bca8fa0a to your computer and use it in GitHub Desktop.
'use strict';
const Urls = ['www.google.es','www.google.com','www.google.it','www.google.de','www.google.no'];
const extension = (joi) => ({
base: joi.string(),
name: 'url',
language: {
metrics: 'needs to be one of {{urls}}'
},
pre(value, state, options) {
return value.trim(); // remove whitespaces
},
describe(description) {
if ( this._flags.metrics ) {
description.valids = Urls;
}
return description;
},
rules: [
{
name: 'metrics',
setup( params ) {
this._flags.metrics = true;
},
validate(params, value, state, options) {
if (Urls.indexOf(value) === -1) {
// Generate an error, state and options need to be passed
return this.createError('url.metrics', { urls: Urls }, state, options);
}
return value; // Everything is OK
}
}
]
});
const Joi = require('joi');
const Felicity = require('felicity');
const joi = Joi.extend(extension);
Felicity.example(joi.object().keys({url: joi.url()}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment