Issue with `Schema.is` and `Schema.validate` when using `Schema.parseJson` in Effect Typescript

Schema.is/validate fails when decode passes when using Schema.parseJson

I ran into a problem with using Schema.is to check if something matches a schema, when the schema uses Schema.parseJson. Turns out that both is and validate will fail on valid into, and decode will pass.

Minimal reproduction:
  const data = '{ "a": 1 }'
  const schema = Schema.parseJson(Schema.Struct({ a: Schema.Number }))

  const result = yield* Effect.all({
    is: Effect.succeed(Schema.is(schema)(data)),
    validate: Schema.validate(schema)(data),
    decode: Schema.decode(schema)(data)
  }, { mode: "either" })

  yield* Effect.log(result)


gives:

{
    is: { _id: 'Either', _tag: 'Right', right: false },
    validate: {
      _id: 'Either',
      _tag: 'Left',
      left: {
        _id: 'ParseError',
        message: 'Expected { readonly a: number }, actual "{ \\"a\\": 1 }"'
      }
    },
    decode: { _id: 'Either', _tag: 'Right', right: { a: 1 } }
  }


Note that the decode is successful (right) and the validate is a failure, (left). is is a success but with false as the result.

From the ParseError output, it appears that the parseJson hasn't been applied.

Note that this will fail with any valid Json, I used the above as its a little clearer to see from the ParseError where the problem lies.

https://effect.website/play/#49db060dbf23
Was this page helpful?