Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

always reuse AJV instance from store #1624

Merged
merged 3 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/angular-material/src/other/label.renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import {
OwnPropsOfRenderer,
RankedTester,
rankWith,
uiTypeIs
uiTypeIs,
getAjv
} from '@jsonforms/core';
import { Subscription } from 'rxjs';

Expand All @@ -46,7 +47,7 @@ const mapStateToProps = (
const visible =
ownProps.visible !== undefined
? ownProps.visible
: isVisible(ownProps.uischema, getData(state));
: isVisible(ownProps.uischema, getData(state), undefined, getAjv(state));

return {
visible
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/reducers/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ export const coreReducer = (
export const extractData = (state: JsonFormsCore) => get(state, 'data');
export const extractSchema = (state: JsonFormsCore) => get(state, 'schema');
export const extractUiSchema = (state: JsonFormsCore) => get(state, 'uischema');
export const extractAjv = (state: JsonFormsCore) => get(state, 'ajv');

export const errorsAt = (
instancePath: string,
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
coreReducer,
errorAt,
errorsAt,
extractAjv,
extractData,
extractRefParserOptions,
extractSchema,
Expand Down Expand Up @@ -63,6 +64,7 @@ import RefParser from 'json-schema-ref-parser';
import { cellReducer } from './cells';
import { configReducer } from './config';
import get from 'lodash/get';
import { Ajv } from 'ajv';

export {
rendererReducer,
Expand Down Expand Up @@ -98,6 +100,9 @@ export const getUiSchema = (state: JsonFormsState): UISchemaElement =>
extractUiSchema(get(state, 'jsonforms.core'));
export const getRefParserOptions = (state: JsonFormsState): RefParser.Options =>
extractRefParserOptions(get(state, 'jsonforms.core'));
export const getAjv = (
state: JsonFormsState
): Ajv => extractAjv(get(state, 'jsonforms.core'));
export const getDefaultData = (
state: JsonFormsState
): JsonFormsDefaultDataRegistryEntry[] =>
Expand All @@ -108,7 +113,6 @@ export const getRenderers = (
export const getCells = (
state: JsonFormsState
): JsonFormsCellRendererRegistryEntry[] => get(state, 'jsonforms.cells');

/**
* Finds a registered UI schema to use, if any.
* @param schema the JSON schema describing the data to be rendered
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/util/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
import isEmpty from 'lodash/isEmpty';
import union from 'lodash/union';
import { getConfig, getData, getErrorAt, getSchema } from '../reducers';
import { getConfig, getData, getErrorAt, getSchema, getAjv } from '../reducers';
import {
formatErrorMessage,
isEnabled,
Expand Down Expand Up @@ -107,12 +107,12 @@ export const mapStateToCellProps = (
const visible =
ownProps.visible !== undefined
? ownProps.visible
: isVisible(uischema, rootData);
: isVisible(uischema, rootData, undefined, getAjv(state));
const readOnly = state.jsonforms.readOnly;
const enabled =
!readOnly && (ownProps.enabled !== undefined
? ownProps.enabled
: isEnabled(uischema, rootData));
: isEnabled(uischema, rootData, undefined, getAjv(state)));
const errors = formatErrorMessage(
union(getErrorAt(path, schema)(state).map(error => error.message))
);
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
toDataPathSegments
} from './path';
import { isEnabled, isVisible } from './runtime';
import { Ajv } from 'ajv';

export { createCleanLabel, createLabelDescriptionFrom } from './label';

Expand Down Expand Up @@ -132,11 +133,11 @@ export { composePaths, composeWithUi, Paths, toDataPath };

// Runtime --
const Runtime = {
isEnabled(uischema: UISchemaElement, data: any): boolean {
return isEnabled(uischema, data);
isEnabled(uischema: UISchemaElement, data: any, ajv: Ajv): boolean {
return isEnabled(uischema, data,undefined, ajv);
},
isVisible(uischema: UISchemaElement, data: any): boolean {
return isVisible(uischema, data);
isVisible(uischema: UISchemaElement, data: any, ajv: Ajv): boolean {
return isVisible(uischema, data, undefined, ajv);
}
};
export { isEnabled, isVisible, Runtime, deriveTypes, hasType };
Expand Down
11 changes: 6 additions & 5 deletions packages/core/src/util/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import find from 'lodash/find';
import RefParser from 'json-schema-ref-parser';
import {
findUISchema,
getAjv,
getCells,
getConfig,
getData,
Expand Down Expand Up @@ -395,12 +396,12 @@ export const mapStateToControlProps = (
const path = composeWithUi(uischema, ownProps.path);
const visible: boolean =
ownProps.visible === undefined || hasShowRule(uischema)
? isVisible(uischema, rootData, ownProps.path)
? isVisible(uischema, rootData, ownProps.path, getAjv(state))
: ownProps.visible;
const readOnly = state.jsonforms.readOnly;
const enabled: boolean =
!readOnly && (ownProps.enabled === undefined || hasEnableRule(uischema)
? isEnabled(uischema, rootData, ownProps.path)
? isEnabled(uischema, rootData, ownProps.path, getAjv(state) )
: ownProps.enabled);
const controlElement = uischema as ControlElement;
const id = ownProps.id;
Expand Down Expand Up @@ -705,12 +706,12 @@ export const mapStateToLayoutProps = (
const { uischema } = ownProps;
const visible: boolean =
ownProps.visible === undefined || hasShowRule(uischema)
? isVisible(ownProps.uischema, rootData, ownProps.path)
? isVisible(ownProps.uischema, rootData, ownProps.path, getAjv(state))
: ownProps.visible;
const readOnly = state.jsonforms.readOnly;
const enabled: boolean =
!readOnly && (ownProps.enabled === undefined || hasEnableRule(uischema)
? isEnabled(ownProps.uischema, rootData, ownProps.path)
? isEnabled(ownProps.uischema, rootData, ownProps.path, getAjv(state))
: ownProps.enabled);

const data = Resolve.data(rootData, ownProps.path);
Expand Down Expand Up @@ -800,7 +801,7 @@ const mapStateToCombinatorRendererProps = (
);
const visible: boolean =
ownProps.visible === undefined || hasShowRule(uischema)
? isVisible(uischema, getData(state), ownProps.path)
? isVisible(uischema, getData(state), ownProps.path, getAjv(state))
: ownProps.visible;
const id = ownProps.id;

Expand Down
35 changes: 20 additions & 15 deletions packages/core/src/util/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ import {
import { resolveData } from './resolvers';
import { composeWithUi } from './path';
import { createAjv } from './validator';

const ajv = createAjv();
import { Ajv } from 'ajv';

const isOrCondition = (condition: Condition): condition is OrCondition =>
condition.type === 'OR';
Expand All @@ -59,16 +58,17 @@ const getConditionScope = (condition: Scopable, path: string): string => {
const evaluateCondition = (
data: any,
condition: Condition,
path: string
path: string,
ajv: Ajv
): boolean => {
if (isAndCondition(condition)) {
return condition.conditions.reduce(
(acc, cur) => acc && evaluateCondition(data, cur, path),
(acc, cur) => acc && evaluateCondition(data, cur, path, ajv),
true
);
} else if (isOrCondition(condition)) {
return condition.conditions.reduce(
(acc, cur) => acc || evaluateCondition(data, cur, path),
(acc, cur) => acc || evaluateCondition(data, cur, path, ajv),
false
);
} else if (isLeafCondition(condition)) {
Expand All @@ -86,18 +86,20 @@ const evaluateCondition = (
const isRuleFulfilled = (
uischema: UISchemaElement,
data: any,
path: string
path: string,
ajv: Ajv
): boolean => {
const condition = uischema.rule.condition;
return evaluateCondition(data, condition, path);
return evaluateCondition(data, condition, path, ajv);
};

export const evalVisibility = (
uischema: UISchemaElement,
data: any,
path: string = undefined
path: string = undefined,
ajv: Ajv = createAjv()
DBosley marked this conversation as resolved.
Show resolved Hide resolved
): boolean => {
const fulfilled = isRuleFulfilled(uischema, data, path);
const fulfilled = isRuleFulfilled(uischema, data, path, ajv);

switch (uischema.rule.effect) {
case RuleEffect.HIDE:
Expand All @@ -113,9 +115,10 @@ export const evalVisibility = (
export const evalEnablement = (
uischema: UISchemaElement,
data: any,
path: string = undefined
path: string = undefined,
ajv: Ajv = createAjv()
): boolean => {
const fulfilled = isRuleFulfilled(uischema, data, path);
const fulfilled = isRuleFulfilled(uischema, data, path, ajv);

switch (uischema.rule.effect) {
case RuleEffect.DISABLE:
Expand Down Expand Up @@ -153,10 +156,11 @@ export const hasEnableRule = (uischema: UISchemaElement): boolean => {
export const isVisible = (
uischema: UISchemaElement,
data: any,
path: string = undefined
path: string = undefined,
ajv: Ajv = createAjv()
): boolean => {
if (uischema.rule) {
return evalVisibility(uischema, data, path);
return evalVisibility(uischema, data, path, ajv);
}

return true;
Expand All @@ -165,10 +169,11 @@ export const isVisible = (
export const isEnabled = (
uischema: UISchemaElement,
data: any,
path: string = undefined
path: string = undefined,
ajv: Ajv = createAjv()
): boolean => {
if (uischema.rule) {
return evalEnablement(uischema, data, path);
return evalEnablement(uischema, data, path, ajv);
}

return true;
Expand Down
13 changes: 8 additions & 5 deletions packages/material/src/layouts/MaterialCategorizationLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ import {
} from '@jsonforms/core';
import { RendererComponent, withJsonFormsLayoutProps } from '@jsonforms/react';
import {
AjvProps,
MaterialLayoutRenderer,
MaterialLayoutRendererProps
MaterialLayoutRendererProps,
withAjvProps
} from '../util/layout';

export const isSingleLevelCategorization: Tester = and(
Expand All @@ -67,7 +69,7 @@ export interface CategorizationState {
}

export interface MaterialCategorizationLayoutRendererProps
extends StatePropsOfLayout {
extends StatePropsOfLayout, AjvProps {
selected?: number;
ownState?: boolean;
data?: any;
Expand All @@ -92,7 +94,8 @@ export class MaterialCategorizationLayoutRenderer extends RendererComponent<
uischema,
visible,
enabled,
selected
selected,
ajv
DBosley marked this conversation as resolved.
Show resolved Hide resolved
} = this.props;
const categorization = uischema as Categorization;
const value = this.hasOwnState() ? this.state.activeCategory : selected;
Expand All @@ -107,7 +110,7 @@ export class MaterialCategorizationLayoutRenderer extends RendererComponent<
cells
};
const categories = categorization.elements.filter((category: Category) =>
isVisible(category, data)
isVisible(category, data, undefined, ajv)
);
return (
<Hidden xsUp={!visible}>
Expand Down Expand Up @@ -140,4 +143,4 @@ export class MaterialCategorizationLayoutRenderer extends RendererComponent<
};
}

export default withJsonFormsLayoutProps(MaterialCategorizationLayoutRenderer);
export default withJsonFormsLayoutProps(withAjvProps(MaterialCategorizationLayoutRenderer));
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ import {
} from '@jsonforms/core';
import { RendererComponent, withJsonFormsLayoutProps } from '@jsonforms/react';
import {
AjvProps,
MaterialLayoutRenderer,
MaterialLayoutRendererProps
MaterialLayoutRendererProps,
withAjvProps
} from '../util/layout';

export const materialCategorizationStepperTester: RankedTester = rankWith(
Expand All @@ -57,8 +59,8 @@ export interface CategorizationStepperState {
}

export interface MaterialCategorizationStepperLayoutRendererProps
extends StatePropsOfLayout {
data: any;
extends StatePropsOfLayout, AjvProps {
data: any;
}

export class MaterialCategorizationStepperLayoutRenderer extends RendererComponent<
Expand All @@ -82,7 +84,8 @@ export class MaterialCategorizationStepperLayoutRenderer extends RendererCompone
uischema,
visible,
cells,
config
config,
ajv
} = this.props;
const categorization = uischema as Categorization;
const activeCategory = this.state.activeCategory;
Expand All @@ -96,7 +99,7 @@ export class MaterialCategorizationStepperLayoutRenderer extends RendererCompone
float: 'right' as 'right'
};
const buttonStyle = {
marginRight: '1em'
marginRight: '1em'
};
const childProps: MaterialLayoutRendererProps = {
elements: categorization.elements[activeCategory].elements,
Expand All @@ -108,7 +111,7 @@ export class MaterialCategorizationStepperLayoutRenderer extends RendererCompone
cells
};
const categories = categorization.elements.filter((category: Category) =>
isVisible(category, data)
isVisible(category, data, undefined, ajv)
);
return (
<Hidden xsUp={!visible}>
Expand Down Expand Up @@ -149,6 +152,6 @@ export class MaterialCategorizationStepperLayoutRenderer extends RendererCompone
}
}

export default withJsonFormsLayoutProps(
export default withJsonFormsLayoutProps(withAjvProps(
MaterialCategorizationStepperLayoutRenderer
);
));
21 changes: 18 additions & 3 deletions packages/material/src/util/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@
*/
import isEmpty from 'lodash/isEmpty';
import React from 'react';
import { ComponentType } from 'react';
import { Ajv } from 'ajv';
import {
getAjv,
JsonFormsCellRendererRegistryEntry,
JsonFormsRendererRegistryEntry,
JsonSchema,
OwnPropsOfRenderer,
UISchemaElement,
JsonFormsCellRendererRegistryEntry
UISchemaElement
} from '@jsonforms/core';
import { areEqual, ResolvedJsonFormsDispatch } from '@jsonforms/react';
import { areEqual, ResolvedJsonFormsDispatch, useJsonForms } from '@jsonforms/react';
import { Grid, Hidden } from '@material-ui/core';

export const renderLayoutElements = (
Expand Down Expand Up @@ -96,3 +99,15 @@ export const MaterialLayoutRenderer = React.memo(
},
areEqual
);

export interface AjvProps {
ajv: Ajv;
}

export const withAjvProps = <P extends {}>(Component: ComponentType<AjvProps & P>) =>
(props: P) => {
const ctx = useJsonForms();
const ajv = getAjv({jsonforms: {...ctx}});

return (<Component {...props} ajv={ajv} />);
};
Loading