ianstormtaylor / superstruct

A simple and composable way to validate data in JavaScript (and TypeScript).
https://docs.superstructjs.org
MIT License
7.01k stars 223 forks source link

Swap arguments order and add currying for validation functions? #461

Closed vaderkos closed 3 months ago

vaderkos commented 4 years ago

It would be great if validation functions like is, assert, coerce, validate supported currying and had swapped arguments order.

In that way there would a bit more ways to use superstruct, so bound function can be easily prepared.

import { assert, number, string, StructType } from 'superstruct'

export const ArticleStruct = object({
  id: number(),
  title: string()
})

export type Article = StructType<typeof ArticleStruct>

export const assertArticle = assert(ArticleStruct)
mikestopcontinues commented 4 years ago

I like this. In the meantime, you can use the babel implementation of the partial operator to get what you want without wrapper functions.

ianstormtaylor commented 3 years ago

I need to document this better, but you can already use:

ArticleStruct.assert(data)

Instead of:

assert(data, ArticleStruct)

I'm not sure we gain much more with currying?

The reason to have the helpers as functional ones at all is unfortunately because we're pushing the limits of what TypeScript is capable of, and TypeScript will error when using Struct.assert(data), which I'm fairly sure will also error when using the curried method. (Here's the TypeScript issue tracking the limitation: https://github.com/microsoft/TypeScript/issues/34596)

ianstormtaylor commented 3 years ago

I take it back, I do see the value in offering currying. Once that upstream TypeScript issue is fixed I'm down to have a breaking change to swap the order of those arguments and add currying. I don't want to do it until then though because I don't want to end up with more parts of the codebase that only work when not using TypeScript.

arturmuller commented 3 months ago

It seems like the TS issue is not going to be solved. Closing for now.