Skip to content

Commit

Permalink
fix: Disable schema validation by default
Browse files Browse the repository at this point in the history
* add `schemaValidation` flag to `IAgentOptions` to enable validation of inputs and outputs.
* update config templates to set `schemaValidation` flag to false

Closes #255
Closes #275
  • Loading branch information
mirceanis committed Nov 20, 2020
1 parent b97ff3b commit adf88b4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/daf-cli/default/client.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ server:
agent:
$require: daf-core#Agent
$args:
- plugins:
- schemaValidation: false
plugins:
- $require: daf-rest#AgentRestClient
$args:
- url: http://localhost:3332
Expand Down
3 changes: 2 additions & 1 deletion packages/daf-cli/default/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ messageHandler:
agent:
$require: daf-core#Agent
$args:
- plugins:
- schemaValidation: true
plugins:
- $require: daf-key-manager#KeyManager
$args:
- store:
Expand Down
14 changes: 12 additions & 2 deletions packages/daf-core/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ export interface IAgentOptions {
* ```
*/
context?: Record<string, any>

/**
* Flag that enables schema validation for plugin methods.
*
* @default false
*/
schemaValidation?: boolean
}

/**
Expand All @@ -85,6 +92,7 @@ export class Agent implements IAgent {
readonly methods: IPluginMethodMap = {}

private schema: IAgentPluginSchema
private schemaValidation: boolean
private context?: Record<string, any>
private protectedMethods = ['execute', 'availableMethods', 'emit']

Expand Down Expand Up @@ -167,6 +175,8 @@ export class Agent implements IAgent {
this[method] = async (args: any) => this.execute(method, args)
}
}

this.schemaValidation = options?.schemaValidation || false
}

/**
Expand Down Expand Up @@ -215,11 +225,11 @@ export class Agent implements IAgent {
Debug('daf:agent:' + method)('%o', args)
if (!this.methods[method]) throw Error('Method not available: ' + method)
const _args = args || {}
if (this.schema.components.methods[method]) {
if (this.schemaValidation && this.schema.components.methods[method]) {
validateArguments(method, _args, this.schema)
}
const result = await this.methods[method](_args, { ...this.context, agent: this })
if (this.schema.components.methods[method]) {
if (this.schemaValidation && this.schema.components.methods[method]) {
validateReturnType(method, result, this.schema)
}
Debug('daf:agent:' + method + ':result')('%o', result)
Expand Down

0 comments on commit adf88b4

Please sign in to comment.