From b99431b368bb41fb87439436eb62adc2d425737a Mon Sep 17 00:00:00 2001 From: ssube Date: Sat, 2 Nov 2019 10:13:29 -0500 Subject: [PATCH] feat(visitor): move jsonpath pick to context for modules to use --- src/rule/SchemaRule.ts | 13 +++------- src/utils/index.ts | 4 +++ src/visitor/VisitorContext.ts | 46 +++++++++++++++++++++++++---------- 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/src/rule/SchemaRule.ts b/src/rule/SchemaRule.ts index 4c59e00..c54caf8 100644 --- a/src/rule/SchemaRule.ts +++ b/src/rule/SchemaRule.ts @@ -1,5 +1,4 @@ import { ValidateFunction } from 'ajv'; -import { JSONPath } from 'jsonpath-plus'; import { cloneDeep, defaultTo, isNil } from 'lodash'; import { LogLevel } from 'noicejs'; @@ -39,17 +38,13 @@ export class SchemaRule implements RuleData, Visitor { } public async pick(ctx: VisitorContext, root: any): Promise> { - const scopes = JSONPath({ - json: root, - path: this.select, - }); + const items = ctx.pick(this.select, root); - if (hasItems(scopes)) { - return scopes; + if (items.length === 0) { + ctx.logger.debug('no data selected'); } - ctx.logger.debug('no data selected'); - return []; + return items; } public async visit(ctx: VisitorContext, node: any): Promise { diff --git a/src/utils/index.ts b/src/utils/index.ts index f4c0fc7..4e80cf0 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,5 +1,9 @@ import { isNil } from 'lodash'; +export function doesExist(val: T | null | undefined): val is T { + return !isNil(val); +} + export function hasItems(val: Array | null | undefined): val is Array { return (Array.isArray(val) && val.length > 0); } diff --git a/src/visitor/VisitorContext.ts b/src/visitor/VisitorContext.ts index 671835a..061eb93 100644 --- a/src/visitor/VisitorContext.ts +++ b/src/visitor/VisitorContext.ts @@ -1,6 +1,8 @@ import Ajv from 'ajv'; +import { JSONPath } from 'jsonpath-plus'; import { Logger, logWithLevel } from 'noicejs'; +import { doesExist, hasItems } from '../utils'; import { VisitorError } from './VisitorError'; import { VisitorResult } from './VisitorResult'; @@ -45,6 +47,22 @@ export class VisitorContext implements VisitorContextOptions, VisitorResult { this.innerOptions = options.innerOptions; } + public addSchema(name: string, schema: any): void { + this.logger.debug({ + name, + schema, + }, 'adding ajv schema'); + + this.ajv.addSchema({ + $id: name, + definitions: schema, + }); + } + + public compile(schema: any): Ajv.ValidateFunction { + return this.ajv.compile(schema); + } + public error(...errors: Array) { for (const err of errors) { logWithLevel(this.logger, err.level, err.data, err.msg); @@ -59,19 +77,21 @@ export class VisitorContext implements VisitorContextOptions, VisitorResult { return this; } - public compile(schema: any): Ajv.ValidateFunction { - return this.ajv.compile(schema); - } - - public addSchema(name: string, schema: any): void { - this.logger.debug({ - name, - schema, - }, 'adding ajv schema'); - - this.ajv.addSchema({ - $id: name, - definitions: schema, + public pick(path: string, root: any): Array { + const items = JSONPath({ + json: root, + path, }); + + this.logger.debug({ + items, + path, + }, 'path query picked items'); + + if (hasItems(items)) { + return items.filter(doesExist); + } else { + return []; + } } }