Skip to content

Commit

Permalink
Preselect with default schema values #1193 (#1224)
Browse files Browse the repository at this point in the history
* Preselect with default schema values #1193
- Allow for options to be passed into `createAjv`. Clients that want to have preselected
default values should pass in the 'useDefaults: true' option to the
createAjv method and use the Ajv object in the Actions.init call.
  • Loading branch information
AlexandraBuzila authored and edgarmueller committed Jan 23, 2019
1 parent 1c348fb commit f5351df
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 4 deletions.
6 changes: 4 additions & 2 deletions packages/core/src/util/validator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as AJV from 'ajv';
import { Options } from 'ajv';
import { Draft4 } from '../models/draft4';

export const createAjv = () => {
export const createAjv = (options?: Options) => {
const ajv = new AJV({
schemaId: 'auto',
allErrors: true,
jsonPointers: true,
errorDataPath: 'property'
errorDataPath: 'property',
...options
});
ajv.addFormat('time', '^([0-1][0-9]|2[0-3]):[0-5][0-9]$');
ajv.addMetaSchema(Draft4);
Expand Down
141 changes: 139 additions & 2 deletions packages/core/test/util/renderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { init, update, UPDATE_DATA, UpdateAction } from '../../src/actions';
import test from 'ava';
import * as _ from 'lodash';
import * as Redux from 'redux';
import {
clearAllIds,
createAjv,
createDefaultValue,
mapDispatchToArrayControlProps,
mapDispatchToControlProps,
Expand All @@ -36,7 +37,7 @@ import {
OwnPropsOfControl
} from '../../src/util';
import configureStore from 'redux-mock-store';
import { init, update, UPDATE_DATA, UpdateAction } from '../../src/actions';
import * as _ from 'lodash';
import { generateDefaultUISchema } from '../../src/generators';
import {
ControlElement,
Expand Down Expand Up @@ -588,3 +589,139 @@ test('mapStateToLayoutProps - hidden via state with path from ownProps ', t => {
const props = mapStateToLayoutProps(state, ownProps);
t.false(props.visible);
});

test('should assign defaults to enum', t => {
const schema: JsonSchema = {
type: 'object',
properties: {
name: {
type: 'string',
minLength: 1
},
color: {
type: 'string',
enum: ['red', 'green', 'blue'],
default: 'green'
}
}
};

const uischema: UISchemaElement = undefined;

const data = {
name: 'foo'
};

const initState = {
jsonforms: {
core: {
uischema,
schema,
data,
errors: [] as ErrorObject[]
}
}
};
const store: Store<JsonFormsState> = createStore(
combineReducers({ jsonforms: jsonformsReducer() }),
initState
);
store.dispatch(
init(data, schema, uischema, createAjv({ useDefaults: true }))
);

t.is(store.getState().jsonforms.core.data.color, 'green');
});

test('should assign defaults to empty item within nested object of an array', t => {
const schema: JsonSchema = {
type: 'array',
items: {
type: 'object',
properties: {
message: {
type: 'string',
default: 'foo'
}
}
}
};

const uischema: ControlElement = {
type: 'Control',
scope: '#'
};

const data = [{}];

const initState = {
jsonforms: {
core: {
uischema,
schema,
data,
errors: [] as ErrorObject[]
}
}
};
const store: Store<JsonFormsState> = createStore(
combineReducers({ jsonforms: jsonformsReducer() }),
initState
);
store.dispatch(
init(data, schema, uischema, createAjv({ useDefaults: true }))
);

t.is(store.getState().jsonforms.core.data.length, 1);
t.deepEqual(store.getState().jsonforms.core.data[0], { message: 'foo' });
});

test('should assign defaults to newly added item within nested object of an array', t => {
const schema: JsonSchema = {
type: 'array',
items: {
type: 'object',
properties: {
message: {
type: 'string',
default: 'foo'
}
}
}
};

const uischema: ControlElement = {
type: 'Control',
scope: '#'
};

const data = [{}];

const initState = {
jsonforms: {
core: {
uischema,
schema,
data,
errors: [] as ErrorObject[]
}
}
};
const store: Store<JsonFormsState> = createStore(
combineReducers({ jsonforms: jsonformsReducer() }),
initState
);
store.dispatch(
init(data, schema, uischema, createAjv({ useDefaults: true }))
);
const ownProps: OwnPropsOfControl = {
schema,
uischema
};
const props = mapDispatchToArrayControlProps(store.dispatch, ownProps);

props.addItem('')();

t.is(store.getState().jsonforms.core.data.length, 2);
t.deepEqual(store.getState().jsonforms.core.data[1], { message: 'foo' });
});

0 comments on commit f5351df

Please sign in to comment.