Skip to content

Commit

Permalink
feat: allow to use an Action Selector with only a fn callback. path i…
Browse files Browse the repository at this point in the history
…s now optional
  • Loading branch information
emyann committed Jul 10, 2019
1 parent 1ebaa57 commit 33d0d2e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/MorphismTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ export class MorphismSchemaTree<Target, Source> {
parentKeyPath = parentKeyPath ? `${parentKeyPath}.${actionKey}` : actionKey;
} else {
if (actionKey) {
if (isEmptyObject(partialSchema as any))
throw new Error(
`A value of a schema property can't be an empty object. Value ${JSON.stringify(partialSchema)} found for property ${actionKey}`
);
// check if actionKey exists to verify if not root node
this.add({ propertyName: actionKey, action: null }, parentKeyPath);
parentKeyPath = parentKeyPath ? `${parentKeyPath}.${actionKey}` : actionKey;
Expand Down Expand Up @@ -214,10 +218,14 @@ export class MorphismSchemaTree<Target, Source> {
// Action<Object>: a path and a function: [ destination : { path: 'source', fn:(fieldValue, items) }]
return ({ object, items, objectToCompute }) => {
let result;
if (Array.isArray(action.path)) {
result = aggregator(action.path, object);
} else if (isString(action.path)) {
result = get(object, action.path);
if (action.path) {
if (Array.isArray(action.path)) {
result = aggregator(action.path, object);
} else if (isString(action.path)) {
result = get(object, action.path);
}
} else {
result = object;
}

if (action.fn) {
Expand Down
22 changes: 22 additions & 0 deletions src/morphism.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,28 @@ describe('Morphism', () => {
expect(errors.length).toBe(1);
}
});

it('should allow to use an action selector with a `fn` callback only', () => {
interface Target {
t1: string;
}
const schema = createSchema<Target>({ t1: { fn: value => value.s1 } });
const result = morphism(schema, { s1: 'value' });
expect(result.t1).toEqual('value');
});

it('should allow to use an action selector with a `fn` callback only along with validation options', () => {
interface Target {
t1: string;
}
const schema = createSchema<Target>({ t1: { fn: value => value.s1, type: string } });
const result = morphism(schema, { s1: 1234 });
const errors = reporter.report(result);
expect(errors).not.toBeNull();
if (errors) {
expect(errors.length).toBe(1);
}
});
});
describe('Function Predicate', function() {
it('should support es6 destructuring as function predicate', function() {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export type ActionAggregator<T extends unknown = unknown> = T extends object ? (
*
*/
export interface ActionSelector<Source = object, R = any> {
path: ActionString<Source> | ActionAggregator<Source>;
path?: ActionString<Source> | ActionAggregator<Source>;
// | ((source: Source) => unknown);
fn?: (fieldValue: any, object: Source, items: Source, objectToCompute: R) => R;
type?: BasicTypes;
Expand Down

0 comments on commit 33d0d2e

Please sign in to comment.