UNPKG

@nxtoai/gati

Version:

A flexible Aerospike service for NestJS applications

136 lines (135 loc) 7.16 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GatiValidator = exports.GatiValidationError = void 0; const common_1 = require("@nestjs/common"); const error_messages_1 = require("./error-messages"); class GatiValidationError extends Error { constructor(message) { super(message); this.name = 'GatiValidationError'; } } exports.GatiValidationError = GatiValidationError; let GatiValidator = class GatiValidator { validateKey(key) { if (!key) { throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.KEY_EMPTY.code}: ${error_messages_1.ERROR_MESSAGES.KEY_EMPTY.message}`); } if (typeof key !== 'string') { throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.KEY_NOT_STRING.code}: ${error_messages_1.ERROR_MESSAGES.KEY_NOT_STRING.message}`); } if (key.length > 255) { throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.KEY_TOO_LONG.code}: ${error_messages_1.ERROR_MESSAGES.KEY_TOO_LONG.message}`); } } validateBins(bins) { if (!bins) { throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.BINS_EMPTY.code}: ${error_messages_1.ERROR_MESSAGES.BINS_EMPTY.message}`); } if (typeof bins !== 'object') { throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.BINS_NOT_OBJECT.code}: ${error_messages_1.ERROR_MESSAGES.BINS_NOT_OBJECT.message}`); } if (Object.keys(bins).length === 0) { throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.BINS_NO_KEYS.code}: ${error_messages_1.ERROR_MESSAGES.BINS_NO_KEYS.message}`); } // Validate each bin value for (const [key, value] of Object.entries(bins)) { this.validateBinKey(key); this.validateBinValue(value); } } validateBinKey(key) { if (!key) { throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.BIN_KEY_EMPTY.code}: ${error_messages_1.ERROR_MESSAGES.BIN_KEY_EMPTY.message}`); } if (typeof key !== 'string') { throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.BIN_KEY_NOT_STRING.code}: ${error_messages_1.ERROR_MESSAGES.BIN_KEY_NOT_STRING.message}`); } if (key.length > 14) { throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.BIN_KEY_TOO_LONG.code}: ${error_messages_1.ERROR_MESSAGES.BIN_KEY_TOO_LONG.message}`); } } validateBinValue(value) { if (value === undefined) { throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.BIN_VALUE_UNDEFINED.code}: ${error_messages_1.ERROR_MESSAGES.BIN_VALUE_UNDEFINED.message}`); } // Check for supported data types const type = typeof value; if (type === 'object' && value !== null) { if (Array.isArray(value)) { this.validateArray(value); } else if (value instanceof Date) { // Date is supported } else if (value instanceof Map || value instanceof Set) { throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.BIN_VALUE_MAP_SET.code}: ${error_messages_1.ERROR_MESSAGES.BIN_VALUE_MAP_SET.message}`); } else { // Recursively validate nested objects for (const [key, val] of Object.entries(value)) { this.validateBinKey(key); this.validateBinValue(val); } } } else if (!['string', 'number', 'boolean', 'object'].includes(type)) { throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.BIN_VALUE_UNSUPPORTED_TYPE.code}: ${error_messages_1.ERROR_MESSAGES.BIN_VALUE_UNSUPPORTED_TYPE.message}: ${type}`); } } validateArray(arr) { if (arr.length > 1000000) { throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.ARRAY_TOO_LARGE.code}: ${error_messages_1.ERROR_MESSAGES.ARRAY_TOO_LARGE.message}`); } for (const item of arr) { this.validateBinValue(item); } } validateTtl(ttl) { if (ttl !== undefined) { if (typeof ttl !== 'number') { throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.TTL_NOT_NUMBER.code}: ${error_messages_1.ERROR_MESSAGES.TTL_NOT_NUMBER.message}`); } if (ttl < 0) { throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.TTL_NEGATIVE.code}: ${error_messages_1.ERROR_MESSAGES.TTL_NEGATIVE.message}`); } if (ttl > 315360000) { // 10 years in seconds throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.TTL_TOO_LARGE.code}: ${error_messages_1.ERROR_MESSAGES.TTL_TOO_LARGE.message}`); } } } validateScanOptions(options) { if (options) { if (typeof options !== 'object') { throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.SCAN_OPTIONS_NOT_OBJECT.code}: ${error_messages_1.ERROR_MESSAGES.SCAN_OPTIONS_NOT_OBJECT.message}`); } const opts = options; if (opts.limit !== undefined) { if (typeof opts.limit !== 'number') { throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.SCAN_LIMIT_NOT_NUMBER.code}: ${error_messages_1.ERROR_MESSAGES.SCAN_LIMIT_NOT_NUMBER.message}`); } if (opts.limit < 0) { throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.SCAN_LIMIT_NEGATIVE.code}: ${error_messages_1.ERROR_MESSAGES.SCAN_LIMIT_NEGATIVE.message}`); } } if (opts.timeout !== undefined) { if (typeof opts.timeout !== 'number') { throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.SCAN_TIMEOUT_NOT_NUMBER.code}: ${error_messages_1.ERROR_MESSAGES.SCAN_TIMEOUT_NOT_NUMBER.message}`); } if (opts.timeout < 0) { throw new GatiValidationError(`${error_messages_1.ERROR_MESSAGES.SCAN_TIMEOUT_NEGATIVE.code}: ${error_messages_1.ERROR_MESSAGES.SCAN_TIMEOUT_NEGATIVE.message}`); } } } } }; exports.GatiValidator = GatiValidator; exports.GatiValidator = GatiValidator = __decorate([ (0, common_1.Injectable)() ], GatiValidator);