effect icon indicating copy to clipboard operation
effect copied to clipboard

InArray filter for strings

Open parischap opened this issue 2 years ago • 1 comments

🚀 Feature request

Current Behavior

With S.literal, you can create a schema that validates a string belongs to a set of strings known at compile-time. But sometimes, the strings to validate against are only known at run-time.

Desired Behavior

A String filter that checks the string belongs to an array of strings passed as parameter.

Suggested Solution

export const inArray = <C, B extends A, A extends string = B>(
	a: ReadonlyArray<C>,
	f: (s: string) => (c: C) => boolean
) =>
	S.filter<B, A>((s) => pipe(a, RA.findFirst(f(s)), O.isSome), {
		message: () => 'Not one of the allowed values'
	});

Who does this impact? Who is this for?

Describe alternatives you've considered

Additional context

Your environment

Software Version(s)
@effect/schema
TypeScript

parischap avatar Jun 14 '23 07:06 parischap

+1 I think this should be more general though, accepting an Equivalence instead of only working with string[].

jessekelly881 avatar Jun 14 '23 18:06 jessekelly881