gtg2json/node_modules/joi/lib/types/lazy/index.js

54 lines
1005 B
JavaScript

'use strict';
// Load modules
const Any = require('../any');
const Hoek = require('hoek');
// Declare internals
const internals = {};
internals.Lazy = class extends Any {
constructor() {
super();
this._type = 'lazy';
}
_base(value, state, options) {
const result = { value };
const lazy = this._flags.lazy;
if (!lazy) {
result.errors = this.createError('lazy.base', null, state, options);
return result;
}
const schema = lazy();
if (!(schema instanceof Any)) {
result.errors = this.createError('lazy.schema', null, state, options);
return result;
}
return schema._validate(value, state, options);
}
set(fn) {
Hoek.assert(typeof fn === 'function', 'You must provide a function as first argument');
const obj = this.clone();
obj._flags.lazy = fn;
return obj;
}
};
module.exports = new internals.Lazy();