incubed
Version:
Typescript-version of the incubed client
54 lines • 2.52 kB
JavaScript
"use strict";
/***********************************************************
* This file is part of the Slock.it IoT Layer. *
* The Slock.it IoT Layer contains: *
* - USN (Universal Sharing Network) *
* - INCUBED (Trustless INcentivized remote Node Network) *
************************************************************
* Copyright (C) 2016 - 2018 Slock.it GmbH *
* All Rights Reserved. *
************************************************************
* You may use, distribute and modify this code under the *
* terms of the license contract you have concluded with *
* Slock.it GmbH. *
* For information about liability, maintenance etc. also *
* refer to the contract concluded with Slock.it GmbH. *
************************************************************
* For more information, please refer to http://slock.it *
* For questions, please contact info@slock.it *
***********************************************************/
Object.defineProperty(exports, "__esModule", { value: true });
const Ajv = require("ajv");
/**
* the ajv instance with custom formatters and keywords
*/
exports.ajv = new Ajv();
exports.ajv.addFormat('address', /^0x[0-9a-fA-F]{40}$/);
exports.ajv.addFormat('bytes32', /^0x[0-9a-fA-F]{64}$/);
exports.ajv.addFormat('bytes64', /^0x[0-9a-fA-F]{128}$/);
exports.ajv.addFormat('hex', /^0x[0-9a-fA-F]{2,}$/);
exports.ajv.addFormat('hexWithout', /^[0-9a-fA-F]{2,}$/);
exports.ajv.addFormat('path', /^[\/a-zA-Z_\-0-9]+$/);
exports.ajv.addKeyword('timestamp', {
type: 'number',
validate: (sch, data) => sch === 'current'
? !!(data > Date.now() / 1000 - 60 || data < Date.now() / 1000 + 60)
: !!(data === 0 || Date.now() / 1000 - 3600 * 24 * 365 || data < Date.now() / 1000 + 3600 * 24 * 365)
});
/**
* validates the data and throws an error in case they are not valid.
*
* @export
* @param {Ajv.ValidateFunction} fn
* @param {any} ob
*/
function validateAndThrow(fn, ob) {
if (!fn(ob))
throw new Error('ERRKEY: invalid_data : ' + (fn).errors.map(_ => _.dataPath + '(' + JSON.stringify(_.data || _.params) + '):' + _.message).join(', ') + ':' + JSON.stringify(ob, null, 2));
}
exports.validateAndThrow = validateAndThrow;
function validate(ob, def) {
validateAndThrow(exports.ajv.compile(def), ob);
}
exports.validate = validate;
//# sourceMappingURL=validate.js.map