@@ -27,7 +27,7 @@ export const FrontendConfigContext = createContext<FrontendConfigContextProps |
2727) ;
2828
2929
30- const fetchPromise = fetch ( '/frontend-config.json' ) . then ( ( res ) => res . json ( ) ) . then ( ( data ) => data as FrontendConfig ) ;
30+ const fetchPromise = fetch ( '/frontend-config.json' ) . then ( ( res ) => res . json ( ) ) . then ( ( data ) => validateAndCastFrontendConfig ( data ) ) ;
3131
3232interface FrontendConfigProviderProps {
3333 children : ReactNode ;
@@ -55,3 +55,42 @@ export const useFrontendConfig = () => {
5555 }
5656 return context ;
5757} ;
58+
59+ function validateAndCastFrontendConfig ( config : unknown ) : FrontendConfig {
60+ if ( typeof config !== 'object' || config === null ) {
61+ throw new Error ( 'Invalid frontend config' ) ;
62+ }
63+ const castedConfig = config as FrontendConfig ;
64+ if ( ! castedConfig . backendUrl ) {
65+ throw new Error ( 'Invalid frontend config: missing backendUrl' ) ;
66+ }
67+ if ( ! castedConfig . gatewayUrl ) {
68+ throw new Error ( 'Invalid frontend config: missing gatewayUrl' ) ;
69+ }
70+ if ( ! castedConfig . documentationBaseUrl ) {
71+ throw new Error ( 'Invalid frontend config: missing documentationBaseUrl' ) ;
72+ }
73+ if ( ! castedConfig . oidcConfig ) {
74+ throw new Error ( 'Invalid frontend config: missing oidcConfig' ) ;
75+ }
76+ if ( typeof castedConfig . oidcConfig !== 'object' || castedConfig . oidcConfig === null ) {
77+ throw new Error ( 'Invalid frontend config: oidcConfig is not an object' ) ;
78+ }
79+ if ( ! castedConfig . oidcConfig . clientId ) {
80+ throw new Error ( 'Invalid frontend config: missing clientId in oidcConfig' ) ;
81+ }
82+ if ( ! castedConfig . oidcConfig . issuerUrl ) {
83+ throw new Error ( 'Invalid frontend config: missing issuerUrl in oidcConfig' ) ;
84+ }
85+ if ( ! castedConfig . oidcConfig . scopes ) {
86+ throw new Error ( 'Invalid frontend config: missing scopes in oidcConfig' ) ;
87+ }
88+ if ( ! Array . isArray ( castedConfig . oidcConfig . scopes ) ) {
89+ throw new Error ( 'Invalid frontend config: scopes in oidcConfig is not an array' ) ;
90+ }
91+ if ( castedConfig . landscape && ! Object . values ( Landscape ) . includes ( castedConfig . landscape ) ) {
92+ throw new Error ( 'Invalid frontend config: invalid landscape' ) ;
93+ }
94+
95+ return castedConfig ;
96+ }
0 commit comments