Skip to content

Commit 0805fbb

Browse files
committed
PR 1762
1 parent 1c46094 commit 0805fbb

File tree

10 files changed

+233
-0
lines changed

10 files changed

+233
-0
lines changed

docs/modules/Either.ts.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,22 +1043,96 @@ Added in v3.0.0
10431043
10441044
## getAltValidation
10451045
1046+
The default [`Alt`](#alt) instance returns the last error, if you want to
1047+
get all errors you need to provide an way to concatenate them via a `Semigroup`.
1048+
10461049
**Signature**
10471050
10481051
```ts
10491052
export declare const getAltValidation: <E>(S: Semigroup<E>) => Alt2C<'Either', E>
10501053
```
10511054
1055+
**Example**
1056+
1057+
```ts
1058+
import * as E from 'fp-ts/Either'
1059+
import { pipe } from 'fp-ts/function'
1060+
import * as S from 'fp-ts/Semigroup'
1061+
import * as string from 'fp-ts/string'
1062+
1063+
const parseString = (u: unknown): E.Either<string, string> =>
1064+
typeof u === 'string' ? E.right(u) : E.left('not a string')
1065+
1066+
const parseNumber = (u: unknown): E.Either<string, number> =>
1067+
typeof u === 'number' ? E.right(u) : E.left('not a number')
1068+
1069+
const parse = (u: unknown): E.Either<string, string | number> =>
1070+
pipe(
1071+
parseString(u),
1072+
E.alt<string, string | number>(() => parseNumber(u))
1073+
)
1074+
1075+
assert.deepStrictEqual(parse(true), E.left('not a number')) // <= last error
1076+
1077+
const Alt = E.getAltValidation(pipe(string.Semigroup, S.intercalate(', ')))
1078+
1079+
const parseAll = (u: unknown): E.Either<string, string | number> =>
1080+
pipe(
1081+
parseString(u),
1082+
Alt.alt<string | number>(() => parseNumber(u))
1083+
)
1084+
1085+
assert.deepStrictEqual(parseAll(true), E.left('not a string, not a number')) // <= all errors
1086+
```
1087+
10521088
Added in v3.0.0
10531089

10541090
## getApplicativeValidation
10551091

1092+
The default [`Applicative`](#applicative) instance returns the first error, if you want to
1093+
get all errors you need to provide an way to concatenate them via a `Semigroup`.
1094+
10561095
**Signature**
10571096

10581097
```ts
10591098
export declare const getApplicativeValidation: <E>(S: Semigroup<E>) => Applicative2C<'Either', E>
10601099
```
10611100
1101+
**Example**
1102+
1103+
```ts
1104+
import * as A from 'fp-ts/Apply'
1105+
import * as E from 'fp-ts/Either'
1106+
import { pipe } from 'fp-ts/function'
1107+
import * as S from 'fp-ts/Semigroup'
1108+
import * as string from 'fp-ts/string'
1109+
1110+
const parseString = (u: unknown): E.Either<string, string> =>
1111+
typeof u === 'string' ? E.right(u) : E.left('not a string')
1112+
1113+
const parseNumber = (u: unknown): E.Either<string, number> =>
1114+
typeof u === 'number' ? E.right(u) : E.left('not a number')
1115+
1116+
interface Person {
1117+
readonly name: string
1118+
readonly age: number
1119+
}
1120+
1121+
const parsePerson = (input: Record<string, unknown>): E.Either<string, Person> =>
1122+
pipe(E.Do, E.apS('name', parseString(input.name)), E.apS('age', parseNumber(input.age)))
1123+
1124+
assert.deepStrictEqual(parsePerson({}), E.left('not a string')) // <= first error
1125+
1126+
const Applicative = E.getApplicativeValidation(pipe(string.Semigroup, S.intercalate(', ')))
1127+
1128+
const apS = A.apS(Applicative)
1129+
1130+
const parsePersonAll = (input: Record<string, unknown>): E.Either<string, Person> =>
1131+
pipe(E.Do, apS('name', parseString(input.name)), apS('age', parseNumber(input.age)))
1132+
1133+
assert.deepStrictEqual(parsePersonAll({}), E.left('not a string, not a number')) // <= all errors
1134+
```
1135+
10621136
Added in v3.0.0
10631137

10641138
## getCompactable

docs/modules/IOEither.ts.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,11 @@ Added in v3.0.0
901901
902902
## getAltIOValidation
903903
904+
The default [`Alt`](#alt) instance returns the last error, if you want to
905+
get all errors you need to provide an way to concatenate them via a `Semigroup`.
906+
907+
See [`getAltValidation`](./Either.ts.html#getaltvalidation).
908+
904909
**Signature**
905910
906911
```ts
@@ -911,6 +916,11 @@ Added in v3.0.0
911916
912917
## getApplicativeIOValidation
913918
919+
The default [`ApplicativePar`](#applicativepar) instance returns the first error, if you want to
920+
get all errors you need to provide an way to concatenate them via a `Semigroup`.
921+
922+
See [`getApplicativeValidation`](./Either.ts.html#getapplicativevalidation).
923+
914924
**Signature**
915925
916926
```ts

docs/modules/ReaderEither.ts.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,11 @@ Added in v3.0.0
997997
998998
## getAltReaderValidation
999999
1000+
The default [`Alt`](#alt) instance returns the last error, if you want to
1001+
get all errors you need to provide an way to concatenate them via a `Semigroup`.
1002+
1003+
See [`getAltValidation`](./Either.ts.html#getaltvalidation).
1004+
10001005
**Signature**
10011006
10021007
```ts
@@ -1007,6 +1012,11 @@ Added in v3.0.0
10071012
10081013
## getApplicativeReaderValidation
10091014
1015+
The default [`Applicative`](#applicative) instance returns the first error, if you want to
1016+
get all errors you need to provide an way to concatenate them via a `Semigroup`.
1017+
1018+
See [`getApplicativeValidation`](./Either.ts.html#getapplicativevalidation).
1019+
10101020
**Signature**
10111021
10121022
```ts

docs/modules/ReaderTaskEither.ts.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,11 @@ Added in v3.0.0
14641464
14651465
## getAltReaderTaskValidation
14661466
1467+
The default [`Alt`](#alt) instance returns the last error, if you want to
1468+
get all errors you need to provide an way to concatenate them via a `Semigroup`.
1469+
1470+
See [`getAltValidation`](./Either.ts.html#getaltvalidation).
1471+
14671472
**Signature**
14681473
14691474
```ts
@@ -1474,6 +1479,11 @@ Added in v3.0.0
14741479
14751480
## getApplicativeReaderTaskValidation
14761481
1482+
The default [`ApplicativePar`](#applicativepar) instance returns the first error, if you want to
1483+
get all errors you need to provide an way to concatenate them via a `Semigroup`.
1484+
1485+
See [`getApplicativeValidation`](./Either.ts.html#getapplicativevalidation).
1486+
14771487
**Signature**
14781488
14791489
```ts

docs/modules/TaskEither.ts.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,11 @@ Added in v3.0.0
11381138
11391139
## getAltTaskValidation
11401140
1141+
The default [`Alt`](#alt) instance returns the last error, if you want to
1142+
get all errors you need to provide an way to concatenate them via a `Semigroup`.
1143+
1144+
See [`getAltValidation`](./Either.ts.html#getaltvalidation).
1145+
11411146
**Signature**
11421147
11431148
```ts
@@ -1148,6 +1153,11 @@ Added in v3.0.0
11481153
11491154
## getApplicativeTaskValidation
11501155
1156+
The default [`ApplicativePar`](#applicativepar) instance returns the first error, if you want to
1157+
get all errors you need to provide an way to concatenate them via a `Semigroup`.
1158+
1159+
See [`getApplicativeValidation`](./Either.ts.html#getapplicativevalidation).
1160+
11511161
**Signature**
11521162
11531163
```ts

src/Either.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,55 @@ export const Applicative: Applicative2<URI> = {
865865
}
866866

867867
/**
868+
* The default [`Applicative`](#applicative) instance returns the first error, if you want to
869+
* get all errors you need to provide an way to concatenate them via a `Semigroup`.
870+
*
871+
* @example
872+
* import * as A from 'fp-ts/Apply'
873+
* import * as E from 'fp-ts/Either'
874+
* import { pipe } from 'fp-ts/function'
875+
* import * as S from 'fp-ts/Semigroup'
876+
* import * as string from 'fp-ts/string'
877+
*
878+
* const parseString = (u: unknown): E.Either<string, string> =>
879+
* typeof u === 'string' ? E.right(u) : E.left('not a string')
880+
*
881+
* const parseNumber = (u: unknown): E.Either<string, number> =>
882+
* typeof u === 'number' ? E.right(u) : E.left('not a number')
883+
*
884+
* interface Person {
885+
* readonly name: string
886+
* readonly age: number
887+
* }
888+
*
889+
* const parsePerson = (
890+
* input: Record<string, unknown>
891+
* ): E.Either<string, Person> =>
892+
* pipe(
893+
* E.Do,
894+
* E.apS('name', parseString(input.name)),
895+
* E.apS('age', parseNumber(input.age))
896+
* )
897+
*
898+
* assert.deepStrictEqual(parsePerson({}), E.left('not a string')) // <= first error
899+
*
900+
* const Applicative = E.getApplicativeValidation(
901+
* pipe(string.Semigroup, S.intercalate(', '))
902+
* )
903+
*
904+
* const apS = A.apS(Applicative)
905+
*
906+
* const parsePersonAll = (
907+
* input: Record<string, unknown>
908+
* ): E.Either<string, Person> =>
909+
* pipe(
910+
* E.Do,
911+
* apS('name', parseString(input.name)),
912+
* apS('age', parseNumber(input.age))
913+
* )
914+
*
915+
* assert.deepStrictEqual(parsePersonAll({}), E.left('not a string, not a number')) // <= all errors
916+
*
868917
* @category instances
869918
* @since 3.0.0
870919
*/
@@ -954,6 +1003,36 @@ export const Alt: Alt2<URI> = {
9541003
}
9551004

9561005
/**
1006+
* The default [`Alt`](#alt) instance returns the last error, if you want to
1007+
* get all errors you need to provide an way to concatenate them via a `Semigroup`.
1008+
*
1009+
* @example
1010+
* import * as E from 'fp-ts/Either'
1011+
* import { pipe } from 'fp-ts/function'
1012+
* import * as S from 'fp-ts/Semigroup'
1013+
* import * as string from 'fp-ts/string'
1014+
*
1015+
* const parseString = (u: unknown): E.Either<string, string> =>
1016+
* typeof u === 'string' ? E.right(u) : E.left('not a string')
1017+
*
1018+
* const parseNumber = (u: unknown): E.Either<string, number> =>
1019+
* typeof u === 'number' ? E.right(u) : E.left('not a number')
1020+
*
1021+
* const parse = (u: unknown): E.Either<string, string | number> =>
1022+
* pipe(
1023+
* parseString(u),
1024+
* E.alt<string, string | number>(() => parseNumber(u))
1025+
* )
1026+
*
1027+
* assert.deepStrictEqual(parse(true), E.left('not a number')) // <= last error
1028+
*
1029+
* const Alt = E.getAltValidation(pipe(string.Semigroup, S.intercalate(', ')))
1030+
*
1031+
* const parseAll = (u: unknown): E.Either<string, string | number> =>
1032+
* pipe(parseString(u), Alt.alt<string | number>(() => parseNumber(u)))
1033+
*
1034+
* assert.deepStrictEqual(parseAll(true), E.left('not a string, not a number')) // <= all errors
1035+
*
9571036
* @category instances
9581037
* @since 3.0.0
9591038
*/

src/IOEither.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,11 @@ declare module './HKT' {
415415
}
416416

417417
/**
418+
* The default [`ApplicativePar`](#applicativepar) instance returns the first error, if you want to
419+
* get all errors you need to provide an way to concatenate them via a `Semigroup`.
420+
*
421+
* See [`getApplicativeValidation`](./Either.ts.html#getapplicativevalidation).
422+
*
418423
* @category instances
419424
* @since 3.0.0
420425
*/
@@ -425,6 +430,11 @@ export const getApplicativeIOValidation = <E>(S: Semigroup<E>): Applicative2C<UR
425430
})
426431

427432
/**
433+
* The default [`Alt`](#alt) instance returns the last error, if you want to
434+
* get all errors you need to provide an way to concatenate them via a `Semigroup`.
435+
*
436+
* See [`getAltValidation`](./Either.ts.html#getaltvalidation).
437+
*
428438
* @category instances
429439
* @since 3.0.0
430440
*/

src/ReaderEither.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,11 @@ declare module './HKT' {
424424
}
425425

426426
/**
427+
* The default [`Applicative`](#applicative) instance returns the first error, if you want to
428+
* get all errors you need to provide an way to concatenate them via a `Semigroup`.
429+
*
430+
* See [`getApplicativeValidation`](./Either.ts.html#getapplicativevalidation).
431+
*
427432
* @category instances
428433
* @since 3.0.0
429434
*/
@@ -434,6 +439,11 @@ export const getApplicativeReaderValidation = <E>(S: Semigroup<E>): Applicative3
434439
})
435440

436441
/**
442+
* The default [`Alt`](#alt) instance returns the last error, if you want to
443+
* get all errors you need to provide an way to concatenate them via a `Semigroup`.
444+
*
445+
* See [`getAltValidation`](./Either.ts.html#getaltvalidation).
446+
*
437447
* @category instances
438448
* @since 3.0.0
439449
*/

src/ReaderTaskEither.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,11 @@ declare module './HKT' {
682682
}
683683

684684
/**
685+
* The default [`ApplicativePar`](#applicativepar) instance returns the first error, if you want to
686+
* get all errors you need to provide an way to concatenate them via a `Semigroup`.
687+
*
688+
* See [`getApplicativeValidation`](./Either.ts.html#getapplicativevalidation).
689+
*
685690
* @category instances
686691
* @since 3.0.0
687692
*/
@@ -692,6 +697,11 @@ export const getApplicativeReaderTaskValidation = <E>(A: Apply1<T.URI>, S: Semig
692697
})
693698

694699
/**
700+
* The default [`Alt`](#alt) instance returns the last error, if you want to
701+
* get all errors you need to provide an way to concatenate them via a `Semigroup`.
702+
*
703+
* See [`getAltValidation`](./Either.ts.html#getaltvalidation).
704+
*
695705
* @category instances
696706
* @since 3.0.0
697707
*/

src/TaskEither.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,11 @@ declare module './HKT' {
636636
}
637637

638638
/**
639+
* The default [`ApplicativePar`](#applicativepar) instance returns the first error, if you want to
640+
* get all errors you need to provide an way to concatenate them via a `Semigroup`.
641+
*
642+
* See [`getApplicativeValidation`](./Either.ts.html#getapplicativevalidation).
643+
*
639644
* @category instances
640645
* @since 3.0.0
641646
*/
@@ -646,6 +651,11 @@ export const getApplicativeTaskValidation = <E>(A: Apply1<T.URI>, S: Semigroup<E
646651
})
647652

648653
/**
654+
* The default [`Alt`](#alt) instance returns the last error, if you want to
655+
* get all errors you need to provide an way to concatenate them via a `Semigroup`.
656+
*
657+
* See [`getAltValidation`](./Either.ts.html#getaltvalidation).
658+
*
649659
* @category instances
650660
* @since 3.0.0
651661
*/

0 commit comments

Comments
 (0)