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

How to supply a Semigroup appender to sequenceValidation? #237

Open
jdwolk opened this issue Feb 15, 2021 · 0 comments
Open

How to supply a Semigroup appender to sequenceValidation? #237

jdwolk opened this issue Feb 15, 2021 · 0 comments

Comments

@jdwolk
Copy link

jdwolk commented Feb 15, 2021

Hi!

I'm using Validation and I noticed most of the examples in https://github.com/monet/monet.js/blob/master/docs/VALIDATION.md use strings as the data structure on the Fail side. I want to supply a richer data structure but so far I'm coming up short.

I know that the parameterized type on the side of Fail needs to be a semigroup so that values can be "rolled up" with the equivalent of mconcat (I think?). So the error message I'm getting (Couldn't find a semigroup appender in the environment, please specify your own append function) does make sense to me. I'm just not sure how to go about creating the data structure + operations to make them compliant w/ this lib so I can use them.

Here's my code:

import * as R from 'ramda';
import { Validation, List } from 'monet';
const { Success, Fail } = Validation;

const isNonNullString = (str) => {
  return R.contains('null', str) ?
         Fail({ val: str, message: `'${str}' contained null`}) :
         Success(str)
}

/* NOTE: ^ CAN work with the following:

Fail([{ val: str, message: `'${str}' contained null`}])

since array is already a supported semigroup (I think?)
*/

const testStr1 = 'hello'
const testStr2 = 'hinullthere'
const testStr3 = 'boomnullalso'

const check = (str) => Success(str).chain(isNonNullString)

const testStrs = [
  testStr1,
  testStr2,
  testStr3,
];

const allTests = List.fromArray(R.map(check, testStrs)).sequenceValidation()
console.log('All tests: ', allTests);

Assuming I don't want my return type to be a known semigroup like string or array: how would I make one + supply it to sequenceValidation (or sequence?) to avoid getting this error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant