github Effect-TS/effect effect@3.11.3

Patch Changes

  • #4080 90906f7 Thanks @gcanti! - Fix the Schema.TemplateLiteral output type when the arguments include a branded type.

    Before

    import { Schema } from "effect"
    
    const schema = Schema.TemplateLiteral(
      "a ",
      Schema.String.pipe(Schema.brand("MyBrand"))
    )
    
    // type Type = `a ${Schema.brand<typeof Schema.String, "MyBrand"> & string}`
    // | `a ${Schema.brand<typeof Schema.String, "MyBrand"> & number}`
    // | `a ${Schema.brand<typeof Schema.String, "MyBrand"> & bigint}`
    // | `a ${Schema.brand<...> & false}`
    // | `a ${Schema.brand<...> & true}`
    type Type = typeof schema.Type

    After

    import { Schema } from "effect"
    
    const schema = Schema.TemplateLiteral(
      "a ",
      Schema.String.pipe(Schema.brand("MyBrand"))
    )
    
    // type Type = `a ${string & Brand<"MyBrand">}`
    type Type = typeof schema.Type
  • #4076 3862cd3 Thanks @gcanti! - Schema: fix bug in Schema.TemplateLiteralParser resulting in a runtime error.

    Before

    import { Schema } from "effect"
    
    const schema = Schema.TemplateLiteralParser("a", "b")
    // throws TypeError: Cannot read properties of undefined (reading 'replace')

    After

    import { Schema } from "effect"
    
    const schema = Schema.TemplateLiteralParser("a", "b")
    
    console.log(Schema.decodeUnknownSync(schema)("ab"))
    // Output: [ 'a', 'b' ]
  • #4076 3862cd3 Thanks @gcanti! - SchemaAST: fix TemplateLiteral model.

    Added Literal and Union as valid types.

  • #4083 343b6aa Thanks @gcanti! - Preserve MissingMessageAnnotations on property signature declarations when another field is a property signature transformation.

    Before

    import { Console, Effect, ParseResult, Schema } from "effect"
    
    const schema = Schema.Struct({
      a: Schema.propertySignature(Schema.String).annotations({
        missingMessage: () => "message1"
      }),
      b: Schema.propertySignature(Schema.String)
        .annotations({ missingMessage: () => "message2" })
        .pipe(Schema.fromKey("c")), // <= transformation
      d: Schema.propertySignature(Schema.String).annotations({
        missingMessage: () => "message3"
      })
    })
    
    Effect.runPromiseExit(
      Schema.decodeUnknown(schema, { errors: "all" })({}).pipe(
        Effect.tapError((error) =>
          Console.log(ParseResult.ArrayFormatter.formatErrorSync(error))
        )
      )
    )
    /*
    Output:
    [
      { _tag: 'Missing', path: [ 'a' ], message: 'is missing' }, // <= wrong
      { _tag: 'Missing', path: [ 'c' ], message: 'message2' },
      { _tag: 'Missing', path: [ 'd' ], message: 'is missing' } // <= wrong
    ]
    */

    After

    import { Console, Effect, ParseResult, Schema } from "effect"
    
    const schema = Schema.Struct({
      a: Schema.propertySignature(Schema.String).annotations({
        missingMessage: () => "message1"
      }),
      b: Schema.propertySignature(Schema.String)
        .annotations({ missingMessage: () => "message2" })
        .pipe(Schema.fromKey("c")), // <= transformation
      d: Schema.propertySignature(Schema.String).annotations({
        missingMessage: () => "message3"
      })
    })
    
    Effect.runPromiseExit(
      Schema.decodeUnknown(schema, { errors: "all" })({}).pipe(
        Effect.tapError((error) =>
          Console.log(ParseResult.ArrayFormatter.formatErrorSync(error))
        )
      )
    )
    /*
    Output:
    [
      { _tag: 'Missing', path: [ 'a' ], message: 'message1' },
      { _tag: 'Missing', path: [ 'c' ], message: 'message2' },
      { _tag: 'Missing', path: [ 'd' ], message: 'message3' }
    ]
    */
  • #4081 afba339 Thanks @gcanti! - Fix the behavior of Schema.TemplateLiteralParser when the arguments include literals other than string literals.

    Before

    import { Schema } from "effect"
    
    const schema = Schema.TemplateLiteralParser(Schema.String, 1)
    
    console.log(Schema.decodeUnknownSync(schema)("a1"))
    /*
    throws
    ParseError: (`${string}1` <-> readonly [string, 1])
    └─ Type side transformation failure
       └─ readonly [string, 1]
          └─ [1]
             └─ Expected 1, actual "1"
    */

    After

    import { Schema } from "effect"
    
    const schema = Schema.TemplateLiteralParser(Schema.String, 1)
    
    console.log(Schema.decodeUnknownSync(schema)("a1"))
    // Output: [ 'a', 1 ]

Don't miss a new effect release

NewReleases is sending notifications on new releases.