Expose host params validation methods as per bb2a3fb

dev
KaKi87 2023-01-26 20:01:12 +01:00
parent fed1e76b73
commit 4c5c08bc44
1 changed files with 12 additions and 5 deletions

17
mod.js
View File

@ -60,6 +60,13 @@ const account = username => {
return _account;
};
export const
isHostNameValid = name => !Joi.string().validate(name).error,
isHostAddressValid = address => !Joi.string().hostname().validate(address).error,
isHostPortValid = port => !Joi.number().integer().validate(port).error,
isHostUsernameValid = username => !Joi.string().validate(username).error,
isHostPasswordValid = password => !Joi.string().validate(password).error;
const Account = function(username, password){
let _account = account(username);
let _isTotpLocked = _account.isTotpEnabled();
@ -80,15 +87,15 @@ const Account = function(username, password){
const _setHost = host => {
const data = _getData();
const _host = data.hosts.find(_host => _host.name === host.name);
if(Joi.string().validate(host.name).error)
if(!isHostNameValid(host.name))
throw new Error('INVALID_NAME');
if(Joi.string().hostname().validate(host.address).error)
if(!isHostAddressValid(host.address))
throw new Error('INVALID_ADDRESS');
if(Joi.number().integer().validate(host.port).error)
if(!isHostPortValid(host.port))
throw new Error('INVALID_PORT');
if(Joi.string().validate(host.username).error)
if(!isHostUsernameValid(host.username))
throw new Error('INVALID_USER');
if(Joi.string().validate(host.password).error)
if(!isHostPasswordValid(host.password))
throw new Error('INVALID_PASSWORD');
if(_host)
Object.assign(_host, host);