-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add assistant config schema api (#193)
* Add API to get full configuration schema Resolve #192 * Remove unused import * Support all rule option types Adjust `RuleOption` type to not allow null values in complex object types. This is consistent with the primitive option data types/ * Add changeset
- Loading branch information
1 parent
9ee7d59
commit 54a3ab9
Showing
4 changed files
with
287 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@sketch-hq/sketch-assistant-types': minor | ||
'@sketch-hq/sketch-assistant-utils': minor | ||
--- | ||
|
||
Add `buildAssistantConfigurationSchema` to get JSON Schema describing entire Assistant configuration | ||
shape. |
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
223 changes: 223 additions & 0 deletions
223
packages/utils/src/assistant-config-schema/__tests__/index.ts
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,223 @@ | ||
import { buildAssistantConfigSchema } from '..' | ||
import { createAssistantConfig, createAssistantDefinition, createRule } from '../../test-helpers' | ||
|
||
describe('buildAssistantConfigSchema', () => { | ||
test('builds configuration schema', (): void => { | ||
const assistant = createAssistantDefinition({ | ||
rules: [createRule({ name: 'foo' })], | ||
config: createAssistantConfig({ | ||
rules: { | ||
foo: { active: true }, | ||
}, | ||
}), | ||
}) | ||
|
||
expect(buildAssistantConfigSchema(assistant)).toMatchInlineSnapshot(` | ||
Object { | ||
"properties": Object { | ||
"rules": Object { | ||
"properties": Object { | ||
"foo": Object { | ||
"properties": Object { | ||
"active": Object { | ||
"default": true, | ||
"type": "boolean", | ||
}, | ||
}, | ||
"required": Array [ | ||
"active", | ||
], | ||
"type": "object", | ||
}, | ||
}, | ||
"type": "object", | ||
}, | ||
}, | ||
"type": "object", | ||
} | ||
`) | ||
}) | ||
|
||
test('builds configuration schema exclusing inactive rules', (): void => { | ||
const assistant = createAssistantDefinition({ | ||
rules: [createRule({ name: 'foo' })], | ||
config: createAssistantConfig({ | ||
rules: {}, | ||
}), | ||
}) | ||
|
||
expect(buildAssistantConfigSchema(assistant)).toMatchInlineSnapshot(` | ||
Object { | ||
"properties": Object { | ||
"rules": Object { | ||
"properties": Object {}, | ||
"type": "object", | ||
}, | ||
}, | ||
"type": "object", | ||
} | ||
`) | ||
}) | ||
|
||
test('use integer option config value as default', (): void => { | ||
const assistant = createAssistantDefinition({ | ||
rules: [ | ||
createRule({ | ||
name: 'foo', | ||
getOptions: (helpers) => [ | ||
helpers.integerOption({ | ||
name: 'option', | ||
title: 'integer option', | ||
description: 'some detail', | ||
}), | ||
], | ||
}), | ||
], | ||
config: createAssistantConfig({ | ||
rules: { | ||
foo: { active: true, option: 1 }, | ||
}, | ||
}), | ||
}) | ||
|
||
expect(buildAssistantConfigSchema(assistant)).toMatchInlineSnapshot(` | ||
Object { | ||
"properties": Object { | ||
"rules": Object { | ||
"properties": Object { | ||
"foo": Object { | ||
"properties": Object { | ||
"active": Object { | ||
"default": true, | ||
"type": "boolean", | ||
}, | ||
"option": Object { | ||
"default": 1, | ||
"description": "some detail", | ||
"title": "integer option", | ||
"type": "integer", | ||
}, | ||
}, | ||
"required": Array [ | ||
"active", | ||
"option", | ||
], | ||
"type": "object", | ||
}, | ||
}, | ||
"type": "object", | ||
}, | ||
}, | ||
"type": "object", | ||
} | ||
`) | ||
}) | ||
|
||
test('use number option config value as default', (): void => { | ||
const assistant = createAssistantDefinition({ | ||
rules: [ | ||
createRule({ | ||
name: 'foo', | ||
getOptions: (helpers) => [ | ||
helpers.numberOption({ | ||
name: 'option', | ||
title: 'number option', | ||
description: 'some detail', | ||
}), | ||
], | ||
}), | ||
], | ||
config: createAssistantConfig({ | ||
rules: { | ||
foo: { active: true, option: 1.5 }, | ||
}, | ||
}), | ||
}) | ||
|
||
expect(buildAssistantConfigSchema(assistant)).toMatchInlineSnapshot(` | ||
Object { | ||
"properties": Object { | ||
"rules": Object { | ||
"properties": Object { | ||
"foo": Object { | ||
"properties": Object { | ||
"active": Object { | ||
"default": true, | ||
"type": "boolean", | ||
}, | ||
"option": Object { | ||
"default": 1.5, | ||
"description": "some detail", | ||
"title": "number option", | ||
"type": "number", | ||
}, | ||
}, | ||
"required": Array [ | ||
"active", | ||
"option", | ||
], | ||
"type": "object", | ||
}, | ||
}, | ||
"type": "object", | ||
}, | ||
}, | ||
"type": "object", | ||
} | ||
`) | ||
}) | ||
|
||
test('use string option config value as default', (): void => { | ||
const assistant = createAssistantDefinition({ | ||
rules: [ | ||
createRule({ | ||
name: 'foo', | ||
getOptions: (helpers) => [ | ||
helpers.stringOption({ | ||
name: 'option', | ||
title: 'string option', | ||
description: 'some detail', | ||
}), | ||
], | ||
}), | ||
], | ||
config: createAssistantConfig({ | ||
rules: { | ||
foo: { active: true, option: 'hello world' }, | ||
}, | ||
}), | ||
}) | ||
|
||
expect(buildAssistantConfigSchema(assistant)).toMatchInlineSnapshot(` | ||
Object { | ||
"properties": Object { | ||
"rules": Object { | ||
"properties": Object { | ||
"foo": Object { | ||
"properties": Object { | ||
"active": Object { | ||
"default": true, | ||
"type": "boolean", | ||
}, | ||
"option": Object { | ||
"default": "hello world", | ||
"description": "some detail", | ||
"title": "string option", | ||
"type": "string", | ||
}, | ||
}, | ||
"required": Array [ | ||
"active", | ||
"option", | ||
], | ||
"type": "object", | ||
}, | ||
}, | ||
"type": "object", | ||
}, | ||
}, | ||
"type": "object", | ||
} | ||
`) | ||
}) | ||
}) |
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,51 @@ | ||
import { | ||
AssistantConfigSchemaCreator, | ||
JSONSchemaProps, | ||
ReservedRuleOptionNames, | ||
RuleConfig, | ||
RuleDefinition, | ||
} from '@sketch-hq/sketch-assistant-types' | ||
import { buildRuleOptionSchema, helpers } from '../rule-option-schemas' | ||
|
||
const getReservedRuleOptions = (config: RuleConfig): JSONSchemaProps => { | ||
const typeMap = { | ||
[ReservedRuleOptionNames.active]: 'boolean', | ||
[ReservedRuleOptionNames.severity]: 'number', | ||
[ReservedRuleOptionNames.ruleTitle]: 'string', | ||
} | ||
|
||
return Object.entries(typeMap).reduce((acc, [opt, type]) => { | ||
if (!(opt in config)) return acc | ||
return { ...acc, [opt]: { type } } | ||
}, {}) | ||
} | ||
|
||
const applyRuleConfig = (ops: JSONSchemaProps, config: RuleConfig): JSONSchemaProps => { | ||
return Object.entries(ops).reduce((acc, [key, schema]) => { | ||
return { ...acc, [key]: { ...schema, default: config[key] } } | ||
}, {}) | ||
} | ||
|
||
const buildAssistantConfigSchema: AssistantConfigSchemaCreator = (assistant) => { | ||
return { | ||
type: 'object', | ||
properties: { | ||
rules: { | ||
type: 'object', | ||
properties: assistant.rules.reduce((acc, rule: RuleDefinition) => { | ||
const config = assistant.config.rules[rule.name] | ||
if (!config) return acc | ||
|
||
const ops = [ | ||
getReservedRuleOptions(config), | ||
...(typeof rule.getOptions !== 'undefined' ? rule.getOptions(helpers) : []), | ||
].map((val) => applyRuleConfig(val, config)) | ||
|
||
return { ...acc, [rule.name]: buildRuleOptionSchema(ops) } | ||
}, {}), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
export { buildAssistantConfigSchema } |