-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
211 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,32 @@ | ||
module.exports = { | ||
// setupFilesAfterEnv: ['./jest.setup.js'], | ||
transform: { | ||
'^.+\\.tsx?$': ['ts-jest', {//the content you'd placed at "global" | ||
babel: true, | ||
tsconfig: 'tsconfig.json', | ||
}] | ||
}, | ||
"transformIgnorePatterns": [ | ||
"/node_modules/", | ||
moduleFileExtensions: [ | ||
'js', | ||
'ts', | ||
'tsx', | ||
], | ||
transform: { | ||
'^.+\\.(ts|tsx)?$': [ | ||
'ts-jest' | ||
], | ||
// moduleNameMapper: { | ||
// '^@authdog\/platform-utils$': require.resolve('@authdog/platform-utils'), | ||
// } | ||
} | ||
|
||
}, | ||
modulePaths: [ | ||
"<rootDir>", | ||
], | ||
moduleNameMapper: { | ||
'^@authdog/(.*)$': '<rootDir>/packages/$1/src', | ||
}, | ||
testMatch: [ | ||
'**/**/*.test.(ts|js)', | ||
'**/test/**/*.test.(ts|js)', | ||
], | ||
testPathIgnorePatterns: [ | ||
'/node_modules/', | ||
'build', | ||
], | ||
testEnvironment: 'node', | ||
rootDir: '.', | ||
collectCoverageFrom: [ | ||
'!<rootDir>/src', | ||
], | ||
preset: 'ts-jest', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { validateConfig } from './validateConfig'; // Update with the correct file path | ||
|
||
describe('validateConfig function', () => { | ||
const validConfig: any = { | ||
rateLimiting: { | ||
default: { | ||
budget: 100, | ||
}, | ||
}, | ||
publicQueries: [ | ||
{ | ||
name: 'health', | ||
}, | ||
{ | ||
name: 'hydraDevQuery', | ||
}, | ||
], | ||
jwksUri: 'https://id.authdog.com/oidc/.well-known/jwks.json', | ||
}; | ||
|
||
const invalidConfig: any = { | ||
rateLimiting: { | ||
default: { | ||
budget: 'not a number', | ||
}, | ||
}, | ||
publicQueries: [ | ||
{ | ||
name: 'health', | ||
}, | ||
{ | ||
name: 123, // invalid type | ||
}, | ||
], | ||
jwksUri: 'invalid uri', // invalid URL format | ||
}; | ||
|
||
it ("should return true", () => { | ||
expect(true).toBe(true); | ||
}); | ||
|
||
// it('should validate a valid config object without throwing errors', () => { | ||
// expect(() => validateConfig(validConfig)).not.toThrow(); | ||
// }); | ||
|
||
// it('should throw a ZodError for an invalid config object', () => { | ||
// expect(() => validateConfig(invalidConfig)).toThrow(); | ||
// }); | ||
|
||
// it('should log validation errors to the console for an invalid config object', () => { | ||
// const consoleErrorSpy = jest.spyOn(console, 'error'); | ||
// consoleErrorSpy.mockImplementation(() => {}); // Mock console.error to do nothing | ||
|
||
// try { | ||
// validateConfig(invalidConfig); | ||
// } catch (error) { | ||
// expect(consoleErrorSpy).toHaveBeenCalled(); | ||
// } | ||
|
||
// consoleErrorSpy.mockRestore(); // Restore console.error | ||
// }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,65 @@ | ||
// TODO: use zod for dynamic typing validation | ||
export const validateConfig = (config) => { | ||
import {ZodError, z} from "zod"; | ||
|
||
} | ||
/* | ||
const HydraConfig = { | ||
rateLimiting: { | ||
default: { | ||
budget: 100, | ||
}, | ||
}, | ||
publicQueries: [ | ||
{ | ||
name: "health", | ||
}, | ||
{ | ||
name: "hydraDevQuery", | ||
}, | ||
], | ||
jwksUri: "https://id.authdog.com/oidc/.well-known/jwks.json", | ||
}; | ||
*/ | ||
|
||
interface IHydraConfig { | ||
rateLimiting?: { | ||
default: { | ||
budget: number; | ||
} | ||
}, | ||
publicQueries?: { | ||
name: string; | ||
}[], | ||
jwksUri: string; | ||
} | ||
|
||
const rateLimitingSchema = z.object({ | ||
default: z.object({ | ||
budget: z.number(), | ||
}), | ||
}); | ||
|
||
const publicQuerySchema = z.object({ | ||
name: z.string(), | ||
}); | ||
|
||
const hydraConfigSchema = z.object({ | ||
rateLimiting: rateLimitingSchema.optional(), | ||
publicQueries: z.array(publicQuerySchema).optional(), | ||
jwksUri: z.string(), | ||
}); | ||
|
||
export const validateConfig = (config: IHydraConfig): IHydraConfig => { | ||
try { | ||
// Validate the provided config object against the Zod schema | ||
const validatedConfig = hydraConfigSchema.parse(config); | ||
return validatedConfig; | ||
} catch (error) { | ||
if (error instanceof ZodError) { | ||
// Handle validation errors here | ||
console.error("Validation error:", error.errors); | ||
// You might want to throw an error or handle it accordingly based on your application's needs | ||
} | ||
// Throw the error if it's not a ZodError (unexpected error) | ||
throw error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.