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 050e5f0
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/api/daf-core.iagentoptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ export interface IAgentOptions
| [context](./daf-core.iagentoptions.context.md) | Record<string, any> | The context object that will be available to the plugin methods |
| [overrides](./daf-core.iagentoptions.overrides.md) | [IPluginMethodMap](./daf-core.ipluginmethodmap.md) | The map of plugin methods. Can be used to override methods provided by plugins, or to add additional methods without writing a plugin |
| [plugins](./daf-core.iagentoptions.plugins.md) | [IAgentPlugin](./daf-core.iagentplugin.md)<!-- -->\[\] | The array of agent plugins |
| [schemaValidation](./daf-core.iagentoptions.schemavalidation.md) | boolean | Flag that enables schema validation for plugin methods. false |

15 changes: 15 additions & 0 deletions docs/api/daf-core.iagentoptions.schemavalidation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [daf-core](./daf-core.md) &gt; [IAgentOptions](./daf-core.iagentoptions.md) &gt; [schemaValidation](./daf-core.iagentoptions.schemavalidation.md)

## IAgentOptions.schemaValidation property

Flag that enables schema validation for plugin methods.

false

<b>Signature:</b>

```typescript
schemaValidation?: boolean;
```
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
25 changes: 25 additions & 0 deletions packages/daf-core/api/daf-core.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,31 @@
"startIndex": 1,
"endIndex": 3
}
},
{
"kind": "PropertySignature",
"canonicalReference": "daf-core!IAgentOptions#schemaValidation:member",
"docComment": "/**\n * Flag that enables schema validation for plugin methods.\n *\n * @default false\n */\n",
"excerptTokens": [
{
"kind": "Content",
"text": "schemaValidation?: "
},
{
"kind": "Content",
"text": "boolean"
},
{
"kind": "Content",
"text": ";"
}
],
"releaseTag": "Public",
"name": "schemaValidation",
"propertyTypeTokenRange": {
"startIndex": 1,
"endIndex": 2
}
}
],
"extendsTokenRanges": []
Expand Down
1 change: 1 addition & 0 deletions packages/daf-core/api/daf-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface IAgentOptions {
context?: Record<string, any>
overrides?: IPluginMethodMap
plugins?: IAgentPlugin[]
schemaValidation?: boolean
}

// @public
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 050e5f0

Please sign in to comment.