uniforms-bridge-json-schema
Advanced tools
Comparing version 3.6.0 to 3.6.1
@@ -1,2 +0,2 @@ | ||
import { __extends, __rest } from "tslib"; | ||
import { __rest } from "tslib"; | ||
import invariant from 'invariant'; | ||
@@ -15,6 +15,6 @@ import cloneDeep from 'lodash/cloneDeep'; | ||
invariant(reference.startsWith('#'), 'Reference is not an internal reference, and only such are allowed: "%s"', reference); | ||
var resolvedReference = reference | ||
const resolvedReference = reference | ||
.split('/') | ||
.filter(function (part) { return part && part !== '#'; }) | ||
.reduce(function (definition, next) { return definition[next]; }, schema); | ||
.filter(part => part && part !== '#') | ||
.reduce((definition, next) => definition[next], schema); | ||
invariant(resolvedReference, 'Reference not found in schema: "%s"', reference); | ||
@@ -27,7 +27,7 @@ return resolvedReference; | ||
} | ||
var $ref = partial.$ref, partialWithoutRef = __rest(partial, ["$ref"]); | ||
const { $ref } = partial, partialWithoutRef = __rest(partial, ["$ref"]); | ||
return resolveRefIfNeeded(Object.assign({}, partialWithoutRef, resolveRef($ref, schema)), schema); | ||
} | ||
var partialNames = ['allOf', 'anyOf', 'oneOf']; | ||
var propsToRemove = [ | ||
const partialNames = ['allOf', 'anyOf', 'oneOf']; | ||
const propsToRemove = [ | ||
'default', | ||
@@ -40,3 +40,3 @@ 'enum', | ||
]; | ||
var propsToRename = [ | ||
const propsToRename = [ | ||
['maxItems', 'maxCount'], | ||
@@ -57,26 +57,24 @@ ['maximum', 'max'], | ||
} | ||
var JSONSchemaBridge = /** @class */ (function (_super) { | ||
__extends(JSONSchemaBridge, _super); | ||
function JSONSchemaBridge(schema, validator) { | ||
var _this = _super.call(this) || this; | ||
_this.validator = validator; | ||
_this.schema = resolveRefIfNeeded(schema, schema); | ||
_this._compiledSchema = { '': _this.schema }; | ||
export default class JSONSchemaBridge extends Bridge { | ||
constructor(schema, validator) { | ||
super(); | ||
this.validator = validator; | ||
this.schema = resolveRefIfNeeded(schema, schema); | ||
this._compiledSchema = { '': this.schema }; | ||
// Memoize for performance and referential equality. | ||
_this.getField = memoize(_this.getField.bind(_this)); | ||
_this.getSubfields = memoize(_this.getSubfields.bind(_this)); | ||
_this.getType = memoize(_this.getType.bind(_this)); | ||
return _this; | ||
this.getField = memoize(this.getField.bind(this)); | ||
this.getSubfields = memoize(this.getSubfields.bind(this)); | ||
this.getType = memoize(this.getType.bind(this)); | ||
} | ||
JSONSchemaBridge.prototype.getError = function (name, error) { | ||
var details = error === null || error === void 0 ? void 0 : error.details; | ||
getError(name, error) { | ||
const details = error === null || error === void 0 ? void 0 : error.details; | ||
if (!Array.isArray(details)) { | ||
return null; | ||
} | ||
var nameParts = joinName(null, name); | ||
var rootName = joinName(nameParts.slice(0, -1)); | ||
var baseName = nameParts[nameParts.length - 1]; | ||
var scopedError = details.find(function (error) { | ||
const nameParts = joinName(null, name); | ||
const rootName = joinName(nameParts.slice(0, -1)); | ||
const baseName = nameParts[nameParts.length - 1]; | ||
const scopedError = details.find(error => { | ||
var _a; | ||
var path = pathToName((_a = error.instancePath) !== null && _a !== void 0 ? _a : error.dataPath); | ||
const path = pathToName((_a = error.instancePath) !== null && _a !== void 0 ? _a : error.dataPath); | ||
return (name === path || | ||
@@ -86,26 +84,25 @@ (rootName === path && baseName === error.params.missingProperty)); | ||
return scopedError || null; | ||
}; | ||
JSONSchemaBridge.prototype.getErrorMessage = function (name, error) { | ||
var scopedError = this.getError(name, error); | ||
} | ||
getErrorMessage(name, error) { | ||
const scopedError = this.getError(name, error); | ||
return (scopedError === null || scopedError === void 0 ? void 0 : scopedError.message) || ''; | ||
}; | ||
JSONSchemaBridge.prototype.getErrorMessages = function (error) { | ||
} | ||
getErrorMessages(error) { | ||
if (!error) { | ||
return []; | ||
} | ||
var details = error.details; | ||
const { details } = error; | ||
return Array.isArray(details) | ||
? details.map(function (error) { return error.message; }) | ||
? details.map(error => error.message) | ||
: [error.message || error]; | ||
}; | ||
JSONSchemaBridge.prototype.getField = function (name) { | ||
var _this = this; | ||
return joinName(null, name).reduce(function (definition, next, index, array) { | ||
} | ||
getField(name) { | ||
return joinName(null, name).reduce((definition, next, index, array) => { | ||
var _a, _b, _c; | ||
var _d; | ||
var prevName = joinName(array.slice(0, index)); | ||
var nextName = joinName(prevName, next); | ||
var definitionCache = ((_a = (_d = _this._compiledSchema)[nextName]) !== null && _a !== void 0 ? _a : (_d[nextName] = {})); | ||
const prevName = joinName(array.slice(0, index)); | ||
const nextName = joinName(prevName, next); | ||
const definitionCache = ((_a = (_d = this._compiledSchema)[nextName]) !== null && _a !== void 0 ? _a : (_d[nextName] = {})); | ||
definitionCache.isRequired = !!(((_b = definition.required) === null || _b === void 0 ? void 0 : _b.includes(next)) || | ||
((_c = _this._compiledSchema[prevName].required) === null || _c === void 0 ? void 0 : _c.includes(next))); | ||
((_c = this._compiledSchema[prevName].required) === null || _c === void 0 ? void 0 : _c.includes(next))); | ||
if (next === '$' || next === '' + parseInt(next, 10)) { | ||
@@ -124,11 +121,11 @@ fieldInvariant(name, definition.type === 'array'); | ||
else { | ||
var nextFound_1 = false; | ||
partialNames.forEach(function (partialName) { | ||
let nextFound = false; | ||
partialNames.forEach(partialName => { | ||
var _a; | ||
(_a = definition[partialName]) === null || _a === void 0 ? void 0 : _a.forEach(function (partialElement) { | ||
if (!nextFound_1) { | ||
partialElement = resolveRefIfNeeded(partialElement, _this.schema); | ||
(_a = definition[partialName]) === null || _a === void 0 ? void 0 : _a.forEach((partialElement) => { | ||
if (!nextFound) { | ||
partialElement = resolveRefIfNeeded(partialElement, this.schema); | ||
if (next in partialElement.properties) { | ||
definition = partialElement.properties[next]; | ||
nextFound_1 = true; | ||
nextFound = true; | ||
} | ||
@@ -138,16 +135,16 @@ } | ||
}); | ||
fieldInvariant(name, nextFound_1); | ||
fieldInvariant(name, nextFound); | ||
} | ||
definition = resolveRefIfNeeded(definition, _this.schema); | ||
definition = resolveRefIfNeeded(definition, this.schema); | ||
// Naive computation of combined type, properties and required. | ||
var required = definition.required ? definition.required.slice() : []; | ||
var properties = definition.properties | ||
const required = definition.required ? definition.required.slice() : []; | ||
const properties = definition.properties | ||
? Object.assign({}, definition.properties) | ||
: {}; | ||
partialNames.forEach(function (partialName) { | ||
partialNames.forEach(partialName => { | ||
var _a; | ||
(_a = definition[partialName]) === null || _a === void 0 ? void 0 : _a.forEach(function (partial) { | ||
partial = resolveRefIfNeeded(partial, _this.schema); | ||
(_a = definition[partialName]) === null || _a === void 0 ? void 0 : _a.forEach((partial) => { | ||
partial = resolveRefIfNeeded(partial, this.schema); | ||
if (partial.required) { | ||
required.push.apply(required, partial.required); | ||
required.push(...partial.required); | ||
} | ||
@@ -168,7 +165,7 @@ Object.assign(properties, partial.properties); | ||
}, this.schema); | ||
}; | ||
JSONSchemaBridge.prototype.getInitialValue = function (name, props) { | ||
} | ||
getInitialValue(name, props) { | ||
var _a; | ||
var field = this.getField(name); | ||
var _b = this._compiledSchema[name], _c = _b.default, defaultValue = _c === void 0 ? (_a = field.default) !== null && _a !== void 0 ? _a : get(this.schema.default, name) : _c, _d = _b.type, type = _d === void 0 ? field.type : _d; | ||
const field = this.getField(name); | ||
const { default: defaultValue = (_a = field.default) !== null && _a !== void 0 ? _a : get(this.schema.default, name), type = field.type, } = this._compiledSchema[name]; | ||
if (defaultValue !== undefined) { | ||
@@ -178,5 +175,5 @@ return cloneDeep(defaultValue); | ||
if (type === 'array') { | ||
var item_1 = this.getInitialValue(joinName(name, '0')); | ||
var items = (props === null || props === void 0 ? void 0 : props.initialCount) || 0; | ||
return Array.from({ length: items }, function () { return item_1; }); | ||
const item = this.getInitialValue(joinName(name, '0')); | ||
const items = (props === null || props === void 0 ? void 0 : props.initialCount) || 0; | ||
return Array.from({ length: items }, () => item); | ||
} | ||
@@ -187,7 +184,7 @@ if (type === 'object') { | ||
return undefined; | ||
}; | ||
JSONSchemaBridge.prototype.getProps = function (name, fieldProps) { | ||
} | ||
getProps(name, fieldProps) { | ||
var _a, _b, _c; | ||
var field = this.getField(name); | ||
var props = Object.assign({}, field, field.uniforms, this._compiledSchema[name]); | ||
const field = this.getField(name); | ||
const props = Object.assign({}, field, field.uniforms, this._compiledSchema[name]); | ||
(_a = props.label) !== null && _a !== void 0 ? _a : (props.label = (_b = props.title) !== null && _b !== void 0 ? _b : upperFirst(lowerCase(joinName(null, name).slice(-1)[0]))); | ||
@@ -206,13 +203,11 @@ if (field.type === 'number') { | ||
} | ||
var options = (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.options) || props.options; | ||
const options = (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.options) || props.options; | ||
if (options) { | ||
if (Array.isArray(options)) { | ||
props.allowedValues = options.map(function (option) { return option.value; }); | ||
props.transform = function (value) { | ||
return options.find(function (option) { return option.value === value; }).label; | ||
}; | ||
props.allowedValues = options.map(option => option.value); | ||
props.transform = (value) => options.find(option => option.value === value).label; | ||
} | ||
else { | ||
props.allowedValues = Object.keys(options); | ||
props.transform = function (value) { return options[value]; }; | ||
props.transform = (value) => options[value]; | ||
} | ||
@@ -223,4 +218,3 @@ } | ||
} | ||
propsToRename.forEach(function (_a) { | ||
var key = _a[0], newKey = _a[1]; | ||
propsToRename.forEach(([key, newKey]) => { | ||
if (key in props) { | ||
@@ -231,3 +225,3 @@ props[newKey] = props[key]; | ||
}); | ||
propsToRemove.forEach(function (key) { | ||
propsToRemove.forEach(key => { | ||
if (key in props) { | ||
@@ -238,7 +232,6 @@ delete props[key]; | ||
return props; | ||
}; | ||
JSONSchemaBridge.prototype.getSubfields = function (name) { | ||
if (name === void 0) { name = ''; } | ||
var field = this.getField(name); | ||
var _a = this._compiledSchema[name], _b = _a.properties, properties = _b === void 0 ? field.properties : _b, _c = _a.type, type = _c === void 0 ? field.type : _c; | ||
} | ||
getSubfields(name = '') { | ||
const field = this.getField(name); | ||
const { properties = field.properties, type = field.type } = this._compiledSchema[name]; | ||
if (type === 'object' && properties) { | ||
@@ -248,6 +241,6 @@ return Object.keys(properties); | ||
return []; | ||
}; | ||
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; | ||
} | ||
getType(name) { | ||
const { type: _type, format: fieldFormat } = this.getField(name); | ||
const { type: fieldType = _type } = this._compiledSchema[name]; | ||
if (fieldFormat === 'date-time') { | ||
@@ -276,8 +269,6 @@ return Date; | ||
return fieldType; | ||
}; | ||
JSONSchemaBridge.prototype.getValidator = function () { | ||
} | ||
getValidator() { | ||
return this.validator; | ||
}; | ||
return JSONSchemaBridge; | ||
}(Bridge)); | ||
export default JSONSchemaBridge; | ||
} | ||
} |
{ | ||
"name": "uniforms-bridge-json-schema", | ||
"version": "3.6.0", | ||
"version": "3.6.1", | ||
"license": "MIT", | ||
"main": "./es5/index.js", | ||
"main": "./cjs/index.js", | ||
"module": "./esm/index.js", | ||
@@ -21,8 +21,6 @@ "sideEffects": false, | ||
"files": [ | ||
"es5/*.d.ts", | ||
"es5/*.js", | ||
"cjs/*.d.ts", | ||
"cjs/*.js", | ||
"esm/*.d.ts", | ||
"esm/*.js", | ||
"es6/*.d.ts", | ||
"es6/*.js", | ||
"src/*.ts", | ||
@@ -35,5 +33,5 @@ "src/*.tsx" | ||
"tslib": "^2.2.0", | ||
"uniforms": "^3.6.0" | ||
"uniforms": "^3.6.1" | ||
}, | ||
"gitHead": "8ec8ba7751b6ce4b205c49d2aca1ab460bd09e0a" | ||
"gitHead": "a36891aa6aa9bb10c40bf5709085fbe82ac4ae92" | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
35300
13
850
1
Updateduniforms@^3.6.1