@@ -1657,6 +1657,44 @@ describe("getData", () => {
16571657 } ) ;
16581658 } ) ;
16591659 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+
16601698 it ( "should omit properties without default values when 'useTypeDefaults:false' even if required" , ( ) => {
16611699 const node = compileSchema ( {
16621700 type : "object" ,
@@ -1668,7 +1706,8 @@ describe("getData", () => {
16681706 }
16691707 } ) ;
16701708 const res = node . getData ( undefined , {
1671- extendDefaults : false , useTypeDefaults : false
1709+ extendDefaults : false ,
1710+ useTypeDefaults : false
16721711 } ) ;
16731712
16741713 assert . deepEqual ( res , { name : "John Doe" , country : "USA" } ) ;
@@ -1696,20 +1735,23 @@ describe("getData", () => {
16961735 active : { type : "boolean" , default : true }
16971736 }
16981737 } ) ;
1699- const res = node . getData ( { } , { addOptionalProps : true , useTypeDefaults : false } ) ;
1738+ const res = node . getData ( { } , { addOptionalProps : true , useTypeDefaults : false } ) ;
17001739
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+ ) ;
17101752 } ) ;
17111753
1712- it ( "should handle type string with default value and 'useTypeDefaults:false'" , ( ) => {
1754+ it ( "should handle type string with default value and 'useTypeDefaults:false'" , ( ) => {
17131755 const node = compileSchema ( {
17141756 type : "string" ,
17151757 default : "default value"
@@ -1722,9 +1764,9 @@ describe("getData", () => {
17221764
17231765 it ( "should handle type string without default value and 'useTypeDefaults:false'" , ( ) => {
17241766 const node = compileSchema ( {
1725- type : "string" ,
1767+ type : "string"
17261768 } ) ;
1727- const res = node . getData ( undefined , { useTypeDefaults : false } ) ;
1769+ const res = node . getData ( undefined , { useTypeDefaults : false } ) ;
17281770 assert . deepEqual ( res , undefined ) ;
17291771 } ) ;
17301772
@@ -1733,13 +1775,13 @@ describe("getData", () => {
17331775 type : "object" ,
17341776 required : [ "title" ] ,
17351777 properties : {
1736- title : {
1778+ title : {
17371779 type : "array" ,
17381780 items : { type : "string" }
17391781 }
17401782 }
17411783 } ) ;
1742- const res = node . getData ( undefined , { useTypeDefaults : false } ) ;
1784+ const res = node . getData ( undefined , { useTypeDefaults : false } ) ;
17431785 assert . deepEqual ( res , { title : [ ] } ) ;
17441786 } ) ;
17451787 } ) ;
0 commit comments