|
| 1 | +# Form Validation API ⚡ |
| 2 | + |
| 3 | +<p align="center"> |
| 4 | + <img width="200" src="/public/icon.png"> |
| 5 | +</p> |
| 6 | + |
| 7 | +You don't have to worry about form validation anymore, and write boilerplate code 😩. This API handles validation out-of-the-box 📦! It's as simple as that. |
| 8 | + |
| 9 | +🔗 API URL: `https://form-validation-api.vercel.app/api` |
| 10 | + |
| 11 | +## API Endpoints |
| 12 | + |
| 13 | +There are 4 endpoints to choose from. |
| 14 | +You can expect a response with a status code of `400` (if validation failed), and `200` if succeded. More in the examples section. |
| 15 | + |
| 16 | +| Description | Endpoint | |
| 17 | +| ---------------------------------------------- | -------------- | |
| 18 | +| Validate password | /password | |
| 19 | +| Validate email address | /email-address | |
| 20 | +| Sign In Form (email, password) | /sign-in-form | |
| 21 | +| Sign Up Form (email, password, password match) | /sign-up-form | |
| 22 | + |
| 23 | +### Validating Only Password 🔑 |
| 24 | + |
| 25 | +👉 Endpoint: `/password` |
| 26 | + |
| 27 | +We check whether the password: |
| 28 | + |
| 29 | +- is not empty, |
| 30 | +- contains at least 8 characters, |
| 31 | +- contains at least 1 digit, |
| 32 | +- contains at least 1 capital letter, |
| 33 | +- contains at least 1 special character. |
| 34 | + |
| 35 | +### Validating Only Email Address 📧 |
| 36 | + |
| 37 | +👉 Endpoint: `/email-address` |
| 38 | + |
| 39 | +We check whether the email address: |
| 40 | + |
| 41 | +- is not empty, |
| 42 | +- doesn't contain special characters such as `!#$%^&\*(),?\":{}|<>~^+/=`, |
| 43 | +- has no spaces, |
| 44 | +- contains the @ symbol, |
| 45 | +- does not have an additional @ in the username portion, |
| 46 | +- does not contain offensive, vulgar, or inappropriate content (example words will not be mentioned for ethical reasons). |
| 47 | + |
| 48 | +### Sign In Form Validation |
| 49 | + |
| 50 | +👉 Endpoint: `/sign-in` |
| 51 | + |
| 52 | +- validating both: email & password. |
| 53 | + |
| 54 | +### Sign Up Form Validation |
| 55 | + |
| 56 | +👉 Endpoint: `/sign-up` |
| 57 | + |
| 58 | +- validating: email, password & password match. |
| 59 | + |
| 60 | +## Example |
| 61 | + |
| 62 | +In the example provided below, I am validating the user's password. The same analogy applies to each available form of validation. |
| 63 | + |
| 64 | +``` |
| 65 | +const url = 'https://form-validation-api.vercel.app/api'; |
| 66 | +
|
| 67 | + const reqConfig = (method: string, body: {}): {} => { |
| 68 | + return { |
| 69 | + method: 'POST', |
| 70 | + body: body, |
| 71 | + headers: { |
| 72 | + 'Content-Type': 'application/json', |
| 73 | + }, |
| 74 | + }; |
| 75 | + }; |
| 76 | +
|
| 77 | + const validatePassword = async (password: string) => { |
| 78 | + try { |
| 79 | + const res = await fetch(`${url}/password`, reqConfig('POST', { password: password })); |
| 80 | + const validationResult = await res.json(); |
| 81 | +
|
| 82 | + if (!res.ok) throw new Error(validationResult.error); // if 400 code, 'error' key is available on the response object |
| 83 | +
|
| 84 | + //If we got here, it means that validation is successful |
| 85 | + console.log(validationResult.success); //if 200 code, 'success' key is available on the res. object |
| 86 | + } catch (error) { |
| 87 | + console.log((error as Error).message); |
| 88 | + } |
| 89 | + }; |
| 90 | +
|
| 91 | +``` |
| 92 | + |
| 93 | +## Rate Limit Middleware |
| 94 | + |
| 95 | +Rate limiting is a strategy employed to restrict network traffic and prevent potential abuse of APIs. Each API route is equipped with its own `rateLimiter` variable, which records the `timestamps` of user requests. That, in essence, summarizes the concept. |
| 96 | + |
| 97 | +The number of permitted requests per user, per minute for each API route is set at `10`. |
| 98 | + |
| 99 | +## Contribution |
| 100 | + |
| 101 | +Hey there, awesome folks! 👋 We're on a mission to make magic happen, and we need your collaboration superpowers! Let's team up, share ideas, and pool our talents to create something useful 🚀💫. Feel free to `fork` repo and `pull requests` or submit your request via `issues`. |
| 102 | + |
| 103 | +#CollaborationNation |
0 commit comments