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

Question: Object as value in custom constraint #179

Closed
matthyk opened this issue Feb 11, 2021 · 2 comments
Closed

Question: Object as value in custom constraint #179

matthyk opened this issue Feb 11, 2021 · 2 comments

Comments

@matthyk
Copy link
Contributor

matthyk commented Feb 11, 2021

Since version 4.0.0, it is possible to write your own constraint strategies. In the index.d.ts, however, it is specified that the values for the constraints must be strings. So my question is whether it is safe to also take object as values, as in the example below.

Current typing in index.d.ts:

interface ConstraintStrategy<V extends HTTPVersion> {
    name: string,
    mustMatchWhenDerived?: boolean,
    storage() : {
      get(version: String) : Handler<V> | null,
      set(version: String, store: Handler<V>) : void,
      del(version: String) : void,
      empty() : void
    },
    validate(value: unknown): void,
    deriveConstraint<Context>(req: Req<V>, ctx?: Context) : String,
  }

Example with an object as key instead of a string:

interface MyObject {
    myProperty: string;
    mySecondProperty: string;
    myNumberProperty: number;
}

interface ConstraintStrategy<V extends HTTPVersion> {
    name: string,
    mustMatchWhenDerived?: boolean,
    storage() : {
      get(version: MyObject) : Handler<V> | null,
      set(version: MyObject, store: Handler<V>) : void,
      del(version: MyObject) : void,
      empty() : void
    },
    validate(value: unknown): void,
    deriveConstraint<Context>(req: Req<V>, ctx?: Context) : MyObject,
  }

Could be typed in the index.d.ts like this:

interface ConstraintStrategy<V extends HTTPVersion, T = string> {
    name: string,
    mustMatchWhenDerived?: boolean,
    storage() : {
      get(version: T) : Handler<V> | null,
      set(version: T, store: Handler<V>) : void,
      del(version: T) : void,
      empty() : void
    },
    validate(value: unknown): void,
    deriveConstraint<Context>(req: Req<V>, ctx?: Context) : T,
  }
@mcollina
Copy link
Collaborator

I would say that the best way is to send a PR and figure things out :).

@matthyk
Copy link
Contributor Author

matthyk commented Feb 12, 2021

Created PR #180

@matthyk matthyk closed this as completed Mar 30, 2021
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