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 all 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
36 changes: 20 additions & 16 deletions packages/core/src/util/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ import {
} from '../models/uischema';
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 +57,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 +85,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
): 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 +114,10 @@ export const evalVisibility = (
export const evalEnablement = (
uischema: UISchemaElement,
data: any,
path: string = undefined
path: string = undefined,
ajv: Ajv
): 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 +155,11 @@ export const hasEnableRule = (uischema: UISchemaElement): boolean => {
export const isVisible = (
uischema: UISchemaElement,
data: any,
path: string = undefined
path: string = undefined,
ajv: Ajv
): boolean => {
if (uischema.rule) {
return evalVisibility(uischema, data, path);
return evalVisibility(uischema, data, path, ajv);
}

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

return true;
Expand Down
Loading