gcanti / io-ts-types

A collection of codecs and combinators for use with io-ts
https://gcanti.github.io/io-ts-types/
MIT License
311 stars 40 forks source link

nanoid #121

Closed steida closed 4 years ago

steida commented 4 years ago

🚀 Feature request

I suppose nanoid is the state of art of client id generation.

gcanti commented 4 years ago

@steida not sure what are you requesting?

DenisFrezzato commented 4 years ago

I guess he's asking for a nanoid branded type, as there is one for UUID.

gcanti commented 4 years ago

@DenisFrezzato looks like nanoid is a generation tool, not a validator, how is relevant to this library?

DenisFrezzato commented 4 years ago

nanoid is an ID generation tool, as UUID is, so I'm guessing he's asking for a codec for nanoid IDs, like there is one for UUID.

steida commented 4 years ago

NanoID branded type plus smart constructor example with recommended usage for beginners, maybe.

interface NanoIDBrand {
  readonly NanoID: unique symbol;
}
export const NanoID = t.brand(
  t.string,
  (s): s is t.Branded<string, NanoIDBrand> => s.length <= 21, // regex would be better
  'NanoID',
);
export type NanoID = t.TypeOf<typeof NanoID>;
import nanoid from 'nanoid';
import { NanoID } from '../types';

export const createNanoID = () => NanoID.decode(nanoid());