Skip to content

Commit 987a216

Browse files
committed
More cleaning up and added more tests for merge
A few bug fixes and more tests
1 parent e1e8fbb commit 987a216

File tree

8 files changed

+169
-63
lines changed

8 files changed

+169
-63
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Core library for working with JSON-Schema with a UI-Schema (Form) definition tha
44
This library, through the use of its merge module, combines the schema and ui-schema
55
into a canonical schema for use by its services and external libraries.
66

7+
You **DO NOT** use this file in addition to Angular Schema Form, it is embedded at
8+
build into any frameworks using it.
9+
710
## Work-In-Progress!
811
There is [test output](docs/test.md) that forms some super basic documentation
912
and I intend to expand them much further to the point of almost being

dist/json-schema-form-core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/test.md

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ _merge.merge.should.be.an('function');
1616

1717
<a name="mergejs-merge"></a>
1818
## merge
19-
should combine a schema and form definition, regardless of order.
19+
should handle a schema lookup or schema for first argument.
2020

2121
```js
22-
(0, _merge.merge)(schema, ['name', 'gender']).should.be.deep.equal(stdForm.form);
23-
(0, _merge.merge)(schema, ['gender']).should.be.deep.equal([stdForm.form[1]]);
24-
(0, _merge.merge)(schema, ['gender', 'name']).should.be.deep.equal([stdForm.form[1], stdForm.form[0]]);
22+
(0, _merge.merge)(stdForm.lookup, ['name', 'shoe', 'gender']).should.be.deep.equal(stdForm.form);
23+
(0, _merge.merge)(schema, ['*']).should.be.deep.equal(stdForm.form);
2524
```
2625

2726
should handle a wildcard * in the form definition.
@@ -30,12 +29,107 @@ should handle a wildcard * in the form definition.
3029
(0, _merge.merge)(schema, ['*']).should.be.deep.equal(stdForm.form);
3130
```
3231

32+
should not handle a wildcard * if the schema is a lookup.
33+
34+
```js
35+
(0, _merge.merge)(stdForm.lookup, ['*']).should.not.be.deep.equal(stdForm.form);
36+
```
37+
38+
should combine a schema and form definition, regardless of order.
39+
40+
```js
41+
(0, _merge.merge)(schema, ['name', 'shoe', 'gender']).should.be.deep.equal(stdForm.form);
42+
(0, _merge.merge)(schema, ['gender']).should.be.deep.equal([stdForm.form[2]]);
43+
(0, _merge.merge)(schema, ['gender', 'name']).should.be.deep.equal([stdForm.form[2], stdForm.form[0]]);
44+
```
45+
3346
should allow items that are not in the schema.
3447

3548
```js
3649
(0, _merge.merge)(schema, ['*', { type: 'fieldset' }]).should.be.deep.equal(stdForm.form.concat([{ type: 'fieldset' }]));
3750
```
3851

52+
should translate "readOnly" in schema to "readonly" on the merged form defintion.
53+
54+
```js
55+
var merged = (0, _merge.merge)(schema, ['gender']);
56+
merged[0].should.have.property('readonly');
57+
merged[0].readonly.should.eq(true);
58+
```
59+
60+
should push readOnly in schema down into objects and arrays.
61+
62+
```js
63+
var subschema = {
64+
'type': 'object',
65+
'readOnly': true,
66+
'properties': {
67+
'sub': {
68+
'type': 'object',
69+
'properties': {
70+
'array': {
71+
'type': 'array',
72+
'items': {
73+
'type': 'object',
74+
'properties': {
75+
'foo': {
76+
'type': 'string'
77+
}
78+
}
79+
}
80+
}
81+
}
82+
}
83+
}
84+
};
85+
var merged = (0, _merge.merge)(subschema, ['*']);
86+
//sub
87+
merged[0].should.have.property('readonly');
88+
merged[0].readonly.should.eq(true);
89+
//array
90+
merged[0].items[0].should.have.property('readonly');
91+
merged[0].items[0].readonly.should.eq(true);
92+
//array items
93+
merged[0].items[0].items[0].should.have.property('readonly');
94+
merged[0].items[0].items[0].readonly.should.eq(true);
95+
```
96+
97+
should push readonly in form def down into objects and arrays.
98+
99+
```js
100+
var subschema = {
101+
'type': 'object',
102+
'properties': {
103+
'sub': {
104+
'type': 'object',
105+
'properties': {
106+
'array': {
107+
'type': 'array',
108+
'items': {
109+
'type': 'object',
110+
'properties': {
111+
'foo': {
112+
'type': 'string'
113+
}
114+
}
115+
}
116+
}
117+
}
118+
}
119+
}
120+
};
121+
var merged = (0, _merge.merge)(subschema, [{ key: 'sub', readonly: true }]);
122+
//sub
123+
merged[0].should.have.property('readonly');
124+
merged[0].readonly.should.eq(true);
125+
//array
126+
merged[0].items[0].should.have.property('readonly');
127+
merged[0].items[0].readonly.should.eq(true);
128+
//array items
129+
merged[0].items[0].items[0].should.have.property('readonly');
130+
merged[0].items[0].items[0].readonly.should.eq(true);
131+
```
132+
39133
<a name="schema-defaultsjs"></a>
40134
# schema-defaults.js
41135
should hold functions for generating a default form schema from defaults it creates.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "json-schema-form-core",
3-
"version": "1.0.0-alpha.5",
3+
"version": "1.0.0-alpha.6",
44
"description": "JSON-Schema and JSON-UI-Schema utilities for form generation.",
55
"main": "dist/json-schema-form-core.js",
66
"scripts": {
77
"build": "webpack",
8+
"dist-untested": "webpack --config webpack.config.dist.js",
89
"tslint": "tslint \"src/**/*.ts\"",
910
"test": "mocha --opts .mocha",
1011
"testdoc": "mocha --opts .mocha && mocha --opts .mocha.md > docs/test.md"

src/lib/merge.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@ import { defaultForm, createDefaults } from './schema-defaults';
33
import canonicalTitleMap from './canonical-title-map';
44

55
// export function merge(schema, form, schemaDefaultTypes, ignore, options, readonly, asyncTemplates) {
6-
export function merge(lookup, form, options, readonly, asyncTemplates) {
6+
export function merge(lookup, form, ignore, options, readonly, asyncTemplates) {
7+
let formItems = [];
78
form = form || [];
9+
let idx = form.indexOf('*');
810
options = options || {};
911

10-
const stdForm = defaultForm(lookup, createDefaults());
1112
if(typeof lookup === 'object' && lookup.hasOwnProperty('properties')) {
12-
let defaultForm = stdForm.lookup;
13-
lookup = defaultForm || lookup;
13+
readonly = readonly || lookup.readonly || lookup.readOnly;
14+
const stdForm = defaultForm(lookup, createDefaults(), ignore, options);
15+
let defaultFormLookup = stdForm.lookup;
16+
17+
lookup = defaultFormLookup || lookup;
18+
formItems = formItems.concat(stdForm.form);
1419
};
1520

16-
let idx = form.indexOf('*');
1721
if (idx !== -1) {
18-
form = form.slice(0, idx).concat(stdForm.form).concat(form.slice(idx + 1));
22+
form = form.slice(0, idx).concat(formItems).concat(form.slice(idx + 1));
1923
};
2024

2125
// ok let's merge!
@@ -59,14 +63,14 @@ export function merge(lookup, form, options, readonly, asyncTemplates) {
5963

6064
// if it's a type with items, merge 'em!
6165
if (obj.items) {
62-
obj.items = merge(lookup, obj.items, options, obj.readonly, asyncTemplates);
66+
obj.items = merge(lookup, obj.items, ignore, options, obj.readonly, asyncTemplates);
6367
}
6468

6569
// if its has tabs, merge them also!
6670
if (obj.tabs) {
6771
obj.tabs.forEach((tab) => {
6872
if (tab.items) {
69-
tab.items = merge(lookup, tab.items, options, obj.readonly, asyncTemplates);
73+
tab.items = merge(lookup, tab.items, ignore, options, obj.readonly, asyncTemplates);
7074
}
7175
});
7276
}

src/lib/merge.spec.js

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ describe('merge.js', () => {
2828
"description": "Gimme yea name lad",
2929
"type": "string"
3030
},
31+
"shoe": {
32+
"title": "Shoes",
33+
"description": "Shoe details",
34+
"type": "array",
35+
"items": {
36+
"type": "object",
37+
"properties": {
38+
"brand": {"type":"string"}
39+
}
40+
}
41+
},
3142
"gender": {
3243
"readOnly": true,
3344
"title": "Choose",
@@ -48,53 +59,38 @@ describe('merge.js', () => {
4859
})
4960

5061
describe('merge', () => {
51-
it('should combine a schema and form definition, regardless of order', () => {
52-
merge(schema, ['name','gender']).should.be.deep.equal(stdForm.form);
53-
merge(schema, ['gender']).should.be.deep.equal([stdForm.form[1]]);
54-
merge(schema, ['gender','name']).should.be.deep.equal([stdForm.form[1],stdForm.form[0]]);
62+
it('should handle a schema lookup or schema for first argument', () => {
63+
merge(stdForm.lookup, ['name','shoe','gender']).should.be.deep.equal(stdForm.form);
64+
merge(schema, ['*']).should.be.deep.equal(stdForm.form);
5565
});
5666

5767
it('should handle a wildcard * in the form definition', () => {
5868
merge(schema, ['*']).should.be.deep.equal(stdForm.form);
5969
});
6070

71+
it('should not handle a wildcard * if the schema is a lookup', () => {
72+
merge(stdForm.lookup, ['*']).should.not.be.deep.equal(stdForm.form);
73+
});
74+
75+
it('should combine a schema and form definition, regardless of order', () => {
76+
merge(schema, ['name','shoe','gender']).should.be.deep.equal(stdForm.form);
77+
merge(schema, ['gender']).should.be.deep.equal([stdForm.form[2]]);
78+
merge(schema, ['gender','name']).should.be.deep.equal([stdForm.form[2],stdForm.form[0]]);
79+
});
80+
81+
6182
it('should allow items that are not in the schema', () => {
6283
merge(schema, ['*', { type:'fieldset' }]).should.be.deep.equal(stdForm.form.concat([{ type:'fieldset' }]));
6384
});
64-
});
65-
/**
66-
it('should translate "readOnly" in schema to "readonly" on the merged form defintion', () => {
67-
inject(function(schemaForm){
68-
var schema = {
69-
"type": "object",
70-
"properties": {
71-
"name": {
72-
"title": "Name",
73-
"description": "Gimme yea name lad",
74-
"type": "string"
75-
},
76-
"gender": {
77-
"readOnly": true,
78-
"title": "Choose",
79-
"type": "string",
80-
"enum": [
81-
"undefined",
82-
"null",
83-
"NaN",
84-
]
85-
}
86-
}
87-
};
8885

89-
var merged = schemaForm.merge(schema, ['gender']);
86+
it('should translate "readOnly" in schema to "readonly" on the merged form defintion', () => {
87+
var merged = merge(schema, ['gender']);
9088
merged[0].should.have.property('readonly');
9189
merged[0].readonly.should.eq(true)
9290
});
93-
});
9491

95-
it('should push readOnly in schema down into objects and arrays', () => {
96-
inject(function(schemaForm) {
97-
var schema = {
92+
it('should push readOnly in schema down into objects and arrays', () => {
93+
let subschema = {
9894
'type': 'object',
9995
'readOnly': true,
10096
'properties': {
@@ -117,7 +113,7 @@ describe('merge.js', () => {
117113
}
118114
};
119115

120-
var merged = schemaForm.merge(schema, ['*']);
116+
var merged = merge(subschema, ['*']);
121117

122118
//sub
123119
merged[0].should.have.property('readonly');
@@ -130,13 +126,10 @@ describe('merge.js', () => {
130126
//array items
131127
merged[0].items[0].items[0].should.have.property('readonly');
132128
merged[0].items[0].items[0].readonly.should.eq(true);
133-
134129
});
135-
});
136130

137-
it('should push readonly in form def down into objects and arrays', () => {
138-
inject(function(schemaForm) {
139-
var schema = {
131+
it('should push readonly in form def down into objects and arrays', () => {
132+
let subschema = {
140133
'type': 'object',
141134
'properties': {
142135
'sub': {
@@ -158,7 +151,7 @@ describe('merge.js', () => {
158151
}
159152
};
160153

161-
var merged = schemaForm.merge(schema, [{key: 'sub', readonly: true}]);
154+
var merged = merge(subschema, [{key: 'sub', readonly: true}]);
162155

163156
//sub
164157
merged[0].should.have.property('readonly');
@@ -171,8 +164,6 @@ describe('merge.js', () => {
171164
//array items
172165
merged[0].items[0].items[0].should.have.property('readonly');
173166
merged[0].items[0].items[0].readonly.should.eq(true);
174-
175167
});
176168
});
177-
*/
178169
});

webpack.config.dist.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const config = require('./webpack.config.js');
2+
const path = require('path');
3+
const includes = [
4+
path.join(__dirname, 'src', 'lib', 'index')
5+
];
6+
7+
config.entry = {
8+
"json-schema-form-core": includes,
9+
"json-schema-form-core.min": includes
10+
}
11+
12+
module.exports = config;

webpack.config.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* global __dirname */
2-
var webpack = require('webpack');
3-
var path = require('path');
4-
var pjson = require('./package.json');
5-
var library = 'JSONSchemaFormCore';
6-
console.log('JSON Schema Form Core v' + pjson.version);
2+
const webpack = require('webpack');
3+
const path = require('path');
4+
const package = require('./package.json');
5+
const buildDate = new Date();
6+
console.log('JSON Schema Form Core v' + package.version);
77
const plugins = [
88
new webpack.BannerPlugin(
99
'json-schema-form-core\n' +
@@ -19,14 +19,15 @@ const plugins = [
1919
minimize: true
2020
})
2121
];
22+
let library = 'JSONSchemaFormCore';
2223

2324
module.exports = {
2425
entry: {
25-
"lib": './src/lib/index'
26+
"json-schema-form-core": [ path.join(__dirname, 'src', 'lib', 'index') ]
2627
},
2728
output: {
2829
path: path.join(__dirname, 'dist'),
29-
filename: 'json-schema-form-core.js',
30+
filename: '[name].js',
3031
library: library,
3132
libraryTarget: 'commonjs2'
3233
},

0 commit comments

Comments
 (0)