Skip to content

Commit 6e79281

Browse files
LucianBuzzoepicfaace
authored andcommittedFeb 8, 2019
Infer field type from const value (#1174)
This change adds support for schemas that use the `const` keyword without and adjacent `type` keyword. For example: ```json { "type": "object", "properties": { "firstName": { "const": "Chuck" } } } ``` Signed-off-by: Lucian <lucian.buzzo@gmail.com>
1 parent b481f58 commit 6e79281

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed
 

‎src/utils.js

+5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ export function getDefaultRegistry() {
6767

6868
export function getSchemaType(schema) {
6969
let { type } = schema;
70+
71+
if (!type && schema.const) {
72+
return guessType(schema.const);
73+
}
74+
7075
if (!type && schema.enum) {
7176
type = "string";
7277
}

‎test/const_test.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { expect } from "chai";
2+
3+
import { createFormComponent, createSandbox } from "./test_utils";
4+
5+
describe("const", () => {
6+
let sandbox;
7+
8+
beforeEach(() => {
9+
sandbox = createSandbox();
10+
});
11+
12+
afterEach(() => {
13+
sandbox.restore();
14+
});
15+
16+
it("should render a schema that uses const with a string value", () => {
17+
const schema = {
18+
type: "object",
19+
properties: {
20+
foo: { const: "bar" },
21+
},
22+
};
23+
24+
const { node } = createFormComponent({
25+
schema,
26+
});
27+
28+
expect(node.querySelector("input#root_foo")).not.eql(null);
29+
});
30+
31+
it("should render a schema that uses const with a number value", () => {
32+
const schema = {
33+
type: "object",
34+
properties: {
35+
foo: { const: 123 },
36+
},
37+
};
38+
39+
const { node } = createFormComponent({
40+
schema,
41+
});
42+
43+
expect(node.querySelector("input#root_foo")).not.eql(null);
44+
});
45+
46+
it("should render a schema that uses const with a boolean value", () => {
47+
const schema = {
48+
type: "object",
49+
properties: {
50+
foo: { const: true },
51+
},
52+
};
53+
54+
const { node } = createFormComponent({
55+
schema,
56+
});
57+
58+
expect(node.querySelector("input#root_foo[type='checkbox']")).not.eql(null);
59+
});
60+
});

‎test/utils_test.js

+48
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
dataURItoBlob,
66
deepEquals,
77
getDefaultFormState,
8+
getSchemaType,
89
isFilesArray,
910
isConstant,
1011
toConstant,
@@ -1369,4 +1370,51 @@ describe("utils", () => {
13691370
expect(guessType({})).eql("object");
13701371
});
13711372
});
1373+
1374+
describe("getSchemaType()", () => {
1375+
const cases = [
1376+
{
1377+
schema: { type: "string" },
1378+
expected: "string",
1379+
},
1380+
{
1381+
schema: { type: "number" },
1382+
expected: "number",
1383+
},
1384+
{
1385+
schema: { type: "integer" },
1386+
expected: "integer",
1387+
},
1388+
{
1389+
schema: { type: "object" },
1390+
expected: "object",
1391+
},
1392+
{
1393+
schema: { type: "array" },
1394+
expected: "array",
1395+
},
1396+
{
1397+
schema: { type: "boolean" },
1398+
expected: "boolean",
1399+
},
1400+
{
1401+
schema: { type: "null" },
1402+
expected: "null",
1403+
},
1404+
{
1405+
schema: { const: "foo" },
1406+
expected: "string",
1407+
},
1408+
{
1409+
schema: { const: 1 },
1410+
expected: "number",
1411+
},
1412+
];
1413+
1414+
it("should correctly guess the type of a schema", () => {
1415+
for (const test of cases) {
1416+
expect(getSchemaType(test.schema)).eql(test.expected);
1417+
}
1418+
});
1419+
});
13721420
});

0 commit comments

Comments
 (0)
Please sign in to comment.