Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composable decoders #81

Open
dearlordylord opened this issue Jul 29, 2024 · 2 comments
Open

Composable decoders #81

dearlordylord opened this issue Jul 29, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@dearlordylord
Copy link

I think it's related to "S.merge" accepting only plain object schemas (e.g no unions, no primitives):

const integerSchema = S.refine(S.number, (value, s) => {
  if (value % 1 !== 0) {
    throw s.fail('Must be an integer');
  }
});

const nonNegativeNumberSchema = S.refine(S.number, (value, s) => {
  if (value < 0) {
    throw s.fail('Must be a positive number');
  }
});

// have to repeat, I didn't find a way to combine integerSchema and nonNegativeNumberSchema
// e.g. S.merge() works only with objects
const nonNegativeIntegerSchema = S.refine(integerSchema, (value, s) => {
  if (value < 0) {
    throw s.fail('Must be a positive number');
  }
});
@dearlordylord
Copy link
Author

ref #60

@DZakh
Copy link
Owner

DZakh commented Jul 29, 2024

I already have plans for v9 and probably won't have time adding the feature in the near future. As for workaround I'd suggest move the refine into a reusable function:

const toNonNegativeNumberSchema = schema => S.refine(schema, (value, s) => {
  if (value < 0) {
    throw s.fail('Must be a positive number');
  }
});

const nonNegativeIntegerSchema = toNonNegativeNumberSchema(integerSchema)

Also, there are built-in S.integer and S.numberMin(S.number, 0) you can use https://github.com/DZakh/rescript-schema/blob/main/docs/js-usage.md#numbers.

@DZakh DZakh added the enhancement New feature or request label Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants