move pick implementation to rule
This commit is contained in:
parent
6dc9b70e15
commit
eb8c665bf2
|
@ -2,7 +2,7 @@ import { Document, Source } from '../source';
|
|||
|
||||
export interface Parser {
|
||||
/**
|
||||
* @todo should dump deal with Sources as well? does it care about paths?
|
||||
* @TODO should dump deal with Sources as well? does it care about paths?
|
||||
*/
|
||||
dump(...data: Array<Document>): string;
|
||||
parse(data: Source): Array<Document>;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { doesExist, ensureArray } from '@apextoaster/js-utils';
|
||||
import { doesExist, ensureArray, hasItems } from '@apextoaster/js-utils';
|
||||
import { ErrorObject } from 'ajv';
|
||||
import { JSONPath } from 'jsonpath-plus';
|
||||
import lodash from 'lodash';
|
||||
import { LogLevel } from 'noicejs';
|
||||
|
||||
|
@ -72,13 +73,28 @@ export class SchemaRule implements Rule, RuleData {
|
|||
}
|
||||
|
||||
public async pick(ctx: VisitorContext, root: Document): Promise<Array<Element>> {
|
||||
const items = ctx.pick(this.select, root);
|
||||
const items = JSONPath({
|
||||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
||||
json: root.data as any,
|
||||
path: this.select,
|
||||
}) as Array<unknown>;
|
||||
|
||||
if (items.length === 0) {
|
||||
ctx.logger.debug({
|
||||
items,
|
||||
path: this.select,
|
||||
}, 'path query picked items');
|
||||
|
||||
const elems = items.filter(doesExist).map((data, index) => ({
|
||||
data,
|
||||
document: root,
|
||||
index,
|
||||
}));
|
||||
|
||||
if (!hasItems(elems)) {
|
||||
ctx.logger.debug('no data selected');
|
||||
}
|
||||
|
||||
return items;
|
||||
return elems;
|
||||
}
|
||||
|
||||
public async visit(ctx: VisitorContext, elem: Element): Promise<RuleResult> {
|
||||
|
|
|
@ -1,10 +1,7 @@
|
|||
import { doesExist, hasItems } from '@apextoaster/js-utils';
|
||||
import Ajv, { ValidateFunction } from 'ajv';
|
||||
import { JSONPath } from 'jsonpath-plus';
|
||||
import { Logger } from 'noicejs';
|
||||
|
||||
import { RuleChange, RuleError, RuleResult } from '../rule/index.js';
|
||||
import { Document, Element } from '../source.js';
|
||||
|
||||
export interface RuleOptions {
|
||||
coerce: boolean;
|
||||
|
@ -64,7 +61,7 @@ export class VisitorContext implements VisitorContextOptions, RuleResult {
|
|||
}
|
||||
|
||||
/**
|
||||
* @todo what is data? should use the element
|
||||
* @TODO what is data? should use the element
|
||||
*/
|
||||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
||||
public mergeResult(other: RuleResult, data: any = {}): this {
|
||||
|
@ -78,30 +75,4 @@ export class VisitorContext implements VisitorContextOptions, RuleResult {
|
|||
})));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @TODO move to visitor
|
||||
*/
|
||||
public pick(path: string, root: Document): Array<Element> {
|
||||
const items = JSONPath({
|
||||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
||||
json: root.data as any,
|
||||
path,
|
||||
});
|
||||
|
||||
this.logger.debug({
|
||||
items,
|
||||
path,
|
||||
}, 'path query picked items');
|
||||
|
||||
if (hasItems(items)) {
|
||||
return items.filter(doesExist).map((data, index) => ({
|
||||
data,
|
||||
document: root,
|
||||
index,
|
||||
}));
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,11 +4,6 @@ import EventEmitter from 'events';
|
|||
import { Rule, RuleChange, RuleError, RuleResult } from '../rule/index.js';
|
||||
import { Document, Element } from '../source.js';
|
||||
|
||||
/**
|
||||
* @todo what does this need to contain? accumulated errors?
|
||||
* yes: since visit is called for each rule, this is what collects
|
||||
* results across all of the rules
|
||||
*/
|
||||
export interface Context {
|
||||
compile(schema: object): ValidateFunction;
|
||||
|
||||
|
|
Loading…
Reference in New Issue