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

feat: add two new types #6

Merged
merged 2 commits into from Mar 16, 2022
Merged

feat: add two new types #6

merged 2 commits into from Mar 16, 2022

Conversation

ForbesLindesay
Copy link
Owner

No description provided.

@rollingversions
Copy link

rollingversions bot commented Mar 16, 2022

Change Log for funtypes-schemas (1.0.1 → 1.1.0)

New Features

  • Added ChainCodecs

    Chain multiple codecs together to combine parsers, for example you can parse a Base64 encoded JSON object:

    import {deepEqual} from 'assert';
    
    import * as t from 'funtypes';
    import * as s from 'funtypes-schemas';
    
    const MySchema = s.ChainCodecs(
      s.ParsedBase64String(),
      s.ParsedJsonString(t.Object({value: s.Integer()})),
    );
    
    // ✅ Valid:
    deepEqual(assertMySchema.parse('eyJ2YWx1ZSI6NDJ9'), {value: 42});
    
    // ✅ Valid:
    deepEqual(assertMySchema.serialize({value: 42}), 'eyJ2YWx1ZSI6NDJ9');

    You can pass as many codecs as you like as parameters to ChainCodecs. They will be applied in order when parsing and in reverse order when serializing.

  • Added ParsedJsonString

    Transparently parse/serialize to/from JSON. A codec can optionally be provided to handle the parsed value.

    ✅ Valid:

    import {deepEqual} from 'assert';
    
    import * as t from 'funtypes';
    import * as s from 'funtypes-schemas';
    
    const MySchema = s.ParsedJsonString(
      t.Object({
        level: s.ParsedIntegerString(),
      }),
    );
    
    deepEqual(MySchema.parse(`{"level": "3"}`), {
      level: 3,
    });
    
    deepEqual(
      MySchema.serialize({
        level: 3,
      }),
      `{"level":"3"}`,
    );

    🚨 Invalid:

    import * as t from 'funtypes';
    import * as s from 'funtypes-schemas';
    
    const MySchema = s.ParsedJsonString(
      t.Object({
        level: s.ParsedIntegerString(),
      }),
    );
    
    // The next line throws an error because the string is not valid JSON:
    MySchema.parse(`{level: '3'}`);
    
    // The next line throws an error because the value is not an integer:
    MySchema.parse(`{"level": "3.14"}`);
    
    // The next line throws an error because the value is not an integer:
    MySchema.serialize({
      level: 3.14,
    });

Edit changelog

@ForbesLindesay ForbesLindesay enabled auto-merge (squash) March 16, 2022 14:56
@ForbesLindesay ForbesLindesay merged commit 668db23 into main Mar 16, 2022
@ForbesLindesay ForbesLindesay deleted the feat/fl/new-types branch March 16, 2022 16:03
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

Successfully merging this pull request may close these issues.

None yet

1 participant