UNPKG

@tragedy-labs/sprite

Version:

A TypeScript driver for ArcadeDB

80 lines 3.45 kB
import { SpriteTransaction } from '../transaction/SpriteTransaction.js'; import { SIMPLE } from './regex/SIMPLE.js'; // TODO: Change the name of this to Validate or something like that, // it doesn't only validate ArcadeDB stuff. /** * Static methods for validation of arguments in Sprite. */ class ArcadeValidation { static transaction = (trx) => { if (trx instanceof SpriteTransaction && trx.id) { return true; } else { throw new TypeError(`Recieved an argument that could not be validated as a SpriteTransaction. ${this.getVariableDescription(trx)}`); } }; static bucketName = (variable) => { if (typeof variable === 'string' && SIMPLE.test(variable)) { return true; } if (Array.isArray(variable)) { return variable.every((item) => this.bucketName(item)); } throw new TypeError(`The supplied argument could not be validated as a properly formatted bucket name for ArcadeDB. ${this.getVariableDescription(variable)}`); }; /** * Test a string to validate it as a database name in ArcadeDB. * @param value The string to be tested for existence and non-emptiness * @returns {boolean} `true` or `false` depending on the presence of a non-empty string */ static databaseName = (variable) => { if (SIMPLE.test(variable)) { return true; } else { throw new TypeError(`The supplied argument could not be validated as a properly formatted database name for ArcadeDB. Names with spaces and odd symbols can cause problems. ${this.getVariableDescription(variable)}`); } }; static simpleIdentifier = (variable) => { if (SIMPLE.test(variable)) { return true; } else { throw new TypeError(`The supplied argument could not be validated as an identifier for ArcadeDB Sprite currently only allows simple identifiers as described in the documentation: https://docs.arcadedb.com/#SQL-Syntax. ${this.getVariableDescription(variable)}`); } }; /** * Test a string to validate it as a type name in ArcadeDB. * @param value The string to be tested for existence and non-emptiness * @returns {boolean} `true` or `false` depending on the presence of a non-empty string */ static typeName = (variable) => { if (SIMPLE.test(variable)) { return true; } else { throw new TypeError(`The supplied argument could not be validated as a properly formatted type name for ArcadeDB. ${this.getVariableDescription(variable)}`); } }; /** * Validate a URL (string). * @param value The URL to be validated. * @returns {boolean} `true` or `false` depending on the validity of the URL */ static url = (variable) => { try { new URL(variable); return true; } catch (error) { throw new TypeError(`The supplied argument could not be validated as properly formatted URL. ${this.getVariableDescription(variable)}`); } }; static getVariableDescription = (variable) => { return `The supplied argument was: [${JSON.stringify(variable)}], which is of type: [${typeof variable}].`; }; } export { ArcadeValidation }; export const validation = new ArcadeValidation(); //# sourceMappingURL=ArcadeValidation.js.map