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

Preselect with default schema values #1193 #1224

Merged
merged 3 commits into from
Jan 23, 2019
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
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' });
});