File tree 3 files changed +113
-0
lines changed
3 files changed +113
-0
lines changed Original file line number Diff line number Diff line change @@ -67,6 +67,11 @@ export function getDefaultRegistry() {
67
67
68
68
export function getSchemaType ( schema ) {
69
69
let { type } = schema ;
70
+
71
+ if ( ! type && schema . const ) {
72
+ return guessType ( schema . const ) ;
73
+ }
74
+
70
75
if ( ! type && schema . enum ) {
71
76
type = "string" ;
72
77
}
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change 5
5
dataURItoBlob ,
6
6
deepEquals ,
7
7
getDefaultFormState ,
8
+ getSchemaType ,
8
9
isFilesArray ,
9
10
isConstant ,
10
11
toConstant ,
@@ -1369,4 +1370,51 @@ describe("utils", () => {
1369
1370
expect ( guessType ( { } ) ) . eql ( "object" ) ;
1370
1371
} ) ;
1371
1372
} ) ;
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
+ } ) ;
1372
1420
} ) ;
You can’t perform that action at this time.
0 commit comments