Skip to content

Commit

Permalink
possibility to add our own ajv config
Browse files Browse the repository at this point in the history
  • Loading branch information
GabinH authored and edgarmueller committed Sep 28, 2018
1 parent 4c1eccd commit 0796c36
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
15 changes: 13 additions & 2 deletions packages/core/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import { RankedTester } from '../testers';
import { JsonSchema, UISchemaElement } from '../';
import { generateDefaultUISchema, generateJsonSchema } from '../generators';
import { UISchemaTester } from '../reducers/uischemas';
import * as AJV from 'ajv';

export const INIT: 'jsonforms/INIT' = 'jsonforms/INIT';
export const SET_AJV: 'jsonforms/SET_AJV' = 'jsonforms/SET_AJV';
export const UPDATE_DATA: 'jsonforms/UPDATE' = 'jsonforms/UPDATE';
export const VALIDATE: 'jsonforms/VALIDATE' = 'jsonforms/VALIDATE';
export const ADD_RENDERER: 'jsonforms/ADD_RENDERER' = 'jsonforms/ADD_RENDERER';
Expand All @@ -50,13 +52,15 @@ export interface UpdateAction {
export const init = (
data: any,
schema: JsonSchema = generateJsonSchema(data),
uischema: UISchemaElement = generateDefaultUISchema(schema)
uischema: UISchemaElement = generateDefaultUISchema(schema),
ajv?: AJV.Ajv
) =>
({
type: INIT,
data,
schema,
uischema
uischema,
ajv
});

export const registerDefaultData = (
Expand All @@ -73,6 +77,13 @@ export const unregisterDefaultData = (schemaPath: string) => ({
schemaPath
});

export const setAjv = (
ajv: AJV.Ajv
) => ({
type: SET_AJV,
ajv
});

export const update =
(path: string, updater: (any) => any): UpdateAction => ({
type: UPDATE_DATA,
Expand Down
15 changes: 13 additions & 2 deletions packages/core/src/reducers/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
import * as _ from 'lodash';
import { ErrorObject, ValidateFunction } from 'ajv';
import { INIT, UPDATE_DATA } from '../actions';
import { INIT, SET_AJV, UPDATE_DATA } from '../actions';
import { createAjv } from '../util/validator';
import { JsonSchema, UISchemaElement } from '..';

Expand Down Expand Up @@ -70,7 +70,8 @@ export const coreReducer = (
switch (action.type) {
case INIT: {

const v = ajv.compile(action.schema);
const thisAjv = action.ajv ? action.ajv : ajv;
const v = thisAjv.compile(action.schema);
const e = sanitizeErrors(v, action.data);

return {
Expand All @@ -81,6 +82,16 @@ export const coreReducer = (
errors: e
};
}
case SET_AJV: {
const currentAjv = action.ajv;
const validator = currentAjv.compile(state.schema);
const errors = sanitizeErrors(validator, state.data);
return {
...state,
validator,
errors
};
}
case UPDATE_DATA: {

if (action.path === undefined || action.path === null) {
Expand Down

0 comments on commit 0796c36

Please sign in to comment.