cobinhood-rx
Version:
CobinhoodRx is a rxjs node wrapper for the CobinhoodRx Api.
62 lines • 1.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
class Validator {
market(val) {
val = val.toUpperCase();
if (isValidMarket(val)) {
return val;
}
throw new Error(`Bad Market Name: ${val}. Accepted market name pattern {XXX-XXX}. Eg. 'BTC-USDT'`);
}
uuid(val) {
if (isValidUUID(val)) {
return val;
}
throw new Error(`Ivalid Id: ${val}.`);
}
queryObject(obj) {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
rules[key](obj[key]);
}
}
return obj;
}
}
exports.Validator = Validator;
const rules = {
limit: (val) => {
if (!isValidLimit(val)) {
throw new Error(`Limit must be within the range of 0 and 50. 0 and 50 included.`);
}
},
trading_pair_id: (val) => {
if (!isValidMarket(val)) {
throw new Error(`Bad Market Name: ${val}. Accepted market name pattern {XXX-XXX}. Eg. 'BTC-USDT'`);
}
},
currency: (val) => {
if (!isValidMarket(val)) {
throw new Error(`Bad Currency Name: ${val}. Accepted currency names pattern {XXX,1X, $X} Eg. 'BTC, 1ST, $PAC'`);
}
}
};
function isValidMarket(val) {
return new RegExp(/[$A-Z0-9]+-[$A-Z0-9]+/).test(val);
}
exports.isValidMarket = isValidMarket;
function isValidUUID(val) {
return new RegExp(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/, 'i').test(val);
}
exports.isValidUUID = isValidUUID;
function isValidLimit(val) {
return (val >= 0 && val <= 50);
}
exports.isValidLimit = isValidLimit;
function isValidCurrency(val) {
return new RegExp(/[A-Z0-9]{2,5}/).test(val);
}
exports.isValidCurrency = isValidCurrency;
const Validate = new Validator();
exports.default = Validate;
//# sourceMappingURL=Validator.js.map