UNPKG

@schema-render/core-react

Version:

Through a set of simple JSON Schema, efficiently build a set of forms.

28 lines (27 loc) 910 B
import { dropRight, get } from "./tinyLodash"; import { isBoolean, isString } from "./checking"; import logger from "./logger"; // 执行 hidden、required、disabled 语句 export function performStatement({ statement, parentValue = {}, rootValue = {}, userCtx = {} }) { if (isBoolean(statement)) { return statement; } if (isString(statement)) { try { const fn = new Function('$', '$root', '$userCtx', `return ${statement}`); return !!fn(parentValue, rootValue, userCtx); } catch (e) { logger.warn(e); } } return false; } export function performStatementWithPath({ statement, path, rootValue, userCtx }) { const parentPath = dropRight(path); return performStatement({ statement, parentValue: parentPath.length ? get(rootValue, parentPath) : rootValue, rootValue, userCtx }); }