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

[feature request] define constraints at the type-level #22

Open
ducaale opened this issue Apr 25, 2021 · 1 comment
Open

[feature request] define constraints at the type-level #22

ducaale opened this issue Apr 25, 2021 · 1 comment

Comments

@ducaale
Copy link

ducaale commented Apr 25, 2021

Sometimes, I have a type A that might be used in other types. If I need to add constraints on that type, I would have to repeat those constraints every time I create a validator for a type that references type A.

type Url = string

type Video = {
  url: Url
  length: number
}

type Image = {
  url: Url
  width: number
  height: number
}

registerType('Video')
register('Image')

const imageValidator = createValidator<Image>(undefined, {
  constraints: {
    Url: x => x.startsWith('http://') // this can't be moved to an object or babel-macro will complain
  }
})

const videoValidator = createValidator<Video>(undefined, {
  constraints: {
    Url: x => x.startsWith('http://')
  }
})

It would be nice if one of the following was possible:

  • Type constraints can be defined when registering the corresponding type

    type Url = string
    
    type Video = {
      url: Url
      length: number
    }
    
    type Image = {
      url: Url
      width: number
      height: number
    }
    
    registerType('Url', x => x.startsWith('http://'))
    registerType('Video')
    register('Image')
    
    const x = createValidator<Image>()
    const x = createValidator<Video>()
  • Type constraints can be defined globally

    type Url = string
    
    type Video = {
      url: Url
      length: number
    }
    
    type Image = {
      url: Url
      width: number
      height: number
    }
    
    registerType('Video')
    register('Image')
    
    registerConstraints({
      Url: x => x.startsWith('http://')
    })
    const x = createValidator<Image>()
    const x = createValidator<Video>()
@vedantroy
Copy link
Owner

Hey! This makes a lot of sense. will look into this.

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