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

SetOf(type) and other collection checkers #12

Closed
callumlocke opened this issue Jun 4, 2015 · 3 comments
Closed

SetOf(type) and other collection checkers #12

callumlocke opened this issue Jun 4, 2015 · 3 comments

Comments

@callumlocke
Copy link
Contributor

What about SetOf(type), like ArrayOf but for ES6 sets?

Maybe also MapOf(keyType, valueType)?

This also got me thinking about extensibility... for example, if I am using an a custom/library-provided collection type like Immutable.OrderedSet, is there some way I could define my own ImmutableOrderedSetOf(type) (which I could then compose with other built-ins like Optional or ArrayOf(), etc)?

Maybe extensibility could be provided with a 'custom' type checker, which lets you supply a callback to examine the value in a custom way, like this:

import {Custom} from 'decorate-this';

const ImmutableOrderedSetOf = Custom((value, type) => {
  return value instanceof Immutable.OrderedSet && value.every(item => {
    return item instanceof type;
  });
});

@param(ImmutableOrderedSetOf(String))
function (strings) {
  // ...
}

(I don't actually know if this would work, just an idea.)

@mako-taco
Copy link
Owner

I do eventually want to support custom types. For now, I will probably just add SetOf, MapOf.

I will think on the API for a custom type checker afterwards. I could just expose Validator and ValidatorResult which I use internally, but they are a little confusing, and usually overkill for simple types.

@mako-taco
Copy link
Owner

I think this will get us by.

const ImmutableMap = KeyedCollection(Immutable.Map, map => map.toObject());
const ImmutableListOf = TypedCollection(Immutable.List, list => list.toArray());

class T {
    @param(ImmutableMap({key: String, val: String}))
    doThing() {
    }

    @param(ImmutableListOf(Number))
    doOtherThing() {
    }
}

KeyedCollection takes two arguments, a constructor/class and a transformation function. It returns a function which takes a type.

  • The transformation function must return a plain Object

TypedCollection takes two arguments, a constructor/class and a transformation function. It returns a function which takes a type.

  • The transformation function must return either an Array or an Object.
  • The returned value will be iterated over with a for...of loop
  • Each member of the value will be validated against the type

@callumlocke any comments?

mako-taco pushed a commit that referenced this issue Jun 8, 2015
mako-taco pushed a commit that referenced this issue Jun 8, 2015
@mako-taco mako-taco mentioned this issue Jun 8, 2015
@callumlocke
Copy link
Contributor Author

looks great

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

2 participants