UNPKG

decentralized-internet

Version:

An NPM library of programs to create decentralized web and distributed computing projects

1 lines 4.81 kB
"use strict";const Hoek=require("hoek"),Any=require("./any"),Cast=require("./cast"),Errors=require("./errors"),Lazy=require("./lazy"),Ref=require("./ref"),internals={alternatives:require("./alternatives"),array:require("./array"),boolean:require("./boolean"),binary:require("./binary"),date:require("./date"),number:require("./number"),object:require("./object"),string:require("./string"),root:function(){const e=new Any,t=e.clone();return t.any=function(){return e},t.alternatives=t.alt=function(){return arguments.length?internals.alternatives.try.apply(internals.alternatives,arguments):internals.alternatives},t.array=function(){return internals.array},t.boolean=t.bool=function(){return internals.boolean},t.binary=function(){return internals.binary},t.date=function(){return internals.date},t.func=function(){return internals.object._func()},t.number=function(){return internals.number},t.object=function(){return arguments.length?internals.object.keys.apply(internals.object,arguments):internals.object},t.string=function(){return internals.string},t.ref=function(){return Ref.create.apply(null,arguments)},t.isRef=function(e){return Ref.isRef(e)},t.validate=function(n){const r=arguments[arguments.length-1],a="function"==typeof r?r:null,s=arguments.length-(a?1:0);if(1===s)return e.validate(n,a);const i=3===s?arguments[2]:{};return t.compile(arguments[1])._validateWithOptions(n,i,a)},t.describe=function(){return(arguments.length?t.compile(arguments[0]):e).describe()},t.compile=function(e){try{return Cast.schema(e)}catch(e){throw e.hasOwnProperty("path")&&(e.message=e.message+"("+e.path+")"),e}},t.assert=function(e,n,r){t.attempt(e,n,r)},t.attempt=function(e,n,r){const a=t.validate(e,n),s=a.error;if(s){if(!r)throw s.message=s.annotate(),s;if(!(r instanceof Error))throw s.message=r+" "+s.annotate(),s;throw r}return a.value},t.reach=function(e,t){if(Hoek.assert(e&&e instanceof Any,"you must provide a joi schema"),Hoek.assert("string"==typeof t,"path must be a string"),""===t)return e;const n=t.split("."),r=e._inner.children;if(!r)return;const a=n[0];for(let e=0;e<r.length;++e){const n=r[e];if(n.key===a)return this.reach(n.schema,t.substr(a.length+1))}},t.lazy=function(e){return Lazy.set(e)},t.extend=function(){const e=Hoek.flatten(Array.prototype.slice.call(arguments));Hoek.assert(e.length>0,"You need to provide at least one extension"),this.assert(e,t.extensionsSchema);const n=Object.create(this);for(let t=0;t<e.length;++t){const r=e[t],a=(r.base||this.any()).clone(),s=a.constructor,i=class extends s{constructor(){super(),r.base&&Object.assign(this,a),this._type=r.name,r.language&&(this._settings=this._settings||{language:{}},this._settings.language=Hoek.applyToDefaults(this._settings.language,{[r.name]:r.language}))}};if(r.coerce&&(i.prototype._coerce=function(e,t,n){if(s.prototype._coerce){const r=s.prototype._coerce.call(this,e,t,n);if(r.errors)return r;e=r.value}const a=r.coerce.call(this,e,t,n);return a instanceof Errors.Err?{value:e,errors:a}:{value:a}}),r.pre&&(i.prototype._base=function(e,t,n){if(s.prototype._base){const r=s.prototype._base.call(this,e,t,n);if(r.errors)return r;e=r.value}const a=r.pre.call(this,e,t,n);return a instanceof Errors.Err?{value:e,errors:a}:{value:a}}),r.rules)for(let e=0;e<r.rules.length;++e){const t=r.rules[e],a=t.params?t.params instanceof Any?t.params._inner.children.map(e=>e.key):Object.keys(t.params):[],s=t.params?Cast.schema(t.params):null;i.prototype[t.name]=function(){if(arguments.length>a.length)throw new Error("Unexpected number of arguments");const e=Array.prototype.slice.call(arguments);let r=!1;const i={};for(let t=0;t<a.length;++t)i[a[t]]=e[t],!r&&Ref.isRef(e[t])&&(r=!0);let o;if(s&&n.assert(i,s),t.validate){const e=function(e,n,r){return t.validate.call(this,i,e,n,r)};o=this._test(t.name,i,e,{description:t.description,hasRef:r})}else o=this.clone();return t.setup&&t.setup.call(o,i),o}}r.describe&&(i.prototype.describe=function(){const e=s.prototype.describe.call(this);return r.describe.call(this,e)});const o=new i;n[r.name]=function(){return o}}return n},t.extensionsSchema=internals.array.items(internals.object.keys({base:internals.object.type(Any,"Joi object"),name:internals.string.required(),coerce:internals.object._func().arity(3),pre:internals.object._func().arity(3),language:internals.object,describe:internals.object._func().arity(1),rules:internals.array.items(internals.object.keys({name:internals.string.required(),setup:internals.object._func().arity(1),validate:internals.object._func().arity(4),params:[internals.object.pattern(/.*/,internals.object.type(Any,"Joi object")),internals.object.type(internals.object.constructor,"Joi object")],description:[internals.string,internals.object._func().arity(1)]}).or("setup","validate"))})).strict(),t}};module.exports=internals.root();