Effect CommunityEC
Effect Communityβ€’3y agoβ€’
124 replies
bug

Propagating a Branded Schema to a Larger Schema

hi all. thanks for all your help lately. my team finally got Effect into production!

how might one recommend i propagate a branded schema to a larger schema?

const ODataAnnotationId = 'SelectExpandAnnotationId'

type ODataAnnotationValue = 'select' | 'expand'

const getODataAnnotation = AST.getAnnotation<ODataAnnotationValue>(ODataAnnotationId)

type ODataAnnotatedSchema<I, A> = Schema.Schema<I, A> & Brand.Brand<'ODataAnnotatedSchema'>

const oDataAnnotatedSchema =
  (value: ODataAnnotationValue) =>
  <I, A>(self: Schema.Schema<I, A>) =>
    pipe(
      AST.setAnnotation(self.ast, ODataAnnotationId, value),
      Schema.make,
      Brand.nominal<ODataAnnotatedSchema<I, A>>(),
    )

const oDataSelect = oDataAnnotatedSchema('select')

const oDataExpand = oDataAnnotatedSchema('expand')

// Good:
// const foo: ODataAnnotatedSchema<string, string>
const foo = oDataSelect(Schema.string)

// Bad:
// const fooObj: Schema.Schema<{
//   readonly foo: string;
//   readonly bar: string;
// }, {
//   readonly foo: string;
//   readonly bar: string;
// }>
const fooObj = Schema.struct({
  foo,
  bar: Schema.string,
})


do i need to create a custom implementation of Schema.struct with my own types like StructFields? i'd have tried that already but i'm afraid it will take me hours and still-isn't-the-right-idea. πŸ™‚

there is a chance that my need for a branded type is an indicator of a problem in my design, and that my API needs reconsideration all-together. but it would take a while to explain all the different API variations i've tried, so i tried to distill what i believe to be my best option. i'm willing to talk more about that if it helps.
Was this page helpful?