New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

uniforms-bridge-json-schema

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

uniforms-bridge-json-schema - npm Package Compare versions

Comparing version

to
2.5.0

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "default", {
enumerable: true,
get: function get() {
return _JSONSchemaBridge["default"];
}
});
Object.defineProperty(exports, "JSONSchemaBridge", {
enumerable: true,
get: function get() {
return _JSONSchemaBridge["default"];
}
});
exports.__esModule = true;
require("./register");
var _JSONSchemaBridge = _interopRequireDefault(require("./JSONSchemaBridge"));
var JSONSchemaBridge_1 = require("./JSONSchemaBridge");
exports["default"] = JSONSchemaBridge_1["default"];
exports.JSONSchemaBridge = JSONSchemaBridge_1["default"];
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _Bridge2 = _interopRequireDefault(require("uniforms/Bridge"));
var _cloneDeep = _interopRequireDefault(require("lodash/cloneDeep"));
var _get = _interopRequireDefault(require("lodash/get"));
var _invariant = _interopRequireDefault(require("invariant"));
var _joinName = _interopRequireDefault(require("uniforms/joinName"));
var _lowerCase = _interopRequireDefault(require("lodash/lowerCase"));
var _omit2 = _interopRequireDefault(require("lodash/omit"));
var _upperFirst = _interopRequireDefault(require("lodash/upperFirst"));
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
var resolveRef = function resolveRef(referance, schema) {
(0, _invariant["default"])(referance.startsWith('#'), 'Reference is not an internal reference, and only such are allowed: "%s"', referance);
var resolvedReference = referance.split('/').filter(function (part) {
return part && part !== '#';
}).reduce(function (definition, next) {
return definition[next];
}, schema);
(0, _invariant["default"])(resolvedReference, 'Reference not found in schema: "%s"', referance);
return resolvedReference;
exports.__esModule = true;
var tslib_1 = require("tslib");
var Bridge_1 = tslib_1.__importDefault(require("uniforms/Bridge"));
var cloneDeep_1 = tslib_1.__importDefault(require("lodash/cloneDeep"));
var get_1 = tslib_1.__importDefault(require("lodash/get"));
var invariant_1 = tslib_1.__importDefault(require("invariant"));
var joinName_1 = tslib_1.__importDefault(require("uniforms/joinName"));
var lowerCase_1 = tslib_1.__importDefault(require("lodash/lowerCase"));
var omit_1 = tslib_1.__importDefault(require("lodash/omit"));
var upperFirst_1 = tslib_1.__importDefault(require("lodash/upperFirst"));
var resolveRef = function (referance, schema) {
invariant_1["default"](referance.startsWith('#'), 'Reference is not an internal reference, and only such are allowed: "%s"', referance);
var resolvedReference = referance
.split('/')
.filter(function (part) { return part && part !== '#'; })
.reduce(function (definition, next) { return definition[next]; }, schema);
invariant_1["default"](resolvedReference, 'Reference not found in schema: "%s"', referance);
return resolvedReference;
};
var distinctSchema = function distinctSchema(schema) {
if (schema.type === 'object') {
var distinctSchema = function (schema) {
if (schema.type === 'object') {
return schema;
}
if (schema.$ref) {
return tslib_1.__assign({}, schema, resolveRef(schema.$ref, schema));
}
return schema;
}
if (schema.$ref) {
return _objectSpread({}, schema, {}, resolveRef(schema.$ref, schema));
}
return schema;
};
var extractValue = function extractValue() {
for (var _len = arguments.length, xs = new Array(_len), _key2 = 0; _key2 < _len; _key2++) {
xs[_key2] = arguments[_key2];
}
return xs.reduce(function (x, y) {
return x === false || x === null ? '' : x !== true && x !== undefined ? x : y;
});
var extractValue = function () {
var xs = [];
for (var _i = 0; _i < arguments.length; _i++) {
xs[_i] = arguments[_i];
}
return xs.reduce(function (x, y) {
return x === false || x === null ? '' : x !== true && x !== undefined ? x : y;
});
};
var pathToName = function pathToName(path) {
if (path[0] === '.') path = path.replace(/\['(.+?)'\]/g, '.$1').replace(/\[(.+?)\]/g, '.$1').replace(/\\'/g, "'");else path = path.replace(/\//g, '.').replace(/~0/g, '~').replace(/~1/g, '/');
return path.slice(1);
var pathToName = function (path) {
if (path[0] === '.')
path = path
.replace(/\['(.+?)'\]/g, '.$1')
.replace(/\[(.+?)\]/g, '.$1')
.replace(/\\'/g, "'");
else
path = path
.replace(/\//g, '.')
.replace(/~0/g, '~')
.replace(/~1/g, '/');
return path.slice(1);
};
var toHumanLabel = function toHumanLabel(label) {
return (0, _upperFirst["default"])((0, _lowerCase["default"])(label));
};
var JSONSchemaBridge =
/*#__PURE__*/
function (_Bridge) {
(0, _inherits2["default"])(JSONSchemaBridge, _Bridge);
function JSONSchemaBridge(schema, validator) {
var _this;
(0, _classCallCheck2["default"])(this, JSONSchemaBridge);
_this = (0, _possibleConstructorReturn2["default"])(this, (0, _getPrototypeOf2["default"])(JSONSchemaBridge).call(this));
_this.schema = distinctSchema(schema);
_this._compiledSchema = {};
_this.validator = validator;
return _this;
}
(0, _createClass2["default"])(JSONSchemaBridge, [{
key: "getError",
value: function getError(name, error) {
var nameParts = (0, _joinName["default"])(null, name);
var rootName = (0, _joinName["default"])(nameParts.slice(0, -1));
var baseName = nameParts[nameParts.length - 1];
return error && error.details && error.details.find && error.details.find(function (detail) {
var path = pathToName(detail.dataPath);
return name === path || rootName === path && baseName === detail.params.missingProperty;
});
var toHumanLabel = function (label) { return upperFirst_1["default"](lowerCase_1["default"](label)); };
var JSONSchemaBridge = /** @class */ (function (_super) {
tslib_1.__extends(JSONSchemaBridge, _super);
function JSONSchemaBridge(schema, validator) {
var _this = _super.call(this) || this;
_this.schema = distinctSchema(schema);
_this._compiledSchema = {};
_this.validator = validator;
return _this;
}
}, {
key: "getErrorMessage",
value: function getErrorMessage(name, error) {
var scopedError = this.getError(name, error) || {};
return scopedError && scopedError.message || '';
}
}, {
key: "getErrorMessages",
value: function getErrorMessages(error) {
if (error) {
if (Array.isArray(error.details)) {
return error.details.reduce(function (acc, _ref) {
var message = _ref.message;
return acc.concat(message);
}, []);
JSONSchemaBridge.check = function () {
return false;
};
JSONSchemaBridge.prototype.getError = function (name, error) {
var nameParts = joinName_1["default"](null, name);
var rootName = joinName_1["default"](nameParts.slice(0, -1));
var baseName = nameParts[nameParts.length - 1];
return (error &&
error.details &&
error.details.find &&
error.details.find(function (detail) {
var path = pathToName(detail.dataPath);
return (name === path ||
(rootName === path && baseName === detail.params.missingProperty));
}));
};
JSONSchemaBridge.prototype.getErrorMessage = function (name, error) {
var scopedError = this.getError(name, error) || {};
return (scopedError && scopedError.message) || '';
};
JSONSchemaBridge.prototype.getErrorMessages = function (error) {
if (error) {
if (Array.isArray(error.details)) {
return error.details.reduce(function (acc, _a) {
var message = _a.message;
return acc.concat(message);
}, []);
}
return [error.message || error];
}
return [error.message || error];
}
return [];
}
}, {
key: "getField",
value: function getField(name) {
var _this2 = this;
return (0, _joinName["default"])(null, name).reduce(function (definition, next, nextIndex, array) {
var previous = (0, _joinName["default"])(array.slice(0, nextIndex));
var isRequired = (definition.required || (_this2._compiledSchema[previous] || {}).required || []).includes(next);
var _key = (0, _joinName["default"])(previous, next);
var _definition = _this2._compiledSchema[_key] || {};
if (next === '$' || next === '' + parseInt(next, 10)) {
(0, _invariant["default"])(definition.type === 'array', 'Field not found in schema: "%s"', name);
definition = Array.isArray(definition.items) ? definition.items[parseInt(next, 10)] : definition.items;
} else if (definition.type === 'object') {
(0, _invariant["default"])(definition.properties, 'Field properties not found in schema: "%s"', name);
definition = definition.properties[next];
} else {
var _filter$map = ['allOf', 'anyOf', 'oneOf'].filter(function (key) {
return definition[key];
}).map(function (key) {
return definition[key].find(function (_ref2) {
var _ref2$properties = _ref2.properties,
properties = _ref2$properties === void 0 ? {} : _ref2$properties;
return properties[next];
return [];
};
JSONSchemaBridge.prototype.getField = function (name) {
var _this = this;
return joinName_1["default"](null, name).reduce(function (definition, next, nextIndex, array) {
var previous = joinName_1["default"](array.slice(0, nextIndex));
var isRequired = (definition.required ||
(_this._compiledSchema[previous] || {}).required ||
[]).includes(next);
var _key = joinName_1["default"](previous, next);
var _definition = _this._compiledSchema[_key] || {};
if (next === '$' || next === '' + parseInt(next, 10)) {
invariant_1["default"](definition.type === 'array', 'Field not found in schema: "%s"', name);
definition = Array.isArray(definition.items)
? definition.items[parseInt(next, 10)]
: definition.items;
}
else if (definition.type === 'object') {
invariant_1["default"](definition.properties, 'Field properties not found in schema: "%s"', name);
definition = definition.properties[next];
}
else {
var _a = [
'allOf',
'anyOf',
'oneOf'
]
.filter(function (key) { return definition[key]; })
.map(function (key) {
return definition[key].find(function (_a) {
var _b = _a.properties, properties = _b === void 0 ? {} : _b;
return properties[next];
});
})[0], _b = (_a === void 0 ? {} : _a).properties, combinedDefinition = _b === void 0 ? {} : _b;
definition = combinedDefinition[next];
}
invariant_1["default"](definition, 'Field not found in schema: "%s"', name);
if (definition.$ref) {
definition = resolveRef(definition.$ref, _this.schema);
}
['allOf', 'anyOf', 'oneOf']
.filter(function (key) { return definition[key]; })
.forEach(function (key) {
_definition[key] = definition[key].map(function (def) {
return def.$ref ? resolveRef(def.$ref, _this.schema) : def;
});
});
}),
_filter$map2 = (0, _slicedToArray2["default"])(_filter$map, 1),
_filter$map2$ = _filter$map2[0];
_filter$map2$ = _filter$map2$ === void 0 ? {} : _filter$map2$;
var _filter$map2$$propert = _filter$map2$.properties,
combinedDefinition = _filter$map2$$propert === void 0 ? {} : _filter$map2$$propert;
definition = combinedDefinition[next];
// Naive computation of combined type, properties and required
if (['allOf', 'anyOf', 'oneOf'].filter(function (key) { return definition[key]; }).length) {
_definition.type = ([]
.concat(_definition.allOf, _definition.anyOf, _definition.oneOf)
.filter(function (def) { return def; })
.find(function (def) { return def.type; }) || {}).type;
var _c = []
.concat(_definition.allOf, _definition.anyOf, _definition.oneOf)
.filter(function (def) { return def; })
.reduce(function (_a, _b) {
var _properties = _a[0], _required = _a[1];
var properties = _b.properties, required = _b.required;
return [
Object.assign(_properties, properties),
_required.concat(required)
];
}, [{}, []]), properties = _c[0], required = _c[1];
_definition.properties = properties;
_definition.required = required;
}
_this._compiledSchema[_key] = Object.assign(_definition, { isRequired: isRequired });
return definition;
}, this.schema);
};
JSONSchemaBridge.prototype.getInitialValue = function (name, props) {
if (props === void 0) { props = {}; }
var _a = this.getField(name), _default = _a["default"], _type = _a.type;
var _b = this._compiledSchema[name], _c = _b["default"], defaultValue = _c === void 0 ? _default !== undefined
? _default
: get_1["default"](this.schema["default"], name) : _c, _d = _b.type, type = _d === void 0 ? _type : _d;
if (defaultValue !== undefined)
return cloneDeep_1["default"](defaultValue);
if (type === 'array') {
var item = this.getInitialValue(joinName_1["default"](name, '0'));
var items = props.initialCount || 0;
return Array(items).fill(item);
}
(0, _invariant["default"])(definition, 'Field not found in schema: "%s"', name);
if (definition.$ref) {
definition = resolveRef(definition.$ref, _this2.schema);
if (type === 'object')
return {};
return undefined;
};
JSONSchemaBridge.prototype.getProps = function (name, props) {
if (props === void 0) { props = {}; }
var _a = this.getField(name), uniforms = _a.uniforms, field = tslib_1.__rest(_a, ["uniforms"]);
var _b = omit_1["default"](tslib_1.__assign({}, field, uniforms, this._compiledSchema[name]), ['default', 'format', 'type']), enum_ = _b["enum"], isRequired = _b.isRequired, title = _b.title, ready = tslib_1.__rest(_b, ["enum", "isRequired", "title"]);
if (enum_)
ready.allowedValues = enum_;
if (field.type === 'number')
ready.decimal = true;
if (uniforms && uniforms.type !== undefined)
ready.type = uniforms.type;
if (ready.required === undefined)
ready.required = isRequired;
ready.label = extractValue(ready.label, title, toHumanLabel(joinName_1["default"](null, name).slice(-1)[0]));
var options = props.options || ready.options;
if (options) {
if (!Array.isArray(options)) {
ready.transform = function (value) { return options[value]; };
ready.allowedValues = Object.keys(options);
}
else {
ready.transform = function (value) {
return options.find(function (option) { return option.value === value; }).label;
};
ready.allowedValues = options.map(function (option) { return option.value; });
}
}
['allOf', 'anyOf', 'oneOf'].filter(function (key) {
return definition[key];
}).forEach(function (key) {
_definition[key] = definition[key].map(function (def) {
return def.$ref ? resolveRef(def.$ref, _this2.schema) : def;
});
}); // Naive computation of combined type, properties and required
if (['allOf', 'anyOf', 'oneOf'].filter(function (key) {
return definition[key];
}).length) {
_definition.type = ([].concat(_definition.allOf, _definition.anyOf, _definition.oneOf).filter(function (def) {
return def;
}).find(function (def) {
return def.type;
}) || {}).type;
var _concat$filter$reduce = [].concat(_definition.allOf, _definition.anyOf, _definition.oneOf).filter(function (def) {
return def;
}).reduce(function (_ref3, _ref4) {
var _ref5 = (0, _slicedToArray2["default"])(_ref3, 2),
_properties = _ref5[0],
_required = _ref5[1];
var properties = _ref4.properties,
required = _ref4.required;
return [Object.assign(_properties, properties), _required.concat(required)];
}, [{}, []]),
_concat$filter$reduce2 = (0, _slicedToArray2["default"])(_concat$filter$reduce, 2),
properties = _concat$filter$reduce2[0],
required = _concat$filter$reduce2[1];
_definition.properties = properties;
_definition.required = required;
return ready;
};
JSONSchemaBridge.prototype.getSubfields = function (name) {
if (!name) {
if (this.schema.properties) {
return Object.keys(this.schema.properties);
}
return [];
}
_this2._compiledSchema[_key] = Object.assign(_definition, {
isRequired: isRequired
});
return definition;
}, this.schema);
}
}, {
key: "getInitialValue",
value: function getInitialValue(name) {
var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _this$getField = this.getField(name),
_default = _this$getField["default"],
_type = _this$getField.type;
var _this$_compiledSchema = this._compiledSchema[name],
_this$_compiledSchema2 = _this$_compiledSchema["default"],
defaultValue = _this$_compiledSchema2 === void 0 ? _default !== undefined ? _default : (0, _get["default"])(this.schema["default"], name) : _this$_compiledSchema2,
_this$_compiledSchema3 = _this$_compiledSchema.type,
type = _this$_compiledSchema3 === void 0 ? _type : _this$_compiledSchema3;
if (defaultValue !== undefined) return (0, _cloneDeep["default"])(defaultValue);
if (type === 'array') {
var item = this.getInitialValue((0, _joinName["default"])(name, '0'));
var items = props.initialCount || 0;
return Array(items).fill(item);
}
if (type === 'object') return {};
return undefined;
}
}, {
key: "getProps",
value: function getProps(name) {
var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _this$getField2 = this.getField(name),
uniforms = _this$getField2.uniforms,
field = (0, _objectWithoutProperties2["default"])(_this$getField2, ["uniforms"]);
var _omit = (0, _omit2["default"])(_objectSpread({}, field, {}, uniforms, {}, this._compiledSchema[name]), ['default', 'format', 'type']),
enum_ = _omit["enum"],
isRequired = _omit.isRequired,
title = _omit.title,
ready = (0, _objectWithoutProperties2["default"])(_omit, ["enum", "isRequired", "title"]);
if (enum_) ready.allowedValues = enum_;
if (field.type === 'number') ready.decimal = true;
if (uniforms && uniforms.type !== undefined) ready.type = uniforms.type;
if (ready.required === undefined) ready.required = isRequired;
ready.label = extractValue(ready.label, title, toHumanLabel((0, _joinName["default"])(null, name).slice(-1)[0]));
var options = props.options || ready.options;
if (options) {
if (!Array.isArray(options)) {
ready.transform = function (value) {
return options[value];
};
ready.allowedValues = Object.keys(options);
} else {
ready.transform = function (value) {
return options.find(function (option) {
return option.value === value;
}).label;
};
ready.allowedValues = options.map(function (option) {
return option.value;
});
var _a = this.getField(name), _type = _a.type, _properties = _a.properties;
var _b = this._compiledSchema[name], _c = _b.type, fieldType = _c === void 0 ? _type : _c, _d = _b.properties, fieldProperties = _d === void 0 ? _properties : _d;
if (fieldType === 'object') {
return Object.keys(fieldProperties);
}
}
return ready;
}
}, {
key: "getSubfields",
value: function getSubfields(name) {
if (!name) {
if (this.schema.properties) {
return Object.keys(this.schema.properties);
}
return [];
}
var _this$getField3 = this.getField(name),
_type = _this$getField3.type,
_properties = _this$getField3.properties;
var _this$_compiledSchema4 = this._compiledSchema[name],
_this$_compiledSchema5 = _this$_compiledSchema4.type,
fieldType = _this$_compiledSchema5 === void 0 ? _type : _this$_compiledSchema5,
_this$_compiledSchema6 = _this$_compiledSchema4.properties,
fieldProperties = _this$_compiledSchema6 === void 0 ? _properties : _this$_compiledSchema6;
if (fieldType === 'object') {
return Object.keys(fieldProperties);
}
return [];
}
}, {
key: "getType",
value: function getType(name) {
var _this$getField4 = this.getField(name),
_type = _this$getField4.type,
fieldFormat = _this$getField4.format;
var _this$_compiledSchema7 = this._compiledSchema[name].type,
fieldType = _this$_compiledSchema7 === void 0 ? _type : _this$_compiledSchema7;
if (fieldFormat === 'date-time') return Date;
if (fieldType === 'string') return String;
if (fieldType === 'number') return Number;
if (fieldType === 'integer') return Number;
if (fieldType === 'object') return Object;
if (fieldType === 'array') return Array;
if (fieldType === 'boolean') return Boolean;
(0, _invariant["default"])(fieldType !== 'null', 'Field "%s" can not be represented as a type null', name);
return fieldType;
}
}, {
key: "getValidator",
value: function getValidator() {
return this.validator;
}
}], [{
key: "check",
value: function check() {
return false;
}
}]);
return JSONSchemaBridge;
}(_Bridge2["default"]);
exports["default"] = JSONSchemaBridge;
};
JSONSchemaBridge.prototype.getType = function (name) {
var _a = this.getField(name), _type = _a.type, fieldFormat = _a.format;
var _b = this._compiledSchema[name].type, fieldType = _b === void 0 ? _type : _b;
if (fieldFormat === 'date-time')
return Date;
if (fieldType === 'string')
return String;
if (fieldType === 'number')
return Number;
if (fieldType === 'integer')
return Number;
if (fieldType === 'object')
return Object;
if (fieldType === 'array')
return Array;
if (fieldType === 'boolean')
return Boolean;
invariant_1["default"](fieldType !== 'null', 'Field "%s" can not be represented as a type null', name);
return fieldType;
};
JSONSchemaBridge.prototype.getValidator = function () {
return this.validator;
};
return JSONSchemaBridge;
}(Bridge_1["default"]));
exports["default"] = JSONSchemaBridge;
{
"name": "uniforms-bridge-json-schema",
"version": "2.4.1",
"version": "2.5.0",
"license": "MIT",

@@ -18,12 +18,14 @@ "main": "index.js",

"files": [
"*.d.ts",
"*.js",
"src/"
"src/*.ts",
"src/*.tsx"
],
"dependencies": {
"@babel/runtime": "^7.0.0",
"invariant": "^2.0.0",
"lodash": "^4.0.0",
"uniforms": "^2.4.1"
"tslib": "^1.0.0",
"uniforms": "^2.5.0"
},
"gitHead": "8da4f0cc555bb66c5c55479fb24c64ca0ed37298"
"gitHead": "9faf28364bc7407d056b84d9362ac01bd9823436"
}
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _createSchemaBridge = _interopRequireDefault(require("uniforms/createSchemaBridge"));
var _JSONSchemaBridge = _interopRequireDefault(require("./JSONSchemaBridge"));
exports.__esModule = true;
var tslib_1 = require("tslib");
var createSchemaBridge_1 = tslib_1.__importDefault(require("uniforms/createSchemaBridge"));
var JSONSchemaBridge_1 = tslib_1.__importDefault(require("./JSONSchemaBridge"));
// Register bridge.
_createSchemaBridge["default"].register(_JSONSchemaBridge["default"]);
createSchemaBridge_1["default"].register(JSONSchemaBridge_1["default"]);