Skip to content

Commit

Permalink
Infer field type from const value (#1174)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
LucianBuzzo authored and epicfaace committed Feb 8, 2019
1 parent b481f58 commit 6e79281
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ export function getDefaultRegistry() {

export function getSchemaType(schema) {
let { type } = schema;

if (!type && schema.const) {
return guessType(schema.const);
}

if (!type && schema.enum) {
type = "string";
}
Expand Down
60 changes: 60 additions & 0 deletions test/const_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { expect } from "chai";

import { createFormComponent, createSandbox } from "./test_utils";

describe("const", () => {
let sandbox;

beforeEach(() => {
sandbox = createSandbox();
});

afterEach(() => {
sandbox.restore();
});

it("should render a schema that uses const with a string value", () => {
const schema = {
type: "object",
properties: {
foo: { const: "bar" },
},
};

const { node } = createFormComponent({
schema,
});

expect(node.querySelector("input#root_foo")).not.eql(null);
});

it("should render a schema that uses const with a number value", () => {
const schema = {
type: "object",
properties: {
foo: { const: 123 },
},
};

const { node } = createFormComponent({
schema,
});

expect(node.querySelector("input#root_foo")).not.eql(null);
});

it("should render a schema that uses const with a boolean value", () => {
const schema = {
type: "object",
properties: {
foo: { const: true },
},
};

const { node } = createFormComponent({
schema,
});

expect(node.querySelector("input#root_foo[type='checkbox']")).not.eql(null);
});
});
48 changes: 48 additions & 0 deletions test/utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
dataURItoBlob,
deepEquals,
getDefaultFormState,
getSchemaType,
isFilesArray,
isConstant,
toConstant,
Expand Down Expand Up @@ -1369,4 +1370,51 @@ describe("utils", () => {
expect(guessType({})).eql("object");
});
});

describe("getSchemaType()", () => {
const cases = [
{
schema: { type: "string" },
expected: "string",
},
{
schema: { type: "number" },
expected: "number",
},
{
schema: { type: "integer" },
expected: "integer",
},
{
schema: { type: "object" },
expected: "object",
},
{
schema: { type: "array" },
expected: "array",
},
{
schema: { type: "boolean" },
expected: "boolean",
},
{
schema: { type: "null" },
expected: "null",
},
{
schema: { const: "foo" },
expected: "string",
},
{
schema: { const: 1 },
expected: "number",
},
];

it("should correctly guess the type of a schema", () => {
for (const test of cases) {
expect(getSchemaType(test.schema)).eql(test.expected);
}
});
});
});

0 comments on commit 6e79281

Please sign in to comment.