Effect CommunityEC
Effect Community3y ago
4 replies
pseudopirate

Error in code after updating schema version

Hey!
We have updated our schema version to 0.36.3, and the code below started to throw an error
import * as Schema from "@effect/schema/Schema";
import { parseEither } from "@effect/schema/Parser";
import { isLeft } from "@effect/data/Either";

enum Foo {
    Test1 = "test1",
    Test2 = "test2",
}

export const StateSchema = Schema.struct({
    foo: Schema.enums(Foo),
    somestring: Schema.string.pipe(Schema.optional),
  }).pipe(
    Schema.extend(
      Schema.record(
        Schema.templateLiteral(
          Schema.string,
          Schema.literal("-photo-"),
          Schema.number,
        ),
        Schema.string,
      ),
    ),
  );

const state = {
    foo: Foo.Test1,
    somestring: "djhafk",
    "foo-photo-0": "",
    "foo-photo-1": "",
    "bar-photo-0": "",
}

const parsedState = parseEither(StateSchema)(state);

if (isLeft(parsedState)) {
    console.error("ERROR: ", JSON.stringify(parsedState.left, null, 2))
} else {
    console.log("OK: ", parsedState.right)
}

Error:
{
  "_tag": "ParseError",
  "errors": [
    {
      "_tag": "Key",
      "key": "foo",
      "errors": [
        {
          "_tag": "Type",
          "expected": {
            "_tag": "TemplateLiteral",
            "head": "",
            "spans": [
              {
                "type": {
                  "_tag": "StringKeyword",
                  "annotations": {}
                },
                "literal": "-photo-"
              },
              {
                "type": {
                  "_tag": "NumberKeyword",
                  "annotations": {}
                },
                "literal": ""
              }
            ],
            "annotations": {}
          },
          "actual": "foo",
          "message": {
            "_id": "Option",
            "_tag": "None"
          }
        }
      ]
    }
  ]
}

It seems it has changed its behaviour at 0.35.0. I wonder if that was an intentional change and how to rewrite the schema in that case
Was this page helpful?