|
| 1 | +/** |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import Integer, { isInt } from '../integer' |
| 21 | + |
| 22 | +const ENCRYPTION_ON: string = 'ENCRYPTION_ON' |
| 23 | +const ENCRYPTION_OFF: string = 'ENCRYPTION_OFF' |
| 24 | + |
| 25 | +/** |
| 26 | + * Verifies if the object is null or empty |
| 27 | + * @param obj The subject object |
| 28 | + * @returns {boolean} True if it's empty object or null |
| 29 | + */ |
| 30 | +function isEmptyObjectOrNull(obj?: any): boolean { |
| 31 | + if (obj === null) { |
| 32 | + return true |
| 33 | + } |
| 34 | + |
| 35 | + if (!isObject(obj)) { |
| 36 | + return false |
| 37 | + } |
| 38 | + |
| 39 | + for (const prop in obj) { |
| 40 | + if (Object.prototype.hasOwnProperty.bind(obj, prop)) { |
| 41 | + return false |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return true |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Verify if it's an object |
| 50 | + * @param obj The subject |
| 51 | + * @returns {boolean} True if it's an object |
| 52 | + */ |
| 53 | +function isObject(obj: any): boolean { |
| 54 | + return typeof obj === 'object' && !Array.isArray(obj) && obj !== null |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Check and normalize given query and parameters. |
| 59 | + * @param {string|{text: string, parameters: Object}} query the query to check. |
| 60 | + * @param {Object} parameters |
| 61 | + * @return {{validatedQuery: string|{text: string, parameters: Object}, params: Object}} the normalized query with parameters. |
| 62 | + * @throws TypeError when either given query or parameters are invalid. |
| 63 | + */ |
| 64 | +function validateQueryAndParameters( |
| 65 | + query: string | { text: string; parameters: Object }, |
| 66 | + parameters: Object |
| 67 | +): { |
| 68 | + validatedQuery: string | { text: string; parameters: Object } |
| 69 | + params: Object |
| 70 | +} { |
| 71 | + let validatedQuery = query |
| 72 | + let params = parameters || {} |
| 73 | + |
| 74 | + if (typeof query === 'object' && query.text) { |
| 75 | + validatedQuery = query.text |
| 76 | + params = query.parameters || {} |
| 77 | + } |
| 78 | + |
| 79 | + assertCypherQuery(validatedQuery) |
| 80 | + assertQueryParameters(params) |
| 81 | + |
| 82 | + return { validatedQuery, params } |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Assert it's a object |
| 87 | + * @param {any} obj The subject |
| 88 | + * @param {string} objName The object name |
| 89 | + * @returns {object} The subject object |
| 90 | + * @throws {TypeError} when the supplied param is not an object |
| 91 | + */ |
| 92 | +function assertObject(obj: any, objName: string): Object { |
| 93 | + if (!isObject(obj)) { |
| 94 | + throw new TypeError( |
| 95 | + objName + ' expected to be an object but was: ' + JSON.stringify(obj) |
| 96 | + ) |
| 97 | + } |
| 98 | + return obj |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Assert it's a string |
| 103 | + * @param {any} obj The subject |
| 104 | + * @param {string} objName The object name |
| 105 | + * @returns {string} The subject string |
| 106 | + * @throws {TypeError} when the supplied param is not a string |
| 107 | + */ |
| 108 | +function assertString(obj: any, objName: Object): string { |
| 109 | + if (!isString(obj)) { |
| 110 | + throw new TypeError( |
| 111 | + objName + ' expected to be string but was: ' + JSON.stringify(obj) |
| 112 | + ) |
| 113 | + } |
| 114 | + return obj |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Assert it's a number |
| 119 | + * @param {any} obj The subject |
| 120 | + * @param {string} objName The object name |
| 121 | + * @returns {number} The number |
| 122 | + * @throws {TypeError} when the supplied param is not a number |
| 123 | + */ |
| 124 | +function assertNumber(obj: any, objName: string): Number { |
| 125 | + if (typeof obj !== 'number') { |
| 126 | + throw new TypeError( |
| 127 | + objName + ' expected to be a number but was: ' + JSON.stringify(obj) |
| 128 | + ) |
| 129 | + } |
| 130 | + return obj |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * Assert it's a number or integer |
| 135 | + * @param {any} obj The subject |
| 136 | + * @param {string} objName The object name |
| 137 | + * @returns {number|Integer} The subject object |
| 138 | + * @throws {TypeError} when the supplied param is not a number or integer |
| 139 | + */ |
| 140 | +function assertNumberOrInteger(obj: any, objName: string): number | Integer { |
| 141 | + if (typeof obj !== 'number' && !isInt(obj)) { |
| 142 | + throw new TypeError( |
| 143 | + objName + |
| 144 | + ' expected to be either a number or an Integer object but was: ' + |
| 145 | + JSON.stringify(obj) |
| 146 | + ) |
| 147 | + } |
| 148 | + return obj |
| 149 | +} |
| 150 | + |
| 151 | +/** |
| 152 | + * Assert it's a valid datae |
| 153 | + * @param {any} obj The subject |
| 154 | + * @param {string} objName The object name |
| 155 | + * @returns {Date} The valida date |
| 156 | + * @throws {TypeError} when the supplied param is not a valid date |
| 157 | + */ |
| 158 | +function assertValidDate(obj: any, objName: string): Date { |
| 159 | + if (Object.prototype.toString.call(obj) !== '[object Date]') { |
| 160 | + throw new TypeError( |
| 161 | + objName + |
| 162 | + ' expected to be a standard JavaScript Date but was: ' + |
| 163 | + JSON.stringify(obj) |
| 164 | + ) |
| 165 | + } |
| 166 | + if (Number.isNaN(obj.getTime())) { |
| 167 | + throw new TypeError( |
| 168 | + objName + |
| 169 | + ' expected to be valid JavaScript Date but its time was NaN: ' + |
| 170 | + JSON.stringify(obj) |
| 171 | + ) |
| 172 | + } |
| 173 | + return obj |
| 174 | +} |
| 175 | + |
| 176 | +/** |
| 177 | + * Validates a cypher query string |
| 178 | + * @param {any} obj The query |
| 179 | + * @returns {void} |
| 180 | + * @throws {TypeError} if the query is not valid |
| 181 | + */ |
| 182 | +function assertCypherQuery(obj: any): void { |
| 183 | + assertString(obj, 'Cypher query') |
| 184 | + if (obj.trim().length === 0) { |
| 185 | + throw new TypeError('Cypher query is expected to be a non-empty string.') |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +/** |
| 190 | + * Validates if the query parameters is an object |
| 191 | + * @param {any} obj The parameters |
| 192 | + * @returns {void} |
| 193 | + * @throws {TypeError} if the parameters is not valid |
| 194 | + */ |
| 195 | +function assertQueryParameters(obj: any): void { |
| 196 | + if (!isObject(obj)) { |
| 197 | + // objects created with `Object.create(null)` do not have a constructor property |
| 198 | + const constructor = obj.constructor ? ' ' + obj.constructor.name : '' |
| 199 | + throw new TypeError( |
| 200 | + `Query parameters are expected to either be undefined/null or an object, given:${constructor} ${obj}` |
| 201 | + ) |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +/** |
| 206 | + * Verify if the supplied object is a string |
| 207 | + * |
| 208 | + * @param str The string |
| 209 | + * @returns {boolean} True if the supplied object is an string |
| 210 | + */ |
| 211 | +function isString(str: any): boolean { |
| 212 | + return Object.prototype.toString.call(str) === '[object String]' |
| 213 | +} |
| 214 | + |
| 215 | +export { |
| 216 | + isEmptyObjectOrNull, |
| 217 | + isObject, |
| 218 | + isString, |
| 219 | + assertObject, |
| 220 | + assertString, |
| 221 | + assertNumber, |
| 222 | + assertNumberOrInteger, |
| 223 | + assertValidDate, |
| 224 | + validateQueryAndParameters, |
| 225 | + ENCRYPTION_ON, |
| 226 | + ENCRYPTION_OFF |
| 227 | +} |
0 commit comments