Moving `makeValidDataIndex` Validation to Schema Discussion

can we move the validation of makeValidDataIndex to the schema ?
class transformations seem overkill, perhaps the below is idiomatic enough

export const ValidDataIndexSchema = Primitives.PositiveIntSchema.pipe(
  Schema.brand("ValidDataIndex")
);

export type ValidDataIndex = typeof ValidDataIndexSchema.Type;

export const makeValidDataIndex = (
  tree: MerkleTree,
  index: Primitives.PositiveInt
): Either.Either<ValidDataIndex, IndexOutOfBoundsError> => {
  const leafCount = Chunk.size(tree.leaves);
  return index >= leafCount
    ? Either.left(
        new IndexOutOfBoundsError({
          message: `Index ${index} out of bounds [0, ${leafCount - 1}]`,
          index,
          maxIndex: leafCount - 1,
        })
      )
    : Either.right(index as ValidDataIndex);
};
Was this page helpful?