UNPKG

@linaframework/arango-orm

Version:

> Please check out https://github.com/awesome-graphql-space/lina and https://github.com/oknoah/final/packages/arangolize for similar projects that MAY be more up to date

224 lines (184 loc) 6.5 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _from = require('babel-runtime/core-js/array/from'); var _from2 = _interopRequireDefault(_from); var _stringify = require('babel-runtime/core-js/json/stringify'); var _stringify2 = _interopRequireDefault(_stringify); var _isFinite = require('babel-runtime/core-js/number/is-finite'); var _isFinite2 = _interopRequireDefault(_isFinite); var _set = require('babel-runtime/core-js/set'); var _set2 = _interopRequireDefault(_set); var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _field = require('./field'); var _field2 = _interopRequireDefault(_field); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var FieldType = function (_Field) { (0, _inherits3.default)(FieldType, _Field); function FieldType(basePath, path, type, options) { var internal = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false; (0, _classCallCheck3.default)(this, FieldType); var _this = (0, _possibleConstructorReturn3.default)(this, (FieldType.__proto__ || (0, _getPrototypeOf2.default)(FieldType)).call(this, basePath, path, options, internal)); _this.checkType(type); _this.type = type; return _this; } (0, _createClass3.default)(FieldType, [{ key: 'checkType', value: function checkType(type) { if (type === Boolean) return; if (type === String) return; if (type === Number) return; if (type === Date) return; if (type === _set2.default) return; if (!type.prototype.toJSON) { throw Error('Custom type \'' + type.name + '\' must have method \'toJSON\''); } if (!type.fromJSON) { throw Error('Custom type \'' + type.name + '\' must have static method \'fromJSON\''); } } }, { key: 'validate', value: function validate(data, basePath) { if (this.internal) return; var value = this.getByPath(data); if (this.isOptional(value)) return; if (!this.validateValue(value, basePath)) { this.typeError(this.type, value, basePath); } } }, { key: 'validateValue', value: function validateValue(value, basePath) { var type = this.type; var options = this.options; if ('enum' in options) { this.validateEnum(value, options, basePath); } switch (type) { case String: return this.validateString(value, options, basePath); case Number: return this.validateNumber(value, options, basePath); case Boolean: return typeof value === 'boolean'; case _set2.default: return this.validateSet(value, options, basePath); default: return value instanceof type; } } }, { key: 'validateNumber', value: function validateNumber(value, options, basePath) { if (typeof value !== 'number') return false; if (!(0, _isFinite2.default)(value)) { this.throwError('must be finite number, but have ' + value, basePath); } if ('min' in options) if (value < options.min) { this.throwError('must be more or equal ' + options.min + ', but have ' + value, basePath); } if ('max' in options) if (value > options.max) { this.throwError('must be less or equal ' + options.max + ', but have ' + value, basePath); } return true; } }, { key: 'validateString', value: function validateString(value, options, basePath) { if (typeof value !== 'string') return false; if ('regExp' in options) if (!options.regExp.test(value)) { this.throwError('must be match regExp ' + options.regExp + ', but have \'' + value + '\'', basePath); } if ('min' in options) if (value.length < options.min) { this.throwError('length must be more or equal ' + options.min + ' symbols, but have \'' + value + '\'', basePath); } if ('max' in options) if (value.length > options.max) { this.throwError('length must be less or equal ' + options.max + ' symbols, but have \'' + value + '\'', basePath); } return true; } }, { key: 'validateEnum', value: function validateEnum(value, options, basePath) { var enums = options.enum; if (enums.indexOf(value) === -1) { var enumText = (0, _stringify2.default)(enums); var valueText = this.valueToString(value); var message = 'must be one of enum ' + enumText + ', but have ' + valueText; this.throwError(message, basePath); } } }, { key: 'validateSet', value: function validateSet(value, options, basePath) { var _this2 = this; if (!(value instanceof _set2.default)) return false; if ('set' in options) { var sets = options.set; value.forEach(function (item) { if (sets.indexOf(item) === -1) { var setText = (0, _stringify2.default)(sets); var itemValue = _this2.valueToString(item); var message = 'must contain item only from ' + setText + ', but have ' + itemValue; _this2.throwError(message, basePath); } }); } return true; } }, { key: 'convertToModelValue', value: function convertToModelValue(value) { if (value == null) return value; var type = this.type; switch (type) { case String: return String(value); case Number: return Number(value); case Boolean: return Boolean(value); case Date: return new Date(value); case _set2.default: return new _set2.default(value); } return type.fromJSON(value); } }, { key: 'convertToDocumentValue', value: function convertToDocumentValue(value) { if (value == null) return value; switch (this.type) { case String: return value; case Number: return value; case Boolean: return value; case Date: return value.getTime(); case _set2.default: return (0, _from2.default)(value); } // for custom types return value.toJSON(); } }]); return FieldType; }(_field2.default); exports.default = FieldType;