Effect CommunityEC
Effect Community2y ago
3 replies
HerbCaudill

Understanding Validation

I was a little surprised by how validation works and I just want to confirm that I've actually understood it.

const schema = S.Date
const validate = S.validateSync(schema)

// I use this to confirm that a value is already the target type
const d = new Date('2024-06-11')
expect(validate(d)).toBe(d)

// If it's not, an error is thrown
expect(() => validate({ 'this is not': 'a date' })).toThrow()

// this doesn't work the way I thought, which that you'd use it to tell whether something could be decoded:
// expect(validate('1970-01-01T00:00:00.000Z')).toEqual(true)

// I think the way to actually validate is just to try to decode
const decode = S.decodeUnknownSync(schema)
expect(decode('1970-01-01T00:00:00.000Z')).toBeInstanceOf(Date)
expect(() => decode('asdf')).toThrow()
Was this page helpful?