From e3b49153b76932a08b2a33b7149921efec4ee81b Mon Sep 17 00:00:00 2001 From: goenning Date: Mon, 27 Dec 2021 12:08:42 +0000 Subject: [PATCH] add example of custom operator Signed-off-by: goenning --- .changeset/spicy-moons-poke.md | 2 +- .../README.md | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/.changeset/spicy-moons-poke.md b/.changeset/spicy-moons-poke.md index fdf130c491..cc39d629bf 100644 --- a/.changeset/spicy-moons-poke.md +++ b/.changeset/spicy-moons-poke.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-tech-insights-backend-module-jsonfc': minor +'@backstage/plugin-tech-insights-backend-module-jsonfc': patch --- ability to add custom operators diff --git a/plugins/tech-insights-backend-module-jsonfc/README.md b/plugins/tech-insights-backend-module-jsonfc/README.md index 108b1c8564..6703f4b9f9 100644 --- a/plugins/tech-insights-backend-module-jsonfc/README.md +++ b/plugins/tech-insights-backend-module-jsonfc/README.md @@ -85,3 +85,32 @@ export const exampleCheck: TechInsightJsonRuleCheck = { }, }; ``` + +# Custom operators + +json-rules-engine supports a limited [number of built-in operators](https://github.com/CacheControl/json-rules-engine/blob/master/docs/rules.md#operators) that can be used in conditions. You can add your own operators by adding them to the `operators` array in the `JsonRulesEngineFactCheckerFactory` constructor. For example: + +```diff +const myFactCheckerFactory = new JsonRulesEngineFactCheckerFactory({ + checks: [], + logger, ++ operators: [ new Operator("startsWith", (a, b) => a.startsWith(b) ] +}) +``` + +And you can then use it in your checks like this: + +```js +... +rule: { + conditions: { + any: [ + { + fact: 'version', + operator: 'startsWith', + value: '12', + }, + ], + }, +} +```