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

Create yup.js #86

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }