Effect CommunityEC
Effect Community2y ago
7 replies
erikshestopal

Schema Decoding vs Class Constructor Validation in Effect-TS

When using a class constructor as a validator (https://github.com/Effect-TS/effect/blob/main/packages/schema/README.md#class-constructor-as-a-validator), what does Schema do under the hood in order to decode the input? In the example below, using new Person() throws an error when the name property includes whitespace whereas calling Schema.decodeUnknownSync properly trims the whitespace as expected.

import { Schema } from '@effect/schema';

class Person extends Schema.Class<Person>('Person')({
  id: Schema.Number,
  name: Schema.Trim,
}) {}

const johnny = new Person({ id: 1, name: ' johnny ' }); // throws error
const john = Schema.decodeUnknownSync(Person)({ id: 1, name: ' john ' }); // does not throw 

console.log(johnny);
console.log(john);
Was this page helpful?