UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

140 lines (139 loc) 5.49 kB
"use client"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import "core-js/modules/web.dom-collections.iterator.js"; import { useCallback, useContext, useRef } from 'react'; import pointer from '../utils/json-pointer'; import DataContext from '../DataContext/Context'; export const schemaParams = ['minLength', 'maxLength', 'pattern', 'description', 'min', 'max', 'multipleOf', 'exclusiveMinimum', 'exclusiveMaximum']; export default function GenerateSchema(props) { const { generateRef, filterData, log, children } = props || {}; const { fieldInternalsRef, valueInternalsRef, data, hasContext } = useContext(DataContext); const dataRef = useRef({}); dataRef.current = data; const generate = useCallback(() => { const schema = Object.entries((fieldInternalsRef === null || fieldInternalsRef === void 0 ? void 0 : fieldInternalsRef.current) || {}).reduce((acc, _ref) => { let [path, { props }] = _ref; if (path.startsWith('/')) { const objectKey = path.substring(1); const pathList = objectKey.split('/'); const slashCount = pathList.length; const type = props.valueType || 'string'; const propertyValue = { type }; for (const prop of schemaParams) { if (props[prop]) { propertyValue[prop] = props[prop]; } } if (slashCount > 1) { const nestedPath = ['']; for (const path of pathList) { nestedPath.push(path); const pathToSet = nestedPath.join('/properties/'); const isLast = nestedPath.length - 1 === pathList.length; const existingValue = pointer.has(acc, pathToSet) ? pointer.get(acc, pathToSet) : null; const pathValue = isLast ? propertyValue : existingValue; if (isLast) { if ((filterData === null || filterData === void 0 ? void 0 : filterData[pathToSet]) !== false) { pointer.set(acc, pathToSet, pathValue); } } else { const pathValue = _objectSpread({ type: 'object' }, existingValue); const required = []; if (props.required) { required.push(pathList.at(-1)); } if (existingValue !== null && existingValue !== void 0 && existingValue.required) { required.push(...existingValue.required); } if (required.length > 0) { pathValue.required = required; } if ((filterData === null || filterData === void 0 ? void 0 : filterData[pathToSet]) !== false) { pointer.set(acc, pathToSet, pathValue); } } } } else { if ((filterData === null || filterData === void 0 ? void 0 : filterData[path]) !== false) { pointer.set(acc.properties, path, propertyValue); } if (props.required) { acc.required.push(objectKey); } } } return acc; }, { type: 'object', properties: {}, required: [] }); const propsOfFields = Object.entries((fieldInternalsRef === null || fieldInternalsRef === void 0 ? void 0 : fieldInternalsRef.current) || {}).reduce((acc, _ref2) => { let [path, { props }] = _ref2; if (path.startsWith('/')) { const propertyValue = {}; for (const prop in props) { if (props[prop] !== undefined && typeof props[prop] !== 'function') { propertyValue[prop] = props[prop]; } } pointer.set(acc, path, propertyValue); } return acc; }, {}); const propsOfValues = Object.entries((valueInternalsRef === null || valueInternalsRef === void 0 ? void 0 : valueInternalsRef.current) || {}).reduce((acc, _ref3) => { let [path, { props }] = _ref3; if (path.startsWith('/')) { const propertyValue = {}; for (const prop in props) { if (props[prop] !== undefined && typeof props[prop] !== 'function') { propertyValue[prop] = props[prop]; } } pointer.set(acc, path, propertyValue); } return acc; }, {}); if (schema.required.length === 0) { delete schema.required; } return { schema, data: dataRef.current, propsOfFields, propsOfValues }; }, [fieldInternalsRef, filterData, valueInternalsRef]); if (hasContext) { if (log) { console.log(generate().schema); } if (generateRef) { generateRef.current = generate; } } return children; } //# sourceMappingURL=GenerateSchema.js.map