yup
Yup is a schema builder for runtime value parsing and validation. It allows you to define a schema, transform a value to match, assert the shape of an existing value, or do both. Yup schema are highly expressive and capable of modeling complex, interdependent validations or value transformation. Some of the key features of Yup include its concise yet expressive schema interface, powerful TypeScript support, built-in async validation support, extensibility to add your own type-safe methods and schema, as well as rich error details to facilitate debugging.
To get started with Yup, schemas are comprised of parsing actions (transforms) as well as assertions (tests) about the input value. You can validate an input value to parse it and run the configured set of assertions and chain together methods to build a schema. You can also use a schema to coerce or "cast" an input value into the correct type, and optionally transform that value into more concrete and specific values, without making further assertions.
For example, you can define a user schema with attributes such as name, age, email, website, and created, and then parse and assert its validity.
Here's a basic example of using Yup to create a user schema:
import { object, string, number, date, InferType } from 'yup'; let userSchema = object({ name: string().required(), age: number().required().positive().integer(), email: string().email(), website: string().url().nullable(), createdOn: date().default(() => new Date()), }); // Parse and assert validity const user = await userSchema.validate(await fetchUser()); type User = InferType<typeof userSchema>; // Use a schema to coerce or "cast" an input value into the correct type const parsedUser = userSchema.cast({ name: 'jimmy', age: '24', createdOn: '2014-09-23T19:25:25Z' });
This is just a basic overview of Yup and how it can be used for runtime value parsing and validation. If you have specific questions or need further assistance with Yup, feel free to ask!
github.com
npmjs.com
dictionary.com