Giter VIP home page Giter VIP logo

Comments (4)

fabian-hiller avatar fabian-hiller commented on July 4, 2024 1

I added ContentInput, ContentRequirement, LengthInput, SizeInput and ValueInput to the exports.

from valibot.

fabian-hiller avatar fabian-hiller commented on July 4, 2024

Thanks for the feedback. I will check if I want to export these types, but in general it is probably a good idea. nonEmpty is now natively supported out of the box, so you don't have to add it yourself.

In general, there is no difference from before. The only difference is that the API has become more type-safe, which requires more precious typing, resulting in more TypeScript code. But if you don't care, you can write it much the same way as before. Here is an example. You can test it in this playground.

import * as v from 'valibot';

type LengthInput = string | unknown[];

function nonEmpty<TInput extends LengthInput>(
  message: string = 'Non-empty value required.'
): v.MinLengthAction<TInput, 1, string> {
  return v.minLength(1, message);
}

If you prefer precise types, I have rewritten your code to make it a bit simpler. You can test it in this playground.

import * as v from 'valibot';

type LengthInput = string | unknown[];

function nonEmpty<
  TInput extends LengthInput,
  const TMessage extends v.ErrorMessage<v.MinLengthIssue<TInput, 1>>,
>(
  message: TMessage = 'Non-empty value required.' as TMessage
): v.MinLengthAction<TInput, 1, TMessage> {
  return v.minLength(1, message);
}

from valibot.

IlyaSemenov avatar IlyaSemenov commented on July 4, 2024

Another use case is:

export function integerNumber<TMessage extends v.ErrorMessage<v.NumberIssue>>(message?: TMessage) {
  return v.pipe(v.number(message), v.integer())
}

is there a better way to type this? Most of the schemas use function overload to distinguish between the message and undefined:

declare function number(): NumberSchema<undefined>;
declare function number<const TMessage extends ErrorMessage<NumberIssue> | undefined>(message: TMessage): NumberSchema<TMessage>;

Is there a way to proxy this contract somehow to the extending one-liners?

from valibot.

fabian-hiller avatar fabian-hiller commented on July 4, 2024

As I understand it, this is not possible without drawbacks. That's why I use overload signatures.

from valibot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.