@@ -26,15 +26,15 @@ class autoFormly extends autoFormlyHelpers {
2626 * @public
2727 * @method autoFormly.collection
2828 * @param {Collection } collection
29- * @param {array } fields - schema keys you want to use (optional)
29+ * @param {array|object } options - schema keys you want to use or formly fields configuration (optional)
3030 * @returns {array }
3131 */
32- collection ( collection , fields ) {
32+ collection ( collection , options ) {
3333 if ( ! collection . simpleSchema ) {
3434 throw this . createError ( "Collection does not contain SimpleSchema" ) ;
3535 }
3636
37- return this . schema ( collection . simpleSchema ( ) , fields ) ;
37+ return this . schema ( collection . simpleSchema ( ) , options ) ;
3838 }
3939
4040 /**
@@ -43,20 +43,63 @@ class autoFormly extends autoFormlyHelpers {
4343 * @public
4444 * @method autoFormly.schema
4545 * @param {SimpleSchema } schema
46- * @param {array } fields - schema keys you want to use (optional)
46+ * @param {array|object } options - schema keys you want to use or formly fields configuration (optional)
4747 * @returns {array }
4848 */
49- schema ( schema , fields ) {
50- if ( ! schema || ! angular . isFunction ( schema . schema ) ) {
49+ schema ( schema , options ) {
50+ let sortedSchema ;
51+ const schemaCopy = angular . copy ( schema ) ;
52+
53+ /**
54+ * @deprecated avoid filtering with fields keys as array, use object configuration instead
55+ */
56+ if ( ! schemaCopy || ! angular . isFunction ( schemaCopy . schema ) ) {
5157 throw this . createError ( "Schema has to be instance of SimpleSchema" ) ;
5258 }
5359
54- let sortedSchema ;
60+ // filter and return
61+ if ( angular . isArray ( options ) ) {
62+ return this . fields ( this . filterSchema ( schemaCopy , options ) ) ;
63+ }
64+
65+ // no options
66+ if ( angular . isUndefined ( options ) ) {
67+ return this . fields ( schemaCopy . schema ( ) ) ;
68+ }
5569
56- if ( fields ) {
57- sortedSchema = this . filterSchema ( schema , fields ) ;
70+ //
71+ // handle options object
72+ //
73+
74+ // pass only object
75+ if ( ! angular . isObject ( options ) ) {
76+ throw this . createError ( "Options have to be an array or object" ) ;
77+ }
78+
79+ // use filter?
80+ if ( options . all === false ) {
81+ // pass only with defined fields
82+ if ( ! angular . isObject ( options . fields ) ) {
83+ throw this . createError ( "Missing or invalid property fields in options" ) ;
84+ }
85+ // skip fields marked as unused ("fieldKey": false)
86+ sortedSchema = this . filterSchema ( schemaCopy , Object . keys ( _ . omit ( options . fields , ( val ) => val === false ) ) ) ;
5887 } else {
59- sortedSchema = schema . schema ( ) ;
88+ sortedSchema = schemaCopy . schema ( ) ;
89+ }
90+
91+ // check each field
92+ if ( options . fields ) {
93+ _ . each ( sortedSchema , ( fSchema , fKey ) => {
94+ // and skip field if marked as unused
95+ if ( options . fields [ fKey ] === false ) {
96+ delete sortedSchema [ fKey ] ;
97+ }
98+ // or extend autoformly in schema using defined field extension
99+ if ( angular . isObject ( options . fields [ fKey ] ) ) {
100+ sortedSchema [ fKey ] . autoformly = angular . merge ( { } , fSchema . autoformly , options . fields [ fKey ] ) ;
101+ }
102+ } ) ;
60103 }
61104
62105 return this . fields ( sortedSchema ) ;
@@ -65,12 +108,12 @@ class autoFormly extends autoFormlyHelpers {
65108 /**
66109 *
67110 * @param {object } schema
68- * @param {array } fields
111+ * @param {array } fields - schema keys you want to use (optional)
69112 * @returns {object }
70113 */
71114 filterSchema ( schema , fields ) {
72115 if ( ! angular . isArray ( fields ) ) {
73- throw this . createError ( "Fields have to be an array" ) ;
116+ throw this . createError ( "Fields to filter have to be an array" ) ;
74117 }
75118
76119 const sorted = { } ;
0 commit comments