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

[Question] How to implement a codec that works with any array or string codecs? #673

Open
arthurgubaidullin opened this issue Nov 2, 2022 · 0 comments

Comments

@arthurgubaidullin
Copy link

arthurgubaidullin commented Nov 2, 2022

I want to be able to chain codecs in a pipeline.

That doesn't work.

import {pipe} from 'fp-ts/function';
import * as t from 'io-ts';
import {
  NonEmptyArrayC,
  readonlyNonEmptyArray,
  ReadonlyNonEmptyArrayC,
} from 'io-ts-types';

type LenghtUpTo1000Brand = {
  readonly LenghtUpTo1000: unique symbol;
};

const LengthUpTo1000 = <
  C extends
    | t.ArrayType<t.Mixed>
    | t.ReadonlyArrayType<t.Mixed>
    | NonEmptyArrayC<t.Mixed>
    | ReadonlyNonEmptyArrayC<t.Mixed>
>(
  codec: C
) =>
  t.brand(
    codec,
    (a): a is t.Branded<t.TypeOf<C>, LenghtUpTo1000Brand> => a.length <= 1000,
    'LenghtUpTo1000'
  );

export const DataCodec = pipe(t.array(t.string), LengthUpTo1000);

export const DataCodec2 = pipe(t.readonlyArray(t.string), LengthUpTo1000);

export const DataCodec3 = pipe(readonlyNonEmptyArray(t.string), LengthUpTo1000);  // <- error

Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant