uniforms-bridge-simple-schema-2
Advanced tools
Comparing version 3.5.1 to 3.5.2
@@ -11,3 +11,3 @@ import SimpleSchema from 'simpl-schema'; | ||
getInitialValue(name: string, props?: Record<string, any>): any; | ||
getProps(name: string, props?: Record<string, any>): any; | ||
getProps(name: string, fieldProps?: Record<string, any>): any; | ||
getSubfields(name?: string): any[]; | ||
@@ -14,0 +14,0 @@ getType(name: string): any; |
@@ -9,2 +9,3 @@ "use strict"; | ||
var uniforms_1 = require("uniforms"); | ||
var propsToRemove = ['optional', 'type', 'uniforms']; | ||
var SimpleSchema2Bridge = /** @class */ (function (_super) { | ||
@@ -22,5 +23,7 @@ tslib_1.__extends(SimpleSchema2Bridge, _super); | ||
SimpleSchema2Bridge.prototype.getError = function (name, error) { | ||
var _a, _b; | ||
// FIXME: Correct type for `error`. | ||
return ((_b = (_a = error === null || error === void 0 ? void 0 : error.details) === null || _a === void 0 ? void 0 : _a.find) === null || _b === void 0 ? void 0 : _b.call(_a, function (error) { return error.name === name; })) || null; | ||
var details = error === null || error === void 0 ? void 0 : error.details; | ||
if (!Array.isArray(details)) { | ||
return null; | ||
} | ||
return details.find(function (error) { return error.name === name; }) || null; | ||
}; | ||
@@ -56,3 +59,3 @@ SimpleSchema2Bridge.prototype.getErrorMessage = function (name, error) { | ||
catch (_) { | ||
/* ignore it */ | ||
// It's fine. | ||
} | ||
@@ -63,7 +66,6 @@ } | ||
SimpleSchema2Bridge.prototype.getInitialValue = function (name, props) { | ||
if (props === void 0) { props = {}; } | ||
var field = this.getField(name); | ||
if (field.type === Array) { | ||
var item_1 = this.getInitialValue(uniforms_1.joinName(name, '0')); | ||
var items = Math.max(props.initialCount || 0, field.minCount || 0); | ||
var items = Math.max((props === null || props === void 0 ? void 0 : props.initialCount) || 0, field.minCount || 0); | ||
return Array.from({ length: items }, function () { return item_1; }); | ||
@@ -77,46 +79,51 @@ } | ||
// eslint-disable-next-line complexity | ||
SimpleSchema2Bridge.prototype.getProps = function (name, props) { | ||
if (props === void 0) { props = {}; } | ||
var _a = this.getField(name), optional = _a.optional, type = _a.type, uniforms = _a.uniforms, contextField = tslib_1.__rest(_a, ["optional", "type", "uniforms"]); | ||
var field = tslib_1.__assign(tslib_1.__assign({}, contextField), { required: !optional }); | ||
if (uniforms) { | ||
if (typeof uniforms === 'string' || typeof uniforms === 'function') { | ||
field = tslib_1.__assign(tslib_1.__assign({}, field), { component: uniforms }); | ||
SimpleSchema2Bridge.prototype.getProps = function (name, fieldProps) { | ||
var props = Object.assign({}, this.getField(name)); | ||
props.required = !props.optional; | ||
if (typeof props.uniforms === 'function' || | ||
typeof props.uniforms === 'string') { | ||
props.component = props.uniforms; | ||
} | ||
else { | ||
Object.assign(props, props.uniforms); | ||
} | ||
if (props.type === Number) { | ||
props.decimal = true; | ||
} | ||
var options = (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.options) || props.options; | ||
if (options) { | ||
if (typeof options === 'function') { | ||
options = 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; | ||
}; | ||
} | ||
else { | ||
field = tslib_1.__assign(tslib_1.__assign({}, field), uniforms); | ||
props.allowedValues = Object.keys(options); | ||
props.transform = function (value) { return options[value]; }; | ||
} | ||
} | ||
if (type === Array) { | ||
else if (props.type === Array) { | ||
try { | ||
var itemProps = this.getProps(name + ".$", props); | ||
if (itemProps.allowedValues && !props.allowedValues) { | ||
field.allowedValues = itemProps.allowedValues; | ||
var itemProps = this.getProps(name + ".$", fieldProps); | ||
if (itemProps.allowedValues && !(fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.allowedValues)) { | ||
props.allowedValues = itemProps.allowedValues; | ||
} | ||
if (itemProps.transform && !props.transform) { | ||
field.transform = itemProps.transform; | ||
if (itemProps.transform && !(fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.transform)) { | ||
props.transform = itemProps.transform; | ||
} | ||
} | ||
catch (_) { | ||
/* ignore it */ | ||
// It's fine. | ||
} | ||
} | ||
else if (type === Number) { | ||
field = tslib_1.__assign(tslib_1.__assign({}, field), { decimal: true }); | ||
} | ||
var options = props.options || field.options; | ||
if (options) { | ||
if (typeof options === 'function') { | ||
options = options(); | ||
propsToRemove.forEach(function (key) { | ||
if (key in props) { | ||
delete props[key]; | ||
} | ||
if (!Array.isArray(options)) { | ||
field = tslib_1.__assign(tslib_1.__assign({}, field), { transform: function (value) { return options[value]; }, allowedValues: Object.keys(options) }); | ||
} | ||
else { | ||
field = tslib_1.__assign(tslib_1.__assign({}, field), { transform: function (value) { | ||
return options.find(function (option) { return option.value === value; }).label; | ||
}, allowedValues: options.map(function (option) { return option.value; }) }); | ||
} | ||
} | ||
return field; | ||
}); | ||
return props; | ||
}; | ||
@@ -123,0 +130,0 @@ SimpleSchema2Bridge.prototype.getSubfields = function (name) { |
@@ -11,3 +11,3 @@ import SimpleSchema from 'simpl-schema'; | ||
getInitialValue(name: string, props?: Record<string, any>): any; | ||
getProps(name: string, props?: Record<string, any>): any; | ||
getProps(name: string, fieldProps?: Record<string, any>): any; | ||
getSubfields(name?: string): any[]; | ||
@@ -14,0 +14,0 @@ getType(name: string): any; |
@@ -1,2 +0,1 @@ | ||
import { __rest } from "tslib"; | ||
import invariant from 'invariant'; | ||
@@ -7,2 +6,3 @@ import cloneDeep from 'lodash/cloneDeep'; | ||
import { Bridge, joinName } from 'uniforms'; | ||
const propsToRemove = ['optional', 'type', 'uniforms']; | ||
export default class SimpleSchema2Bridge extends Bridge { | ||
@@ -18,5 +18,7 @@ constructor(schema) { | ||
getError(name, error) { | ||
var _a, _b; | ||
// FIXME: Correct type for `error`. | ||
return ((_b = (_a = error === null || error === void 0 ? void 0 : error.details) === null || _a === void 0 ? void 0 : _a.find) === null || _b === void 0 ? void 0 : _b.call(_a, (error) => error.name === name)) || null; | ||
const details = error === null || error === void 0 ? void 0 : error.details; | ||
if (!Array.isArray(details)) { | ||
return null; | ||
} | ||
return details.find(error => error.name === name) || null; | ||
} | ||
@@ -51,3 +53,3 @@ getErrorMessage(name, error) { | ||
catch (_) { | ||
/* ignore it */ | ||
// It's fine. | ||
} | ||
@@ -57,7 +59,7 @@ } | ||
} | ||
getInitialValue(name, props = {}) { | ||
getInitialValue(name, props) { | ||
const field = this.getField(name); | ||
if (field.type === Array) { | ||
const item = this.getInitialValue(joinName(name, '0')); | ||
const items = Math.max(props.initialCount || 0, field.minCount || 0); | ||
const items = Math.max((props === null || props === void 0 ? void 0 : props.initialCount) || 0, field.minCount || 0); | ||
return Array.from({ length: items }, () => item); | ||
@@ -71,43 +73,49 @@ } | ||
// eslint-disable-next-line complexity | ||
getProps(name, props = {}) { | ||
const _a = this.getField(name), { optional, type, uniforms } = _a, contextField = __rest(_a, ["optional", "type", "uniforms"]); | ||
let field = Object.assign(Object.assign({}, contextField), { required: !optional }); | ||
if (uniforms) { | ||
if (typeof uniforms === 'string' || typeof uniforms === 'function') { | ||
field = Object.assign(Object.assign({}, field), { component: uniforms }); | ||
getProps(name, fieldProps) { | ||
const props = Object.assign({}, this.getField(name)); | ||
props.required = !props.optional; | ||
if (typeof props.uniforms === 'function' || | ||
typeof props.uniforms === 'string') { | ||
props.component = props.uniforms; | ||
} | ||
else { | ||
Object.assign(props, props.uniforms); | ||
} | ||
if (props.type === Number) { | ||
props.decimal = true; | ||
} | ||
let options = (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.options) || props.options; | ||
if (options) { | ||
if (typeof options === 'function') { | ||
options = options(); | ||
} | ||
if (Array.isArray(options)) { | ||
props.allowedValues = options.map(option => option.value); | ||
props.transform = (value) => options.find(option => option.value === value).label; | ||
} | ||
else { | ||
field = Object.assign(Object.assign({}, field), uniforms); | ||
props.allowedValues = Object.keys(options); | ||
props.transform = (value) => options[value]; | ||
} | ||
} | ||
if (type === Array) { | ||
else if (props.type === Array) { | ||
try { | ||
const itemProps = this.getProps(`${name}.$`, props); | ||
if (itemProps.allowedValues && !props.allowedValues) { | ||
field.allowedValues = itemProps.allowedValues; | ||
const itemProps = this.getProps(`${name}.$`, fieldProps); | ||
if (itemProps.allowedValues && !(fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.allowedValues)) { | ||
props.allowedValues = itemProps.allowedValues; | ||
} | ||
if (itemProps.transform && !props.transform) { | ||
field.transform = itemProps.transform; | ||
if (itemProps.transform && !(fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.transform)) { | ||
props.transform = itemProps.transform; | ||
} | ||
} | ||
catch (_) { | ||
/* ignore it */ | ||
// It's fine. | ||
} | ||
} | ||
else if (type === Number) { | ||
field = Object.assign(Object.assign({}, field), { decimal: true }); | ||
} | ||
let options = props.options || field.options; | ||
if (options) { | ||
if (typeof options === 'function') { | ||
options = options(); | ||
propsToRemove.forEach(key => { | ||
if (key in props) { | ||
delete props[key]; | ||
} | ||
if (!Array.isArray(options)) { | ||
field = Object.assign(Object.assign({}, field), { transform: (value) => options[value], allowedValues: Object.keys(options) }); | ||
} | ||
else { | ||
field = Object.assign(Object.assign({}, field), { transform: (value) => options.find(option => option.value === value).label, allowedValues: options.map(option => option.value) }); | ||
} | ||
} | ||
return field; | ||
}); | ||
return props; | ||
} | ||
@@ -114,0 +122,0 @@ getSubfields(name) { |
{ | ||
"name": "uniforms-bridge-simple-schema-2", | ||
"version": "3.5.1", | ||
"version": "3.5.2", | ||
"license": "MIT", | ||
"main": "es5/index.js", | ||
"module": "es6/index.js", | ||
"module": "esm/index.js", | ||
"sideEffects": [ | ||
@@ -40,5 +40,5 @@ "es5/index.js", | ||
"tslib": "^2.2.0", | ||
"uniforms": "^3.5.1" | ||
"uniforms": "^3.5.2" | ||
}, | ||
"gitHead": "8e813322a5bb8149e1912cd652f212135472ec76" | ||
"gitHead": "460421d945495f2c404a23b7fd129521343d23e8" | ||
} |
@@ -7,2 +7,4 @@ import invariant from 'invariant'; | ||
const propsToRemove = ['optional', 'type', 'uniforms']; | ||
export default class SimpleSchema2Bridge extends Bridge { | ||
@@ -19,4 +21,8 @@ constructor(public schema: SimpleSchema) { | ||
getError(name: string, error: any) { | ||
// FIXME: Correct type for `error`. | ||
return error?.details?.find?.((error: any) => error.name === name) || null; | ||
const details = error?.details; | ||
if (!Array.isArray(details)) { | ||
return null; | ||
} | ||
return details.find(error => error.name === name) || null; | ||
} | ||
@@ -61,3 +67,3 @@ | ||
} catch (_) { | ||
/* ignore it */ | ||
// It's fine. | ||
} | ||
@@ -69,3 +75,3 @@ } | ||
getInitialValue(name: string, props: Record<string, any> = {}): any { | ||
getInitialValue(name: string, props?: Record<string, any>): any { | ||
const field = this.getField(name); | ||
@@ -75,4 +81,3 @@ | ||
const item = this.getInitialValue(joinName(name, '0')); | ||
const items = Math.max(props.initialCount || 0, field.minCount || 0); | ||
const items = Math.max(props?.initialCount || 0, field.minCount || 0); | ||
return Array.from({ length: items }, () => item); | ||
@@ -89,32 +94,23 @@ } | ||
// eslint-disable-next-line complexity | ||
getProps(name: string, props: Record<string, any> = {}) { | ||
const { optional, type, uniforms, ...contextField } = this.getField(name); | ||
let field = { ...contextField, required: !optional }; | ||
getProps(name: string, fieldProps?: Record<string, any>) { | ||
const props = Object.assign({}, this.getField(name)); | ||
props.required = !props.optional; | ||
if (uniforms) { | ||
if (typeof uniforms === 'string' || typeof uniforms === 'function') { | ||
field = { ...field, component: uniforms }; | ||
} else { | ||
field = { ...field, ...uniforms }; | ||
} | ||
if ( | ||
typeof props.uniforms === 'function' || | ||
typeof props.uniforms === 'string' | ||
) { | ||
props.component = props.uniforms; | ||
} else { | ||
Object.assign(props, props.uniforms); | ||
} | ||
if (type === Array) { | ||
try { | ||
const itemProps = this.getProps(`${name}.$`, props); | ||
if (itemProps.allowedValues && !props.allowedValues) { | ||
field.allowedValues = itemProps.allowedValues; | ||
} | ||
if (itemProps.transform && !props.transform) { | ||
field.transform = itemProps.transform; | ||
} | ||
} catch (_) { | ||
/* ignore it */ | ||
} | ||
} else if (type === Number) { | ||
field = { ...field, decimal: true }; | ||
if (props.type === Number) { | ||
props.decimal = true; | ||
} | ||
let options = props.options || field.options; | ||
type OptionDict = Record<string, string>; | ||
type OptionList = { label: string; value: unknown }[]; | ||
type Options = OptionDict | OptionList | (() => OptionDict | OptionList); | ||
let options: Options = fieldProps?.options || props.options; | ||
if (options) { | ||
@@ -125,19 +121,32 @@ if (typeof options === 'function') { | ||
if (!Array.isArray(options)) { | ||
field = { | ||
...field, | ||
transform: (value: any) => options[value], | ||
allowedValues: Object.keys(options), | ||
}; | ||
if (Array.isArray(options)) { | ||
props.allowedValues = options.map(option => option.value); | ||
props.transform = (value: unknown) => | ||
(options as OptionList).find(option => option.value === value)!.label; | ||
} else { | ||
field = { | ||
...field, | ||
transform: (value: any) => | ||
(options as any[]).find(option => option.value === value).label, | ||
allowedValues: options.map(option => option.value), | ||
}; | ||
props.allowedValues = Object.keys(options); | ||
props.transform = (value: string) => (options as OptionDict)[value]; | ||
} | ||
} else if (props.type === Array) { | ||
try { | ||
const itemProps = this.getProps(`${name}.$`, fieldProps); | ||
if (itemProps.allowedValues && !fieldProps?.allowedValues) { | ||
props.allowedValues = itemProps.allowedValues; | ||
} | ||
if (itemProps.transform && !fieldProps?.transform) { | ||
props.transform = itemProps.transform; | ||
} | ||
} catch (_) { | ||
// It's fine. | ||
} | ||
} | ||
return field; | ||
propsToRemove.forEach(key => { | ||
if (key in props) { | ||
delete props[key]; | ||
} | ||
}); | ||
return props; | ||
} | ||
@@ -144,0 +153,0 @@ |
24732
591
Updateduniforms@^3.5.2