-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from Aryan9592/patch-2
Create yup.js
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Yup | ||
|
||
Yup is an npm module that provides a simple and intuitive way to perform object schema validation in JavaScript. It allows you to define a schema and then use it | ||
to validate and transform values at runtime. | ||
|
||
# Installation | ||
|
||
Using npm: | ||
``` | ||
npm install debug | ||
``` | ||
|
||
# Features: | ||
|
||
- `Yup` is a schema builder for runtime value parsing and validation. | ||
- It offers a dead simple API for defining schemas and validating data. | ||
- With `Yup`, you can define rules and constraints for each field in your object schema. | ||
- Yup provides powerful validation methods like validate, isValid, and cast to handle data validation and transformation | ||
- It is widely used in projects that require form validation, data validation, and input sanitization. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const yup = require('yup'); | ||
|
||
/* | ||
1. Here, we first import the yup module and define a schema using the object().shape() method. The schema specifies the validation rules for each field in the data object. | ||
2. We then create a data object with values for the name, email, and age fields. We use the validate() method of the schema to validate the data against the defined schema. | ||
3. If the data is valid, the validate() method returns the validated data. Otherwise, it throws a validation error with the corresponding error message. | ||
*/ | ||
|
||
// Define a schema for validation | ||
const schema = yup.object().shape({ | ||
name: yup.string().required('Name is required'), | ||
email: yup.string().email('Invalid email').required('Email is required'), | ||
age: yup.number().positive('Age must be a positive number').required('Age is required'), | ||
}); | ||
|
||
// Data to be validated | ||
const data = { | ||
name: 'John Doe', | ||
email: '[email protected]', | ||
age: 25, | ||
}; | ||
|
||
// Validate the data against the schema | ||
schema.validate(data) | ||
.then(validatedData => { | ||
console.log('Data is valid:', validatedData); | ||
}) | ||
.catch(error => { | ||
console.log('Validation error:', error.message); | ||
}); | ||
|
||
// Output | ||
// Data is valid: { name: 'John Doe', email: 'johndoe@example', age: 25 } |