Skip to content

Commit

Permalink
Merge pull request #86 from Aryan9592/patch-2
Browse files Browse the repository at this point in the history
Create yup.js
  • Loading branch information
neer-patel-11 authored Oct 17, 2023
2 parents b72b4f9 + 35ec069 commit 8fc2613
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ So, What are you waiting for? 🤔
| **W** | | |
| **X** | | |
| **Y** | | |
| | [Yup](.npm_Modules/Yup/) | [npm Yup](https://www.npmjs.com/package/yup) | [docs Yup](https://github.com/jquense/yup) |
| **Z** | | |


Expand Down
19 changes: 19 additions & 0 deletions npm_Modules/Yup/README.md
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.
33 changes: 33 additions & 0 deletions npm_Modules/Yup/yup.js
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 }

0 comments on commit 8fc2613

Please sign in to comment.