Last active
June 15, 2017 07:53
-
-
Save danielo515/32e78441e900440f7350db81bca8fa0a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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