Skip to content

Commit

Permalink
Change signature of Validator to be consistent with .finished.* _Ev…
Browse files Browse the repository at this point in the history
…ents_
  • Loading branch information
igorkamyshev committed Nov 24, 2022
1 parent dbd6fee commit d6ab20b
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 44 deletions.
5 changes: 5 additions & 0 deletions .changeset/fair-onions-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@farfetched/core': minor
---

Change signature of Validator to be consistent with `.finished.*` _Events_
11 changes: 6 additions & 5 deletions apps/website/docs/api/primitives/validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@ A _Validator_ have to respond with `ValidationResult` that could be one of the f

### Function

_Validator_ can be a simple function that accepts `data` and `params` and returns `ValidationResult`.
_Validator_ can be a simple function that accepts `{ result, params }` object and returns `ValidationResult`.

```ts
const validator = (data, params): ValidationResult => data[params.id] !== null;
const validator = ({ result, params }): ValidationResult =>
result[params.id] !== null;
```

### Function with external source

_Validator_ can be an object with field `source` which is any [_Store_](https://effector.dev/docs/api/effector/store) and `fn` witch is a function that accepts `data`, `params`, value of `source` and returns `ValidationResult`.
_Validator_ can be an object with field `source` which is any [_Store_](https://effector.dev/docs/api/effector/store) and `fn` witch is a function that accepts object `{ result, params }` and the value of `source` and returns `ValidationResult`.

```ts
const validator = {
source: $externalStore,
fn: (data, params, externalSource): ValidationResult =>
data[params.id] !== null && data[externalSource.id] !== null,
fn: ({ result, params }, externalSource): ValidationResult =>
result[params.id] !== null && result[externalSource.id] !== null,
};
```
38 changes: 38 additions & 0 deletions apps/website/docs/releases/0-4.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,41 @@ query.finished.success.watch(({ result }) => {
// ...
});
```

### Change signature of Validator

`Validator` now accepts `{ result, params }` object instead of two separate arguments to be consistent with `.finished.*` [_Events_](https://effector.dev/docs/api/effector/event).

#### Function form

```ts
// before
const validator = (result, params) => {
// ...
};

// after
const validator = ({ result, params }) => {
// ...
};
```

#### Object form

```ts
// before
const validator = {
source: $externalStore,
fn: (result, params, externalSource) => {
// ...
},
};

// after
const validator = {
source: $externalStore,
fn: ({ result, params }, externalSource) => {
// ...
},
};
```
2 changes: 1 addition & 1 deletion apps/website/docs/tutorial/validators.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const blockQuery = createQuery({

return response.json();
}),
validate: (data, { id }) => data.id === id,
validate: ({ result }, { id }) => result.id === id,
});
```

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/misc/sourced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { combine, createStore, Event, is, sample, Store } from 'effector';

// -- Main case --

type Callback<Data, Result> = (data: Data) => Result;
export type Callback<Data, Result> = (data: Data) => Result;

type CallbackWithSource<Data, Result, Source> = {
export type CallbackWithSource<Data, Result, Source> = {
source: Store<Source>;
fn: (data: Data, source: Source) => Result;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ optional_validation_field: {
request: { url: '', method: 'GET' },
response: {
contract,
validate: (data, params) => {
expectType<number>(data);
validate: ({ result, params }) => {
expectType<number>(result);
expectType<void>(params);

return true;
Expand All @@ -151,8 +151,8 @@ optional_validation_field: {
contract,
validate: {
source: createStore<boolean>(false),
fn: (data, params, s) => {
expectType<number>(data);
fn: ({ result, params }, s) => {
expectType<number>(result);
expectType<void>(params);
expectType<boolean>(s);

Expand All @@ -171,8 +171,8 @@ optional_validation_field: {
request: { url: '', method: 'GET' },
response: {
contract,
validate: (data, params) => {
expectType<number>(data);
validate: ({ result, params }) => {
expectType<number>(result);
expectType<string>(params);

return true;
Expand All @@ -187,8 +187,8 @@ optional_validation_field: {
contract,
validate: {
source: createStore<boolean>(false),
fn: (data, params, s) => {
expectType<number>(data);
fn: ({ result, params }, s) => {
expectType<number>(result);
expectType<string>(params);
expectType<boolean>(s);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ no_params: {
},
response: {
contract: numberContract,
validate: (a, b) => {
expectType<number>(a);
expectType<void>(b);
validate: ({ result, params }) => {
expectType<number>(result);
expectType<void>(params);
return true;
},
},
Expand All @@ -34,9 +34,9 @@ no_params: {
contract: numberContract,
validate: {
source: createStore(false),
fn: (a, b, s) => {
expectType<number>(a);
expectType<void>(b);
fn: ({ result, params }, s) => {
expectType<number>(result);
expectType<void>(params);
expectType<boolean>(s);
return true;
},
Expand All @@ -57,9 +57,9 @@ params: {
},
response: {
contract: numberContract,
validate: (a, b) => {
expectType<number>(a);
expectType<string>(b);
validate: ({ result, params }) => {
expectType<number>(result);
expectType<string>(params);
return true;
},
},
Expand All @@ -76,9 +76,9 @@ params: {
contract: numberContract,
validate: {
source: createStore(false),
fn: (a, b, s) => {
expectType<number>(a);
expectType<string>(b);
fn: ({ result, params }, s) => {
expectType<number>(result);
expectType<string>(params);
expectType<boolean>(s);
return true;
},
Expand Down
16 changes: 7 additions & 9 deletions packages/core/src/remote_operation/create_remote_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,13 @@ function createRemoteOperation<
const { validDataRecieved, __: invalidDataRecieved } = split(
sample({
clock: applyContractFx.done,
source: normalizeSourced(
reduceTwoArgs({
field: validate ?? validValidator,
clock: applyContractFx.done.map(({ result, params }) => [
result,
params.params, // Extract original params, it is params of params
]),
})
),
source: normalizeSourced({
field: validate ?? validValidator,
clock: applyContractFx.done.map(({ result, params }) => ({
result,
params: params.params, // Extract original params, it is params of params
})),
}),
fn: (validation, { params, result: data }) => ({
data,
// Extract original params, it is params of params
Expand Down
15 changes: 8 additions & 7 deletions packages/core/src/validation/type.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { TwoArgsDynamicallySourcedField } from '../misc/sourced';
import { Callback, CallbackWithSource } from '../misc/sourced';

type ValidationResult = boolean | string | string[];

type Validator<Data, Params, ValidationSource> = TwoArgsDynamicallySourcedField<
Data,
Params,
ValidationResult,
ValidationSource
>;
type Validator<Data, Params, ValidationSource> =
| Callback<{ result: Data; params: Params }, ValidationResult>
| CallbackWithSource<
{ result: Data; params: Params },
ValidationResult,
ValidationSource
>;

export { Validator, ValidationResult };

0 comments on commit d6ab20b

Please sign in to comment.