Composing schemas with custom validation messages based on specific conditions can be a bit trick...

Hello team . I want to reuse my ReceiverSchema by composing it with Schema1 and Schem2 like a filed of struct. But i want to have different messages for Schema1 and Schem2 for each filter(startsWith, length). I added identifier ('1', '2') .

import { Schema } from 'effect';

const ReceiverSchema = Schema.String.pipe(
  Schema.startsWith('R', { identifier: '1' }),
  Schema.length(11, { identifier: '2' }),
);

export { ReceiverSchema };


I plan to cach these identifiers like

const Schema1 = Schema.Struct({
  receiverId: Schema.Trim.pipe(
    Schema.nonEmptyString({ message: () => 'Receiver Id required' }),
    Schema.compose(
      ReceiverSchema.annotations({
        message: (f) => {
          if (f._tag === 'Refinement') {
            console.log(f.ast.annotations['Symbol(effect/annotation/Identifier)']);
            return {
              message: '444',
              override: true,
            };
          } else {
            return '';
          }
        },
      }),
    ),


Could you help me to find a good solution please?
Was this page helpful?