This repository was archived by the owner on Jan 13, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +88
-0
lines changed
Expand file tree Collapse file tree 3 files changed +88
-0
lines changed Original file line number Diff line number Diff line change 180180 );
181181 }
182182}
183+
184+ /// Validates keys in provided theme configuration by comparing with original
185+ /// theme configuration.
186+ /// Use this in component's `theme()` mixin implementation to validate if
187+ /// provided theme configuration has supported theme configuration keys.
188+ /// @param {Map} $origin-theme Original theme configuration in Sass map format
189+ /// that has all supported keys.
190+ /// @param {Map} $custom-theme Provided theme configuration in Sass map format
191+ /// that should be validated against `$origin-theme`.
192+ /// @examples
193+ /// @mixin theme($theme) {
194+ /// @include theme.validate-keys($light-theme, $theme);
195+ ///
196+ /// // ...
197+ /// }
198+ @mixin validate-keys ($origin-theme , $custom-theme , $test-only : false) {
199+ $origin-keys : map .keys ($origin-theme );
200+ $custom-keys : map .keys ($custom-theme );
201+ $unsupported-keys : ();
202+
203+ @each $key in $custom-keys {
204+ @if (not list .index ($origin-keys , $key )) {
205+ $unsupported-keys : list .append ($unsupported-keys , $key );
206+ }
207+ }
208+
209+ @if list .length ($unsupported-keys ) > 0 {
210+ $error-message : ' Unsupported keys found: #{$unsupported-keys } . Expected one of: #{$origin-keys } .' ;
211+
212+ @if $test-only {
213+ content : $error-message ;
214+ } @else {
215+ @error $error-message ;
216+ }
217+ }
218+ }
Original file line number Diff line number Diff line change 1+ @use ' ../theme' ;
2+
3+ $origin-theme : (
4+ one : 1 ,
5+ two : ' 22' ,
6+ three : 3px ,
7+ four : (
8+ x : 1 ,
9+ y : 2 ,
10+ ),
11+ );
12+
13+ // Should validate when subset of keys are provided.
14+ @include theme .validate-keys (
15+ $origin-theme ,
16+ (
17+ two: ' 22' ,
18+ three: 3px ,
19+ )
20+ );
21+
22+ // Should validate when theme configuration is empty.
23+ @include theme .validate-keys ($origin-theme , ());
24+
25+ // Should validate when nested theme keys are provided.
26+ @include theme .validate-keys (
27+ $origin-theme ,
28+ (
29+ four: (
30+ x : 11 ,
31+ y : 22 ,
32+ ),
33+ )
34+ );
35+
36+ // Should throw error when unsupported key (i.e., foobar) is provided.
37+ .test {
38+ @include theme .validate-keys (
39+ $origin-theme ,
40+ (
41+ foobar: 66px ,
42+ ),
43+ $test-only : true
44+ );
45+ }
Original file line number Diff line number Diff line change @@ -76,4 +76,11 @@ describe('theme.test.scss', () => {
7676 expect ( css ) . toContain ( '--mdc-theme-primary: teal' ) ;
7777 expect ( css ) . toContain ( '--mdc-theme-secondary: crimson' ) ;
7878 } ) ;
79+
80+ it ( 'validate-keys Should throw error when unsupported key is provided' ,
81+ ( ) => {
82+ const filePath = path . join ( __dirname , 'theme-validate-keys.test.css' ) ;
83+ const css = fs . readFileSync ( filePath , 'utf8' ) . trim ( ) ;
84+ expect ( css ) . toContain ( 'Unsupported keys found: foobar.' ) ;
85+ } ) ;
7986} ) ;
You can’t perform that action at this time.
0 commit comments