Skip to content

Commit bc2f675

Browse files
add frontend config validation
1 parent afca90f commit bc2f675

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

src/context/FrontendConfigContext.tsx

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

3232
interface 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

Comments
 (0)