Skip to content

Commit

Permalink
wip: configuration validation
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrrt committed Dec 14, 2023
1 parent 74216a7 commit fd641a0
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 23 deletions.
44 changes: 30 additions & 14 deletions packages/cli/jest.config.js
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',
}
4 changes: 3 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node-fetch": "^2.6.9",
"commander": "^11.1.0",
"typescript": "^5.2.2"
},
"dependencies": {
"graphql-tag": "^2.12.6"
"graphql-tag": "^2.12.6",
"zod": "^3.22.4"
}
}
4 changes: 0 additions & 4 deletions packages/cli/src/utils/introspectSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,12 @@ async function introspectRemoteSchema(endpointUrl: string) {
}
}


interface GraphQLSchema {
id: string;
uri: string;
token?: string; // some schemas require a token to be introspected
}



export const buildSchemaIntrospection = async (schemas:GraphQLSchema[], outputPath: string) => {
let schemaWithIntrospection = [];

Expand Down Expand Up @@ -86,4 +83,3 @@ export const buildSchemaIntrospection = async (schemas:GraphQLSchema[], outputPa
console.error(err);
}
};
// buildSchemaIntrospection();
62 changes: 62 additions & 0 deletions packages/cli/src/utils/validateConfig.test.ts
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
// });
});
67 changes: 64 additions & 3 deletions packages/cli/src/utils/validateConfig.ts
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;
}
};
3 changes: 2 additions & 1 deletion packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"outDir": "./build",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true
"sourceMap": true,
"strict": true
}
}
50 changes: 50 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fd641a0

Please sign in to comment.