Skip to content

Commit

Permalink
feat: parse config.ts & validate user config (#181)
Browse files Browse the repository at this point in the history
Fixes OGBE-35
  • Loading branch information
NathanFlurry committed Mar 9, 2024
1 parent 8e952a2 commit c59de8b
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/build/deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
export { crypto } from "https://deno.land/std@0.208.0/crypto/mod.ts";
export { encodeHex } from "https://deno.land/std@0.208.0/encoding/hex.ts";

export * as tjs from "npm:typescript-json-schema@^0.62.0";

import Ajv from "npm:ajv@^8.12.0";
export { Ajv };

import dedent from "npm:dedent@^1.5.1";
export { dedent };
11 changes: 7 additions & 4 deletions src/build/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { generateClient } from "../migrate/generate.ts";
import { compileModuleConfigSchema } from "./module_config_schema.ts";

// TODO: Replace this with the OpenGB version instead since it means we'll change . We need to compile this in the build artifacts.
const CACHE_VERSION = 2;
const CACHE_VERSION = 3;

/**
* Which format to use for building.
Expand Down Expand Up @@ -489,7 +489,6 @@ async function buildModule(
project: Project,
module: Module,
) {
// TODO: This has problems with missing files
buildStep(buildState, {
name: "Module config",
module,
Expand All @@ -503,11 +502,15 @@ async function buildModule(
},
async alreadyCached() {
// Read schema from cache
module.configSchema = buildState.cache.oldCache.moduleConfigSchemas[module.name];
const schema = buildState.cache.oldCache.moduleConfigSchemas[module.name];
assertExists(schema);
module.configSchema = schema;
},
async finally() {
assertExists(module.configSchema);

// Populate cache with response
if (module.configSchema) buildState.cache.newCache.moduleConfigSchemas[module.name] = module.configSchema;
buildState.cache.newCache.moduleConfigSchemas[module.name] = module.configSchema;
},
});

Expand Down
34 changes: 31 additions & 3 deletions src/build/module_config_schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Ajv } from "./deps.ts";
import { configPath, Module, Project } from "../project/mod.ts";
import { runJob } from "../utils/worker_pool.ts";
import { WorkerRequest, WorkerResponse } from "./module_config_schema.worker.ts";
Expand All @@ -12,12 +13,39 @@ export async function compileModuleConfigSchema(
_project: Project,
module: Module,
): Promise<void> {
// Read schema
if (await exists(configPath(module))) {
// Load config
const res = await runJob(WORKER_POOL, { module });
module.configSchema = res.moduleConfigSchema;

// TODO: Validate schema
} else {
// TODO: Assert there is no config
// No config
module.configSchema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "#/definitions/Config",
"definitions": {
"Config": {
"type": "null",
},
},
};
}

// Validate config
const moduleConfigAjv = new Ajv.default({
schemas: [module.configSchema],
});

const moduleConfigSchema = moduleConfigAjv.getSchema(
"#/definitions/Config",
);
if (!moduleConfigSchema) {
throw new Error("Failed to get module config schema");
}

if (!moduleConfigSchema(module.userConfig)) {
throw new Error(
`Invalid module config:\n${JSON.stringify(module.userConfig)}\n${JSON.stringify(moduleConfigSchema.errors)}`,
);
}
}
4 changes: 2 additions & 2 deletions src/config/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ export interface ProjectModuleConfig extends Record<string, unknown> {
module?: string;

/**
* The config to pass to the registry.
* The config that configures how this module is ran at runtime.
*/
// config?: any;
config?: any;
}

// export async function readConfig(path: string): Promise<ProjectConfig> {
Expand Down
21 changes: 21 additions & 0 deletions src/project/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Project } from "./project.ts";
import { Registry } from "./registry.ts";
import { validateIdentifier } from "../types/identifiers/mod.ts";
import { IdentType } from "../types/identifiers/defs.ts";
import { ProjectModuleConfig } from "../config/project.ts";

export interface Module {
/**
Expand All @@ -16,8 +17,22 @@ export interface Module {
*/
path: string;
name: string;

/**
* Config from the project.yaml file.
*/
projectModuleConfig: ProjectModuleConfig;

/**
* Config from the module.yaml file.
*/
config: ModuleConfig;

/**
* The config passed to this module in the project.yaml file.
*/
userConfig: any;

/**
* The registry that the module was pulled from.
*/
Expand All @@ -41,6 +56,7 @@ export interface ModuleDatabase {
export async function loadModule(
modulePath: string,
name: string,
projectModuleConfig: ProjectModuleConfig,
registry: Registry,
): Promise<Module> {
// Read config
Expand Down Expand Up @@ -108,9 +124,14 @@ export async function loadModule(
};
}

// Derive config
const userConfig = projectModuleConfig.config ?? null;

return {
path: modulePath,
name,
projectModuleConfig,
userConfig,
config,
registry,
scripts,
Expand Down
4 changes: 3 additions & 1 deletion src/project/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ export async function loadProject(opts: LoadProjectOpts): Promise<Project> {
// Load modules
const modules = new Map<string, Module>();
for (const projectModuleName in projectConfig.modules) {
const projectModuleConfig = projectConfig.modules[projectModuleName];

const { path, registry } = await fetchAndResolveModule(
projectRoot,
projectConfig,
registries,
projectModuleName,
);
const module = await loadModule(path, projectModuleName, registry);
const module = await loadModule(path, projectModuleName, projectModuleConfig, registry);
modules.set(projectModuleName, module);
}

Expand Down
10 changes: 6 additions & 4 deletions tests/basic/backend.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
registries:
# default:
# local:
# directory: ../../../opengb-registry/modules
local:
local:
directory: ./modules
directory: ./modules
modules:
currency: {}
friends: {}
Expand All @@ -13,3 +10,8 @@ modules:
users: {}
foo:
registry: local
config_test:
registry: local
config:
foo: hello world
bar: 1234
4 changes: 4 additions & 0 deletions tests/basic/modules/config_test/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Config {
foo: string;
bar: number;
}
2 changes: 2 additions & 0 deletions tests/basic/modules/config_test/module.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scripts: {}
errors: {}

0 comments on commit c59de8b

Please sign in to comment.