Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add options.from to ResolveOptions #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Condition.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import isSchema from './util/isSchema';
import Reference from './Reference';
import type { ISchema } from './types';
import type { Ancester, ISchema } from './types';

export type ConditionBuilder<T extends ISchema<any, any>> = (
values: any[],
Expand All @@ -18,6 +18,7 @@ export type ResolveOptions<TContext = any> = {
value?: any;
parent?: any;
context?: TContext;
from?: Ancester<TContext>[];
};

class Condition<TIn extends ISchema<any, any> = ISchema<any, any>> {
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import TupleSchema, { create as tupleCreate } from './tuple';
import { create as refCreate } from './Reference';
import { create as lazyCreate } from './Lazy';
import ValidationError from './ValidationError';
import reach, { getIn } from './util/reach';
import reach, { getIn, reachAndResolve } from './util/reach';
import isSchema from './util/isSchema';
import printValue from './util/printValue';
import setLocale, { LocaleObject } from './setLocale';
Expand Down Expand Up @@ -99,6 +99,7 @@ export {
lazyCreate as lazy,
tupleCreate as tuple,
reach,
reachAndResolve,
getIn,
isSchema,
printValue,
Expand Down
8 changes: 7 additions & 1 deletion src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,17 @@ export default class ObjectSchema<
let props = ([] as string[]).concat(
this._nodes,
Object.keys(value).filter((v) => !this._nodes.includes(v)),
);
);

let intermediateValue: Record<string, unknown> = {}; // is filled during the transform below
let intermediateFrom = options.from ? [
{...options.from[0], value: intermediateValue },
...options.from.slice(1)
] : [];
let innerOptions: InternalOptions<TContext> = {
...options,
parent: intermediateValue,
from: intermediateFrom,
__validating: options.__validating || false,
};

Expand All @@ -191,6 +196,7 @@ export default class ObjectSchema<
value: inputValue,
context: options.context,
parent: intermediateValue,
from: innerOptions.from
});

let fieldSpec = field instanceof Schema ? field.spec : undefined;
Expand Down
35 changes: 30 additions & 5 deletions src/util/reach.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { forEach } from 'property-expr';
import type Reference from '../Reference';
import type { InferType, ISchema } from '../types';
import type { Ancester, InferType, ISchema } from '../types';
import type { Get } from 'type-fest';

export function getIn<C = any>(
Expand All @@ -12,16 +12,22 @@ export function getIn<C = any>(
schema: ISchema<any> | Reference<any>;
parent: any;
parentPath: string;
from: Ancester<C>[];
value: any;
} {
let parent: any, lastPart: string, lastPartDebug: string;
let parent: any,
parentSchema: any,
lastPart: string,
lastPartDebug: string,
from: Ancester<C>[] = [];

// root path: ''
if (!path) return { parent, parentPath: path, schema };
if (!path) return { parent, parentPath: path, schema, from, value };

forEach(path, (_part, isBracket, isArray) => {
let part = isBracket ? _part.slice(1, _part.length - 1) : _part;

schema = schema.resolve({ context, parent, value });
schema = schema.resolve({ context, parent, value, from });

let isTuple = schema.type === 'tuple';
let idx = isArray ? parseInt(part, 10) : 0;
Expand Down Expand Up @@ -54,15 +60,17 @@ export function getIn<C = any>(
);

parent = value;
parentSchema = schema;
value = value && value[part];
schema = schema.fields[part];
from = [{ schema: parentSchema, value: parent }, ...from];
}

lastPart = part;
lastPartDebug = isBracket ? '[' + _part + ']' : '.' + _part;
});

return { schema, parent, parentPath: lastPart! };
return { schema, parent, parentPath: lastPart!, from, value };
}

function reach<P extends string, S extends ISchema<any>>(
Expand All @@ -76,4 +84,21 @@ function reach<P extends string, S extends ISchema<any>>(
return getIn(obj, path, value, context).schema as any;
}

export function reachAndResolve<P extends string, S extends ISchema<any>>(
obj: S,
path: P,
value?: any,
context?: any,
):
| Reference<Get<InferType<S>, P>>
| ISchema<Get<InferType<S>, P>, S['__context']> {
const reachedSchema = getIn(obj, path, value, context);
return reachedSchema.schema.resolve({
context,
parent: reachedSchema.parent,
value: reachedSchema.value,
from: reachedSchema.from,
}) as any;
}

export default reach;