neo4j-driver-core
Version:
Internals of neo4j-driver
585 lines (584 loc) • 35.3 kB
JavaScript
;
/**
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.rule = void 0;
var mapping_highlevel_1 = require("./mapping.highlevel");
var graph_types_1 = require("./graph-types");
var spatial_types_1 = require("./spatial-types");
var temporal_types_1 = require("./temporal-types");
var vector_1 = require("./vector");
var error_1 = require("./error");
var integer_1 = __importStar(require("./integer"));
var uuid_1 = require("./uuid");
/**
* @property {function(rule: ?Rule)} asBoolean Create a {@link Rule} that validates the value is a Boolean.
*
* @property {function(rule: ?Rule)} asString Create a {@link Rule} that validates the value is a String.
*
* @property {function(rule: ?Rule & { isInteger?: boolean })} asNumber Create a {@link Rule} that validates the value is a {@link Number}.
*
* @property {function(rule: ?Rule & { acceptNumber?: boolean })} asBigInt Create a {@link Rule} that validates the value is a {@link BigInt}.
*
* @property {function(rule: ?Rule & { acceptNumber?: boolean })} asInteger Create a {@link Rule} that validates the value is an {@link Integer}.
*
* @property {function(rule: ?Rule)} asNode Create a {@link Rule} that validates the value is a {@link Node}.
*
* @property {function(rule: ?Rule)} asRelationship Create a {@link Rule} that validates the value is a {@link Relationship}.
*
* @property {function(rule: ?Rule)} asPath Create a {@link Rule} that validates the value is a {@link Path}.
*
* @property {function(rule: ?Rule)} asPoint Create a {@link Rule} that validates the value is a {@link Point}.
*
* @property {function(rule: ?Rule & { stringify?: boolean })} asDuration Create a {@link Rule} that validates the value is a {@link Duration}.
*
* @property {function(rule: ?Rule & { stringify?: boolean })} asLocalTime Create a {@link Rule} that validates the value is a {@link LocalTime}.
*
* @property {function(rule: ?Rule & { stringify?: boolean })} asTime Create a {@link Rule} that validates the value is a {@link Time}.
*
* @property {function(rule: ?Rule & { stringify?: boolean, jsNativeDate?: boolean })} asDate Create a {@link Rule} that validates the value is a {@link Date}.
*
* @property {function(rule: ?Rule & { stringify?: boolean, jsNativeDate?: boolean })} asLocalDateTime Create a {@link Rule} that validates the value is a {@link LocalDateTime}.
*
* @property {function(rule: ?Rule & { stringify?: boolean, jsNativeDate?: boolean })} asDateTime Create a {@link Rule} that validates the value is a {@link DateTime}.
*
* @property {function(rule: ?Rule & { apply?: Rule })} asList Create a {@link Rule} that validates the value is a List.
*
* @property {function(rule: ?Rule & { asTypedList?: boolean, dimension?: number, type?: VectorType })} asVector Create a {@link Rule} that validates the value is a {@link Vector}.
*
* @property {function(rule: ?Rule & { stringify?: boolean })} asUUID Create a {@link Rule} that validates the value is a {@link UUID}.
*
* @property {function(rules: Rules)} asObject Create a {@link Rule} for an object, allowing complex mapping of even nested results.
*/
exports.rule = Object.freeze({
/**
* Create a {@link Rule} that validates the value is a Boolean.
*
* @param {Rule | undefined} rule Configurations for the rule
* @returns {Rule} A new rule for the value
*/
asBoolean: function (rule) {
return __assign({ validate: function (value, field) {
if (typeof value !== 'boolean') {
throw new TypeError("".concat(field, " should be a boolean but received ").concat(typeof value));
}
} }, rule);
},
/**
* Create a {@link Rule} that validates the value is a String.
*
* Optionally takes a {@link Rule}, in which case the returned rule will keep all fields of the one provided.
*
* @param {Rule | undefined} rule Configurations for the rule
* @returns {Rule} A new rule for the value
*/
asString: function (rule) {
return __assign({ validate: function (value, field) {
if (typeof value !== 'string') {
throw new TypeError("".concat(field, " should be a string but received ").concat(typeof value));
}
} }, rule);
},
/**
* Create a {@link Rule} that validates the value is a {@link Number}.
*
* Optionally takes a {@link Rule}, in which case the returned rule will keep all fields of the one provided.
*
* @param {Rule & { isInteger?: boolean } | undefined} rule Configurations for the rule.
* If `isInteger` is set to true, the created validate function will allow Integer values through, and the conversion functions will ensure results are return as numbers while parameters are transmitted as integers.
* @returns {Rule} A new rule for the value
*/
asNumber: function (rule) {
return __assign({ validate: function (value, field) {
if ((0, integer_1.isInt)(value) && (rule === null || rule === void 0 ? void 0 : rule.isInteger) !== true) {
throw new TypeError('Number returned as Integer Object. To use asNumber mapping with Integers, set "isInteger" in rule configuration.');
}
if ((rule === null || rule === void 0 ? void 0 : rule.isInteger) !== true && typeof value === 'bigint') {
throw new TypeError('Number returned as BigInt. To use asNumber mapping with integer values, set "isInteger" in rule configuration.');
}
if (typeof value !== 'number' && !((0, integer_1.isInt)(value) && (rule === null || rule === void 0 ? void 0 : rule.isInteger) === true)) {
throw new TypeError("".concat(field, " should be a number but received ").concat(typeof value));
}
if ((rule === null || rule === void 0 ? void 0 : rule.isInteger) === true && typeof value === 'number' && !Number.isInteger(value)) {
throw new TypeError("".concat(field, " should be an integer value but received decimal number."));
}
}, convert: function (value) {
if (typeof value === 'bigint') {
return Number(value);
}
if ((0, integer_1.isInt)(value)) {
return value.toNumber();
}
return value;
}, parameterConversion: function (value) {
if ((rule === null || rule === void 0 ? void 0 : rule.isInteger) === true) {
return integer_1.default.fromValue(value);
}
return value;
} }, rule);
},
/**
* Create a {@link Rule} that validates the value is a {@link BigInt}.
*
* Optionally takes a {@link Rule}, in which case the returned rule will keep all fields of the one provided.
*
* @returns {Rule} A new rule for the value
*/
asBigInt: function (rule) {
return __assign({ validate: function (value, field) {
if (typeof value !== 'bigint' && ((rule === null || rule === void 0 ? void 0 : rule.acceptNumber) !== true || typeof value !== 'number') && !(0, integer_1.isInt)(value)) {
throw new TypeError("".concat(field, " should be a bigint but received ").concat(typeof value));
}
}, convert: function (value) {
if (typeof value === 'number') {
return BigInt(value);
}
if ((0, integer_1.isInt)(value)) {
return value.toBigInt();
}
return value;
} }, rule);
},
/**
* Create a {@link Rule} that validates the value is an {@link Integer}.
*
* Optionally takes a {@link Rule}, in which case the returned rule will keep all fields of the one provided.
*
* @param {Rule & { acceptNumber?: boolean } | undefined} rule Configurations for the rule, if `acceptNumber` is set to true, the created validate function will allow Numbers through and the conversion functions will turn Numbers into Integers.
* @returns {Rule} A new rule for the value
*/
asInteger: function (rule) {
return __assign({ validate: function (value, field) {
if (typeof value !== 'bigint' && !(0, integer_1.isInt)(value) && !(typeof value === 'number' && (rule === null || rule === void 0 ? void 0 : rule.acceptNumber) === true)) {
throw new TypeError("".concat(field, " should be an Integer but received ").concat(typeof value));
}
}, convert: function (value) {
if (typeof value === 'bigint') {
return integer_1.default.fromValue(value);
}
return value;
} }, rule);
},
/**
* Create a {@link Rule} that validates the value is a {@link Node}.
*
* Optionally takes a {@link Rule}, in which case the returned rule will keep all fields of the one provided.
*
* @example
* const actingJobsRules: Rules = {
* // Converts the person node to a Person object in accordance with provided rules
* person: neo4j.rule.asNode({
* convert: (node: Node) => node.as(Person, personRules)
* }),
* // Returns the movie node as a Node
* movie: neo4j.rule.asNode({}),
* }
*
* @param {Rule | undefined} rule Configurations for the rule
* @returns {Rule} A new rule for the value
*/
asNode: function (rule) {
return __assign({ validate: function (value, field) {
if (!(0, graph_types_1.isNode)(value)) {
throw new TypeError("".concat(field, " should be a Node but received ").concat(typeof value));
}
} }, rule);
},
/**
* Create a {@link Rule} that validates the value is a {@link Relationship}.
*
* Optionally takes a {@link Rule}, in which case the returned rule will keep all fields of the one provided.
*
* @example
* const actingJobsRules: Rules = {
* // Converts the role relationship to a Role object in accordance with provided rules
* role: neo4j.rule.asRelationship({
* convert: (rel: Relationship) => rel.as(Role, roleRules)
* }),
* // Returns the employment relationship as a Relationship
* employment: neo4j.rule.asRelationship({}),
* }
*
* @param {Rule | undefined} rule Configurations for the rule.
* @returns {Rule} A new rule for the value
*/
asRelationship: function (rule) {
return __assign({ validate: function (value, field) {
if (!(0, graph_types_1.isRelationship)(value)) {
throw new TypeError("".concat(field, " should be a Relationship but received ").concat(typeof value));
}
} }, rule);
},
/**
* Create a {@link Rule} that validates the value is a {@link Path}
*
* Optionally takes a {@link Rule}, in which case the returned rule will keep all fields of the one provided.
*
* @param {Rule | undefined} rule Configurations for the rule
* @returns {Rule} A new rule for the value
*/
asPath: function (rule) {
return __assign({ validate: function (value, field) {
if (!(0, graph_types_1.isPath)(value)) {
throw new TypeError("".concat(field, " should be a Path but received ").concat(typeof value));
}
} }, rule);
},
/**
* Create a {@link Rule} that validates the value is a {@link Point}
*
* Optionally takes a {@link Rule}, in which case the returned rule will keep all fields of the one provided.
*
* @param {Rule | undefined} rule Configurations for the rule
* @returns {Rule} A new rule for the value
*/
asPoint: function (rule) {
return __assign({ validate: function (value, field) {
if (!(0, spatial_types_1.isPoint)(value)) {
throw new TypeError("".concat(field, " should be a Point but received ").concat(typeof value));
}
} }, rule);
},
/**
* Create a {@link Rule} that validates the value is a {@link Duration}
*
* Optionally takes a {@link Rule}, in which case the returned rule will keep all fields of the one provided.
*
* @param {Rule & { stringify?: boolean } | undefined} rule Configurations for the rule. If `stringify` is set, the returned rule will have `convert` and `parameterConversion` functions which automatically convert between strings in user code and {@link Duration}s in the database.
* @returns {Rule} A new rule for the value
*/
asDuration: function (rule) {
if ((rule === null || rule === void 0 ? void 0 : rule.stringify) != null && ((rule === null || rule === void 0 ? void 0 : rule.convert) != null || rule.parameterConversion != null)) {
throw (0, error_1.newError)('Provided rule already has convert and/or parameterConversion function, stringify can not be used in combination with custom conversion functions.');
}
return __assign({ validate: function (value, field) {
if (!(0, temporal_types_1.isDuration)(value)) {
throw new TypeError("".concat(field, " should be a Duration but received ").concat(typeof value));
}
}, convert: function (value) { return (rule === null || rule === void 0 ? void 0 : rule.stringify) === true ? value.toString() : value; }, parameterConversion: (rule === null || rule === void 0 ? void 0 : rule.stringify) === true ? function (str) { return temporal_types_1.Duration.fromString(str); } : undefined }, rule);
},
/**
* Create a {@link Rule} that validates the value is a {@link LocalTime}
*
* @param {Rule & { stringify?: boolean } | undefined} rule Configurations for the rule. If `stringify` is set, the returned rule will have `convert` and `parameterConversion` functions which automatically convert between strings in user code and {@link LocalTime}s in the database.
* @returns {Rule} A new rule for the value
*/
asLocalTime: function (rule) {
if ((rule === null || rule === void 0 ? void 0 : rule.stringify) != null && ((rule === null || rule === void 0 ? void 0 : rule.convert) != null || rule.parameterConversion != null)) {
throw (0, error_1.newError)('Provided rule already has convert and/or parameterConversion function, stringify can not be used in combination with custom conversion functions.');
}
return __assign({ validate: function (value, field) {
if (!(0, temporal_types_1.isLocalTime)(value)) {
throw new TypeError("".concat(field, " should be a LocalTime but received ").concat(typeof value));
}
}, convert: function (value) { return (rule === null || rule === void 0 ? void 0 : rule.stringify) === true ? value.toString() : value; }, parameterConversion: (rule === null || rule === void 0 ? void 0 : rule.stringify) === true ? function (str) { return temporal_types_1.LocalTime.fromString(str); } : undefined }, rule);
},
/**
* Create a {@link Rule} that validates the value is a {@link Time}
*
* @param {Rule & { stringify?: boolean } | undefined} rule Configurations for the rule. If `stringify` is set, the returned rule will have `convert` and `parameterConversion` functions which automatically convert between strings in user code and {@link Time}s in the database.
* @returns {Rule} A new rule for the value
*/
asTime: function (rule) {
if ((rule === null || rule === void 0 ? void 0 : rule.stringify) != null && ((rule === null || rule === void 0 ? void 0 : rule.convert) != null || rule.parameterConversion != null)) {
throw (0, error_1.newError)('Provided rule already has convert and/or parameterConversion function, stringify can not be used in combination with custom conversion functions.');
}
return __assign({ validate: function (value, field) {
if (!(0, temporal_types_1.isTime)(value)) {
throw new TypeError("".concat(field, " should be a Time but received ").concat(typeof value));
}
}, convert: function (value) { return (rule === null || rule === void 0 ? void 0 : rule.stringify) === true ? value.toString() : value; }, parameterConversion: (rule === null || rule === void 0 ? void 0 : rule.stringify) === true ? function (str) { return temporal_types_1.Time.fromString(str); } : undefined }, rule);
},
/**
* Create a {@link Rule} that validates the value is a {@link Date}
*
* @param {Rule & { stringify?: boolean, jsNativeDate?: boolean } | undefined} rule Configurations for the rule. If `stringify`/`jsNativeDate` is set, the returned rule will have `convert` and `parameterConversion` functions which automatically convert between strings/JavaScript Dates in user code and {@link Date}s in the database.
* @returns {Rule} A new rule for the value
*/
asDate: function (rule) {
if ((rule === null || rule === void 0 ? void 0 : rule.stringify) != null && ((rule === null || rule === void 0 ? void 0 : rule.convert) != null || rule.parameterConversion != null)) {
throw (0, error_1.newError)('Provided rule already has convert and/or parameterConversion function, stringify can not be used in combination with custom conversion functions.');
}
if ((rule === null || rule === void 0 ? void 0 : rule.jsNativeDate) != null && ((rule === null || rule === void 0 ? void 0 : rule.convert) != null || rule.parameterConversion != null)) {
throw (0, error_1.newError)('Provided rule already has convert and/or parameterConversion function, jsNativeDate can not be used in combination with custom conversion functions.');
}
if ((rule === null || rule === void 0 ? void 0 : rule.stringify) === true && (rule === null || rule === void 0 ? void 0 : rule.jsNativeDate) === true) {
throw (0, error_1.newError)('both stringify and jsNativeDate cannot be set; use one or neither');
}
var parameterConversion;
if ((rule === null || rule === void 0 ? void 0 : rule.stringify) === true) {
parameterConversion = function (str) { return temporal_types_1.Date.fromString(str); };
}
if ((rule === null || rule === void 0 ? void 0 : rule.jsNativeDate) === true) {
parameterConversion = function (standardDate) { return temporal_types_1.Date.fromStandardDateLocal(standardDate); };
}
return __assign({ validate: function (value, field) {
if (!(0, temporal_types_1.isDate)(value)) {
throw new TypeError("".concat(field, " should be a Date but received ").concat(typeof value));
}
}, convert: function (value) { return convertStdDate(value, rule); }, parameterConversion: parameterConversion }, rule);
},
/**
* Create a {@link Rule} that validates the value is a {@link LocalDateTime}
*
* @param {Rule & { stringify?: boolean, jsNativeDate?: boolean } | undefined} rule Configurations for the rule. If `stringify`/`jsNativeDate` is set, the returned rule will have `convert` and `parameterConversion` functions which automatically convert between strings/JavaScript Dates in user code and {@link LocalDateTime}s in the database.
* @returns {Rule} A new rule for the value
*/
asLocalDateTime: function (rule) {
if ((rule === null || rule === void 0 ? void 0 : rule.stringify) != null && ((rule === null || rule === void 0 ? void 0 : rule.convert) != null || rule.parameterConversion != null)) {
throw (0, error_1.newError)('Provided rule already has convert and/or parameterConversion function, stringify can not be used in combination with custom conversion functions.');
}
if ((rule === null || rule === void 0 ? void 0 : rule.jsNativeDate) != null && ((rule === null || rule === void 0 ? void 0 : rule.convert) != null || rule.parameterConversion != null)) {
throw (0, error_1.newError)('Provided rule already has convert and/or parameterConversion function, jsNativeDate can not be used in combination with custom conversion functions.');
}
if ((rule === null || rule === void 0 ? void 0 : rule.stringify) === true && (rule === null || rule === void 0 ? void 0 : rule.jsNativeDate) === true) {
throw (0, error_1.newError)('both stringify and jsNativeDate cannot be set; use one or neither');
}
var parameterConversion;
if ((rule === null || rule === void 0 ? void 0 : rule.stringify) === true) {
parameterConversion = function (str) { return temporal_types_1.LocalDateTime.fromString(str); };
}
if ((rule === null || rule === void 0 ? void 0 : rule.jsNativeDate) === true) {
parameterConversion = function (standardDate) { return temporal_types_1.LocalDateTime.fromStandardDate(standardDate); };
}
return __assign({ validate: function (value, field) {
if (!(0, temporal_types_1.isLocalDateTime)(value)) {
throw new TypeError("".concat(field, " should be a LocalDateTime but received ").concat(typeof value));
}
}, convert: function (value) { return convertStdDate(value, rule); }, parameterConversion: parameterConversion }, rule);
},
/**
* Create a {@link Rule} that validates the value is a {@link DateTime}
*
* @param {Rule & { stringify?: boolean, jsNativeDate?: boolean } | undefined} rule Configurations for the rule. If `stringify`/`jsNativeDate` is set, the returned rule will have `convert` and `parameterConversion` functions which automatically convert between strings/JavaScript Dates in user code and {@link DateTime}s in the database.
* @returns {Rule} A new rule for the value
*/
asDateTime: function (rule) {
if ((rule === null || rule === void 0 ? void 0 : rule.stringify) != null && ((rule === null || rule === void 0 ? void 0 : rule.convert) != null || rule.parameterConversion != null)) {
throw (0, error_1.newError)('Provided rule already has convert and/or parameterConversion function, stringify can not be used in combination with custom conversion functions.');
}
if ((rule === null || rule === void 0 ? void 0 : rule.jsNativeDate) != null && ((rule === null || rule === void 0 ? void 0 : rule.convert) != null || rule.parameterConversion != null)) {
throw (0, error_1.newError)('Provided rule already has convert and/or parameterConversion function, jsNativeDate can not be used in combination with custom conversion functions.');
}
if ((rule === null || rule === void 0 ? void 0 : rule.stringify) === true && (rule === null || rule === void 0 ? void 0 : rule.jsNativeDate) === true) {
throw (0, error_1.newError)('both stringify and jsNativeDate cannot be set; use one or neither');
}
var parameterConversion;
if ((rule === null || rule === void 0 ? void 0 : rule.stringify) === true) {
parameterConversion = function (str) { return temporal_types_1.DateTime.fromString(str); };
}
if ((rule === null || rule === void 0 ? void 0 : rule.jsNativeDate) === true) {
parameterConversion = function (standardDate) { return temporal_types_1.DateTime.fromStandardDate(standardDate); };
}
return __assign({ validate: function (value, field) {
if (!(0, temporal_types_1.isDateTime)(value)) {
throw new TypeError("".concat(field, " should be a DateTime but received ").concat(typeof value));
}
}, convert: function (value) { return convertStdDate(value, rule); }, parameterConversion: parameterConversion }, rule);
},
/**
* Create a {@link Rule} that validates the value is a List.
*
* Optionally taking a rule for hydrating the contained values.
*
* @param {Rule & { apply?: Rule } | undefined} rule Configurations for the rule. Setting apply to a rule will apply that rule to all elements of the list.
* @returns {Rule} A new rule for the value
*/
asList: function (rule) {
return __assign({ validate: function (list, field) {
var _a;
if (!Array.isArray(list)) {
throw new TypeError("".concat(field, " should be a list but received ").concat(typeof list));
}
var validate = (_a = rule === null || rule === void 0 ? void 0 : rule.apply) === null || _a === void 0 ? void 0 : _a.validate;
if (validate != null) {
list.forEach(function (value, index) { return validate(value, "".concat(field, "[").concat(index, "]")); });
}
}, convert: function (list, field) {
if ((rule === null || rule === void 0 ? void 0 : rule.apply) != null) {
return list.map(function (value, index) { return (0, mapping_highlevel_1.valueAs)(value, "".concat(field, "[").concat(index, "]"), rule.apply); });
}
return list;
}, parameterConversion: function (list) {
var apply = rule === null || rule === void 0 ? void 0 : rule.apply;
if (apply != null) {
return list.map(function (value) { return (0, mapping_highlevel_1.optionalParameterConversion)(value, apply); });
}
return list;
} }, rule);
},
/**
* Create a {@link Rule} that validates the value is a {@link Vector}.
*
* @param {Rule & { asTypedList?: boolean, dimension?: number, type?: VectorType } | undefined} rule Configurations for the rule. Setting asTypedList will automatically convert between TypedList in user code and Vectors in the database.
* @returns {Rule} A new rule for the value
*/
asVector: function (rule) {
if ((rule === null || rule === void 0 ? void 0 : rule.asTypedList) != null && ((rule === null || rule === void 0 ? void 0 : rule.convert) != null || rule.parameterConversion != null)) {
throw (0, error_1.newError)('Provided rule already has convert and/or parameterConversion function, asTypedList can not be used in combination with custom conversion functions.');
}
return __assign({ validate: function (value, field) {
if (!(0, vector_1.isVector)(value)) {
throw new TypeError("".concat(field, " should be a vector but received ").concat(typeof value));
}
if ((rule === null || rule === void 0 ? void 0 : rule.dimension) != null && value.asTypedArray().length !== rule.dimension) {
throw new TypeError("".concat(field, " should be a vector of length ").concat(rule.dimension, " but received length ").concat(value.asTypedArray().length));
}
if ((rule === null || rule === void 0 ? void 0 : rule.type) != null && value.getType() !== rule.type) {
throw new TypeError("".concat(field, " should be a vector of type ").concat(rule.type, " but received type ").concat(value.getType()));
}
}, convert: function (value) {
if ((rule === null || rule === void 0 ? void 0 : rule.asTypedList) === true) {
return value._typedArray;
}
return value;
}, parameterConversion: (rule === null || rule === void 0 ? void 0 : rule.asTypedList) === true ? function (typedArray) { return (0, vector_1.vector)(typedArray); } : undefined }, rule);
},
/**
* Create a {@link Rule} that validates the value is a {@link UUID}.
*
* @param {Rule & { stringify?: boolean } | undefined} rule Configurations for the rule. Setting stringify will automatically convert between Strings in user code and UUIDs in the database.
* @returns {Rule} A new rule for the value
*/
asUUID: function (rule) {
if ((rule === null || rule === void 0 ? void 0 : rule.stringify) != null && ((rule === null || rule === void 0 ? void 0 : rule.convert) != null || rule.parameterConversion != null)) {
throw (0, error_1.newError)('Provided rule already has convert and/or parameterConversion function, stringify can not be used in combination with custom conversion functions.');
}
return __assign({ validate: function (value, field) {
if (!(0, uuid_1.isUUID)(value)) {
throw new TypeError("".concat(field, " should be a UUID but received ").concat(typeof value));
}
}, convert: function (value) {
if ((rule === null || rule === void 0 ? void 0 : rule.stringify) === true) {
return value.toString();
}
return value;
}, parameterConversion: (rule === null || rule === void 0 ? void 0 : rule.stringify) === true ? function (string) { return (0, uuid_1.uuid)(string); } : undefined }, rule);
},
/**
* Create a {@link Rule} for an object, allowing complex mapping of even nested results
*
* NOTE: When using this rule, object identifiers will be mapped according to any name mapping set with neo4j.RecordObjectMapping.translateIdentifiers.
*
* @param {Rules} rules rules for the fields of the object.
* @param {GenericConstructor} constructor The constructor function of the class to map to. The constructor must be callable with all arguments undefined.
* @returns {Rule} A new rule for the value
*/
asObject: function (constructorOrRules, rules) {
var GenericConstructor = typeof constructorOrRules === 'function' ? constructorOrRules : Object;
var theRules = (0, mapping_highlevel_1.getRules)(constructorOrRules, rules);
return __assign({ validate: function (value, field) {
for (var key in theRules) {
var mappedKey = theRules[key].from != null ? theRules[key].from : (0, mapping_highlevel_1.defaultNameMapping)(key);
if (value[mappedKey] == null) {
if (theRules[key].optional === true) {
continue;
}
else {
throw (0, error_1.newError)("Mapped object did not include required field ".concat(field, " with key ").concat(mappedKey, "."));
}
}
if (theRules[key].validate != null) {
theRules[key].validate(value[mappedKey], "".concat(field, "[").concat(key, "]"));
}
}
}, convert: function (value, field) {
var convertedValue = new GenericConstructor();
for (var key in theRules) {
var mappedKey = theRules[key].from != null ? theRules[key].from : (0, mapping_highlevel_1.defaultNameMapping)(key);
if (value[mappedKey] != null && theRules[key].convert != null) {
// @ts-expect-error
convertedValue[key] = theRules[key].convert(value[mappedKey], "".concat(field, "[").concat(mappedKey, "]"));
}
else {
// @ts-expect-error
convertedValue[key] = value[mappedKey];
}
}
return convertedValue;
}, parameterConversion: function (value) {
var convertedValue = {};
for (var key in theRules) {
var mappedKey = theRules[key].from != null ? theRules[key].from : (0, mapping_highlevel_1.defaultNameMapping)(key);
if (value[key] != null && theRules[key].parameterConversion != null) {
convertedValue[mappedKey] = theRules[key].parameterConversion(value[key]);
}
else {
convertedValue[mappedKey] = value[key];
}
}
return convertedValue;
} }, exports.rule);
}
});
function convertStdDate(value, rule) {
if (rule != null) {
if (rule.stringify === true) {
return value.toString();
}
else if (rule.jsNativeDate === true) {
return value.toStandardDate();
}
}
return value;
}