Skip to content

Commit 26c5242

Browse files
committed
Add $ref resolution and update webpack
1 parent a473c1f commit 26c5242

10 files changed

+1474
-88
lines changed

.babelrc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"presets": [
3-
[ "es2015" ]
3+
[ "es2015", "stage-0" ]
44
],
5-
"plugins": [ "transform-flow-strip-types" ]
5+
"plugins": [ "transform-flow-strip-types", "transform-async-to-generator" ]
66
}

bower.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "json-schema-form-core",
3+
"main": "dist/json-schema-form-core.js",
4+
"maintainers": [
5+
"Marcel J Bennett <[email protected]>"
6+
],
7+
"description": "json-schema-form-core",
8+
"keywords": [
9+
"json",
10+
"json-schema",
11+
"schema",
12+
"json-schema-form"
13+
],
14+
"license": "MIT",
15+
"ignore": [
16+
"**/.*",
17+
"node_modules",
18+
"bower_components",
19+
"index.js",
20+
"package.json",
21+
"docs"
22+
],
23+
"devDependencies": {
24+
"json-refs": "2.1.6"
25+
}
26+
}

dist/json-schema-form-core.js

+1,294-79
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/json-refs-standalone-min.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+10-5
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.1",
3+
"version": "1.0.0-alpha.2",
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+
"watch": "webpack --watch",
89
"dist-untested": "webpack --config webpack.config.dist.js",
910
"tslint": "tslint \"src/**/*.ts\"",
1011
"test": "mocha --opts .mocha",
@@ -26,9 +27,9 @@
2627
"bugs": {
2728
"url": "https://github.com/json-schema-form/json-schema-form-core/issues"
2829
},
29-
"repository" : {
30-
"type" : "git",
31-
"url" : "http://github.com/json-schema-form/json-schema-form-core.git"
30+
"repository": {
31+
"type": "git",
32+
"url": "http://github.com/json-schema-form/json-schema-form-core.git"
3233
},
3334
"keywords": [
3435
"angular-schema-form",
@@ -43,12 +44,16 @@
4344
"@types/node": "^6.0.42",
4445
"babel-core": "^6.18.2",
4546
"babel-loader": "^6.2.9",
47+
"babel-plugin-syntax-async-functions": "^6.13.0",
48+
"babel-plugin-transform-async-to-generator": "^6.22.0",
4649
"babel-plugin-transform-flow-strip-types": "^6.21.0",
4750
"babel-polyfill": "^6.20.0",
4851
"babel-preset-es2015": "^6.18.0",
4952
"babel-preset-latest": "^6.16.0",
53+
"babel-preset-stage-0": "^6.22.0",
5054
"babel-register": "^6.18.0",
5155
"chai": "^3.5.0",
56+
"json-refs": "^2.1.6",
5257
"karma": "^1.3.0",
5358
"karma-babel-preprocessor": "^6.0.1",
5459
"karma-chai-sinon": "^0.1.5",
@@ -67,6 +72,6 @@
6772
"tslint-loader": "^3.2.1",
6873
"tv4": "^1.2.7",
6974
"typescript": "~2.0.3",
70-
"webpack": "^2.1.0-beta.27"
75+
"webpack": "^2.2.1"
7176
}
7277
}

src/lib/resolve.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as JsonRefs from './../../lib/json-refs-standalone-min';
2+
3+
export function jsonref(schema, callBack) {
4+
let promise = new Promise(
5+
function(resolve, reject) {
6+
JsonRefs.resolveRefs(schema, {
7+
"filter": [ 'relative', 'local' ]
8+
})
9+
.then(function(res) { resolve(res.resolved); })
10+
.catch(function(err) { reject(new Error(err)); });
11+
}
12+
);
13+
14+
if(typeof(callBack) === 'function') {
15+
promise
16+
.then((resolved) => { callBack(null, resolved); })
17+
.catch((error) => { callBack(error); });
18+
}
19+
else {
20+
return promise;
21+
}
22+
};

src/lib/resolve.spec.js

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import chai from 'chai';
2+
import { describe, it} from 'mocha';
3+
import { jsonref } from './resolve';
4+
5+
chai.should();
6+
7+
describe('resolve.js', () => {
8+
const schema = {
9+
"id": "http://some.site.somewhere/entry-schema#",
10+
"$schema": "http://json-schema.org/draft-04/schema#",
11+
"description": "schema for an fstab entry",
12+
"type": "object",
13+
"required": [ "storage" ],
14+
"properties": {
15+
"storage": {
16+
"type": "object",
17+
"oneOf": [
18+
{ "$ref": "#/definitions/diskDevice" },
19+
{ "$ref": "#/definitions/diskUUID" },
20+
{ "$ref": "#/definitions/nfs" },
21+
{ "$ref": "#/definitions/tmpfs" }
22+
]
23+
}
24+
},
25+
"definitions": {
26+
"diskDevice": {
27+
"properties": {
28+
"type": { "enum": [ "disk" ] },
29+
"device": {
30+
"type": "string",
31+
"pattern": "^/dev/[^/]+(/[^/]+)*$"
32+
}
33+
},
34+
"required": [ "type", "device" ],
35+
"additionalProperties": false
36+
},
37+
"diskUUID": {
38+
"properties": {
39+
"type": { "enum": [ "disk" ] },
40+
"label": {
41+
"type": "string",
42+
"pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
43+
}
44+
},
45+
"required": [ "type", "label" ],
46+
"additionalProperties": false
47+
},
48+
"nfs": {
49+
"properties": {
50+
"type": { "enum": [ "nfs" ] },
51+
"remotePath": {
52+
"type": "string",
53+
"pattern": "^(/[^/]+)+$"
54+
},
55+
"server": {
56+
"type": "string",
57+
"oneOf": [
58+
{ "format": "host-name" },
59+
{ "format": "ipv4" },
60+
{ "format": "ipv6" }
61+
]
62+
}
63+
},
64+
"required": [ "type", "server", "remotePath" ],
65+
"additionalProperties": false
66+
},
67+
"tmpfs": {
68+
"properties": {
69+
"type": { "enum": [ "tmpfs" ] },
70+
"sizeInMB": {
71+
"type": "integer",
72+
"minimum": 16,
73+
"maximum": 512
74+
}
75+
},
76+
"required": [ "type", "sizeInMB" ],
77+
"additionalProperties": false
78+
}
79+
}
80+
};
81+
82+
it('should contain a function for resolving relative & local references', () => {
83+
jsonref.should.be.an('function');
84+
})
85+
86+
describe('jsonref', () => {
87+
88+
it('should resolve json-ref via promise', (done) => {
89+
jsonref(schema)
90+
.then((resolved) => {
91+
resolved.properties.storage.oneOf[0].properties.should.have.property('device');
92+
resolved.properties.storage.oneOf[0].properties.should.equal(
93+
resolved.definitions.diskDevice.properties
94+
);
95+
resolved.properties.storage.oneOf[3].properties.should.equal(
96+
resolved.definitions.tmpfs.properties
97+
);
98+
done();
99+
})
100+
.catch((error) => {
101+
done(error);
102+
});
103+
});
104+
105+
it('should resolve json-ref via callback', (done) => {
106+
jsonref(schema, function(error, resolved) {
107+
if (error) done(error);
108+
resolved.properties.storage.oneOf[0].properties.should.have.property('device');
109+
done();
110+
});
111+
});
112+
});
113+
});

src/module.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import canonicalTitleMapImp from './lib/canonical-title-map';
44

55
export { merge } from './lib/merge';
66
export { select } from './lib/select';
7+
export { jsonref } from './lib/resolve';
78
export { traverseSchema, traverseForm } from './lib/traverse';
89
export { validate } from './lib/validate';
910

src/module.spec.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
validate,
88
sfPath,
99
schemaDefaults,
10-
canonicalTitleMap
10+
canonicalTitleMap,
11+
jsonref
1112
} from './module';
1213

1314
chai.should();
@@ -22,5 +23,6 @@ describe('module.js', () => {
2223
sfPath.should.be.an('object');
2324
schemaDefaults.should.be.an('object');
2425
canonicalTitleMap.should.be.an('function');
26+
jsonref.should.be.an('function');
2527
});
2628
});

0 commit comments

Comments
 (0)