@@ -1657,6 +1657,44 @@ describe("getData", () => {
1657
1657
} ) ;
1658
1658
} ) ;
1659
1659
describe ( "defaultTemplateOptions.useTypeDefaults" , ( ) => {
1660
+ it ( "should return initial value for missing default-properties" , ( ) => {
1661
+ const data = compileSchema ( {
1662
+ required : [ "valid" ] ,
1663
+ properties : { valid : { type : "string" } }
1664
+ } ) . getData ( null , { useTypeDefaults : true } ) ;
1665
+
1666
+ assert . deepEqual ( data , { valid : "" } ) ;
1667
+ } ) ;
1668
+
1669
+ it ( "should omit initial value for missing default-properties" , ( ) => {
1670
+ const data = compileSchema ( {
1671
+ required : [ "valid" ] ,
1672
+ properties : { valid : { type : "string" } }
1673
+ } ) . getData ( null , { useTypeDefaults : false } ) ;
1674
+
1675
+ assert . deepEqual ( data , { } ) ;
1676
+ } ) ;
1677
+
1678
+ // @todo : reevaluate this behaviour
1679
+ it ( "should return initial object-value for missing default-properties, even if useTypeDefaults: false" , ( ) => {
1680
+ const data = compileSchema ( {
1681
+ required : [ "valid" ] ,
1682
+ properties : { valid : { type : "string" } }
1683
+ } ) . getData ( null , { useTypeDefaults : false } ) ;
1684
+
1685
+ assert . deepEqual ( data , { } ) ;
1686
+ } ) ;
1687
+
1688
+ // @todo : reevaluate this behaviour
1689
+ it ( "should return initial array-value for missing default-properties" , ( ) => {
1690
+ const data = compileSchema ( {
1691
+ required : [ "valid" ] ,
1692
+ properties : { valid : { type : "array" , items : { type : "string" } , minItems : 1 } }
1693
+ } ) . getData ( null , { useTypeDefaults : false } ) ;
1694
+
1695
+ assert . deepEqual ( data , { valid : [ undefined ] } ) ;
1696
+ } ) ;
1697
+
1660
1698
it ( "should omit properties without default values when 'useTypeDefaults:false' even if required" , ( ) => {
1661
1699
const node = compileSchema ( {
1662
1700
type : "object" ,
@@ -1668,7 +1706,8 @@ describe("getData", () => {
1668
1706
}
1669
1707
} ) ;
1670
1708
const res = node . getData ( undefined , {
1671
- extendDefaults : false , useTypeDefaults : false
1709
+ extendDefaults : false ,
1710
+ useTypeDefaults : false
1672
1711
} ) ;
1673
1712
1674
1713
assert . deepEqual ( res , { name : "John Doe" , country : "USA" } ) ;
@@ -1696,20 +1735,23 @@ describe("getData", () => {
1696
1735
active : { type : "boolean" , default : true }
1697
1736
}
1698
1737
} ) ;
1699
- const res = node . getData ( { } , { addOptionalProps : true , useTypeDefaults : false } ) ;
1738
+ const res = node . getData ( { } , { addOptionalProps : true , useTypeDefaults : false } ) ;
1700
1739
1701
- assert . deepEqual ( JSON . stringify ( res ) , JSON . stringify ( {
1702
- user : {
1703
- username : "guest" ,
1704
- profile : {
1705
- theme : "light"
1706
- }
1707
- } ,
1708
- active : true
1709
- } ) ) ;
1740
+ assert . deepEqual (
1741
+ JSON . stringify ( res ) ,
1742
+ JSON . stringify ( {
1743
+ user : {
1744
+ username : "guest" ,
1745
+ profile : {
1746
+ theme : "light"
1747
+ }
1748
+ } ,
1749
+ active : true
1750
+ } )
1751
+ ) ;
1710
1752
} ) ;
1711
1753
1712
- it ( "should handle type string with default value and 'useTypeDefaults:false'" , ( ) => {
1754
+ it ( "should handle type string with default value and 'useTypeDefaults:false'" , ( ) => {
1713
1755
const node = compileSchema ( {
1714
1756
type : "string" ,
1715
1757
default : "default value"
@@ -1722,9 +1764,9 @@ describe("getData", () => {
1722
1764
1723
1765
it ( "should handle type string without default value and 'useTypeDefaults:false'" , ( ) => {
1724
1766
const node = compileSchema ( {
1725
- type : "string" ,
1767
+ type : "string"
1726
1768
} ) ;
1727
- const res = node . getData ( undefined , { useTypeDefaults : false } ) ;
1769
+ const res = node . getData ( undefined , { useTypeDefaults : false } ) ;
1728
1770
assert . deepEqual ( res , undefined ) ;
1729
1771
} ) ;
1730
1772
@@ -1733,13 +1775,13 @@ describe("getData", () => {
1733
1775
type : "object" ,
1734
1776
required : [ "title" ] ,
1735
1777
properties : {
1736
- title : {
1778
+ title : {
1737
1779
type : "array" ,
1738
1780
items : { type : "string" }
1739
1781
}
1740
1782
}
1741
1783
} ) ;
1742
- const res = node . getData ( undefined , { useTypeDefaults : false } ) ;
1784
+ const res = node . getData ( undefined , { useTypeDefaults : false } ) ;
1743
1785
assert . deepEqual ( res , { title : [ ] } ) ;
1744
1786
} ) ;
1745
1787
} ) ;
0 commit comments