smartvalidation-api 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +199 -0
- package/index.d.ts +50 -0
- package/index.js +141 -0
- package/package.json +22 -0
package/README.md
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# smartvalidation-api
|
|
2
|
+
|
|
3
|
+
Validate emails, phones & usernames with **helpful error messages** and **fix suggestions**.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install smartvalidation-api
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Get Your API Key
|
|
12
|
+
|
|
13
|
+
1. Go to [RapidAPI](https://rapidapi.com/ritualhere2/api/smart-validation)
|
|
14
|
+
2. Click "Subscribe" (free tier available)
|
|
15
|
+
3. Copy your API key
|
|
16
|
+
|
|
17
|
+
## Quick Start
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
const validate = require('smartvalidation-api')('YOUR_API_KEY');
|
|
21
|
+
|
|
22
|
+
// Validate email
|
|
23
|
+
const email = await validate.email('john@gmial.com');
|
|
24
|
+
console.log(email);
|
|
25
|
+
// {
|
|
26
|
+
// valid: false,
|
|
27
|
+
// reason: 'Domain appears to be misspelled (did you mean gmail.com?)',
|
|
28
|
+
// suggestion: 'john@gmail.com'
|
|
29
|
+
// }
|
|
30
|
+
|
|
31
|
+
// Validate phone
|
|
32
|
+
const phone = await validate.phone('+14155551234');
|
|
33
|
+
console.log(phone);
|
|
34
|
+
// { valid: true, normalized: '+14155551234', type: 'mobile', country: 'US' }
|
|
35
|
+
|
|
36
|
+
// Validate username
|
|
37
|
+
const username = await validate.username('john_doe');
|
|
38
|
+
console.log(username);
|
|
39
|
+
// { valid: true, normalized: 'john_doe' }
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Examples
|
|
43
|
+
|
|
44
|
+
### Check if valid (boolean)
|
|
45
|
+
|
|
46
|
+
```javascript
|
|
47
|
+
if (await validate.isValidEmail('test@gmail.com')) {
|
|
48
|
+
// Email is valid
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (await validate.isValidPhone('+14155551234')) {
|
|
52
|
+
// Phone is valid
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (await validate.isValidUsername('john_doe')) {
|
|
56
|
+
// Username is valid
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Validate multiple fields at once
|
|
61
|
+
|
|
62
|
+
```javascript
|
|
63
|
+
const result = await validate.all({
|
|
64
|
+
email: 'test@gmail.com',
|
|
65
|
+
phone: '+14155551234',
|
|
66
|
+
username: 'john_doe'
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
console.log(result.email.valid); // true
|
|
70
|
+
console.log(result.phone.valid); // true
|
|
71
|
+
console.log(result.username.valid); // true
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Phone with country code
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
// For local numbers, provide the country
|
|
78
|
+
const phone = await validate.phone('6971234567', 'GR');
|
|
79
|
+
// { valid: true, normalized: '+306971234567', country: 'GR' }
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Handle errors nicely
|
|
83
|
+
|
|
84
|
+
```javascript
|
|
85
|
+
const result = await validate.email(userInput);
|
|
86
|
+
|
|
87
|
+
if (!result.valid) {
|
|
88
|
+
// Show the reason to the user
|
|
89
|
+
alert(result.reason);
|
|
90
|
+
|
|
91
|
+
// Offer the suggestion if available
|
|
92
|
+
if (result.suggestion) {
|
|
93
|
+
const useIt = confirm(`Did you mean ${result.suggestion}?`);
|
|
94
|
+
if (useIt) {
|
|
95
|
+
userInput = result.suggestion;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## React Example
|
|
102
|
+
|
|
103
|
+
```jsx
|
|
104
|
+
import { useState } from 'react';
|
|
105
|
+
const validate = require('smart-validation')('YOUR_API_KEY');
|
|
106
|
+
|
|
107
|
+
function SignupForm() {
|
|
108
|
+
const [email, setEmail] = useState('');
|
|
109
|
+
const [error, setError] = useState('');
|
|
110
|
+
const [suggestion, setSuggestion] = useState('');
|
|
111
|
+
|
|
112
|
+
const checkEmail = async () => {
|
|
113
|
+
const result = await validate.email(email);
|
|
114
|
+
|
|
115
|
+
if (result.valid) {
|
|
116
|
+
setError('');
|
|
117
|
+
setSuggestion('');
|
|
118
|
+
} else {
|
|
119
|
+
setError(result.reason);
|
|
120
|
+
setSuggestion(result.suggestion || '');
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
return (
|
|
125
|
+
<div>
|
|
126
|
+
<input
|
|
127
|
+
value={email}
|
|
128
|
+
onChange={(e) => setEmail(e.target.value)}
|
|
129
|
+
onBlur={checkEmail}
|
|
130
|
+
placeholder="Email"
|
|
131
|
+
/>
|
|
132
|
+
{error && <p style={{color: 'red'}}>{error}</p>}
|
|
133
|
+
{suggestion && (
|
|
134
|
+
<button onClick={() => setEmail(suggestion)}>
|
|
135
|
+
Use {suggestion}
|
|
136
|
+
</button>
|
|
137
|
+
)}
|
|
138
|
+
</div>
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Next.js Example
|
|
144
|
+
|
|
145
|
+
```javascript
|
|
146
|
+
// pages/api/validate.js
|
|
147
|
+
const validate = require('smart-validation')(process.env.RAPIDAPI_KEY);
|
|
148
|
+
|
|
149
|
+
export default async function handler(req, res) {
|
|
150
|
+
const { email, phone, username } = req.body;
|
|
151
|
+
|
|
152
|
+
const result = await validate.all({ email, phone, username });
|
|
153
|
+
|
|
154
|
+
res.json(result);
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## What You Get
|
|
159
|
+
|
|
160
|
+
### Email Validation
|
|
161
|
+
- Catches typos (gmial.com → gmail.com)
|
|
162
|
+
- Blocks temporary/disposable emails
|
|
163
|
+
- Returns normalized lowercase version
|
|
164
|
+
|
|
165
|
+
### Phone Validation
|
|
166
|
+
- Works with any format
|
|
167
|
+
- Returns E.164 normalized format
|
|
168
|
+
- Detects mobile vs landline
|
|
169
|
+
- Supports 200+ countries
|
|
170
|
+
|
|
171
|
+
### Username Validation
|
|
172
|
+
- Checks length (3-30 chars)
|
|
173
|
+
- Must start with a letter
|
|
174
|
+
- No consecutive special characters
|
|
175
|
+
- Suggests valid alternatives
|
|
176
|
+
|
|
177
|
+
## API Response Format
|
|
178
|
+
|
|
179
|
+
### Valid
|
|
180
|
+
```json
|
|
181
|
+
{
|
|
182
|
+
"valid": true,
|
|
183
|
+
"normalized": "john@gmail.com"
|
|
184
|
+
}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Invalid
|
|
188
|
+
```json
|
|
189
|
+
{
|
|
190
|
+
"valid": false,
|
|
191
|
+
"reason": "Domain appears to be misspelled",
|
|
192
|
+
"suggestion": "john@gmail.com"
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Need Help?
|
|
197
|
+
|
|
198
|
+
- [Full Documentation](https://rapidapi.com/ritualhere2/api/smart-validation)
|
|
199
|
+
- [Get API Key](https://rapidapi.com/ritualhere2/api/smart-validation)
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
declare module 'smart-validation' {
|
|
2
|
+
interface EmailResult {
|
|
3
|
+
valid: boolean;
|
|
4
|
+
normalized?: string;
|
|
5
|
+
reason?: string;
|
|
6
|
+
suggestion?: string;
|
|
7
|
+
disposable?: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface PhoneResult {
|
|
11
|
+
valid: boolean;
|
|
12
|
+
normalized?: string;
|
|
13
|
+
type?: 'mobile' | 'landline' | 'voip' | 'toll-free' | string;
|
|
14
|
+
country?: string;
|
|
15
|
+
reason?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface UsernameResult {
|
|
19
|
+
valid: boolean;
|
|
20
|
+
normalized?: string;
|
|
21
|
+
reason?: string;
|
|
22
|
+
suggestions?: string[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface AllFields {
|
|
26
|
+
email?: string;
|
|
27
|
+
phone?: string;
|
|
28
|
+
username?: string;
|
|
29
|
+
country?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface AllResults {
|
|
33
|
+
email?: EmailResult;
|
|
34
|
+
phone?: PhoneResult;
|
|
35
|
+
username?: UsernameResult;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface Validator {
|
|
39
|
+
email(email: string): Promise<EmailResult>;
|
|
40
|
+
phone(phone: string, country?: string): Promise<PhoneResult>;
|
|
41
|
+
username(username: string): Promise<UsernameResult>;
|
|
42
|
+
all(fields: AllFields): Promise<AllResults>;
|
|
43
|
+
isValidEmail(email: string): Promise<boolean>;
|
|
44
|
+
isValidPhone(phone: string, country?: string): Promise<boolean>;
|
|
45
|
+
isValidUsername(username: string): Promise<boolean>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function createValidator(apiKey: string): Validator;
|
|
49
|
+
export = createValidator;
|
|
50
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Smart Validation - Simple validation with helpful errors
|
|
3
|
+
*
|
|
4
|
+
* Quick Start:
|
|
5
|
+
* const validate = require('smart-validation')('YOUR_API_KEY');
|
|
6
|
+
* const result = await validate.email('test@gmail.com');
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const API_URL = 'https://smart-validation.p.rapidapi.com/validate';
|
|
10
|
+
const API_HOST = 'smart-validation.p.rapidapi.com';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Create a validator instance
|
|
14
|
+
* @param {string} apiKey - Your RapidAPI key
|
|
15
|
+
* @returns {Object} Validator with email, phone, username, and all methods
|
|
16
|
+
*/
|
|
17
|
+
function createValidator(apiKey) {
|
|
18
|
+
if (!apiKey) {
|
|
19
|
+
throw new Error('API key is required. Get one at: https://rapidapi.com/ritualhere2/api/smart-validation');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Make API request
|
|
24
|
+
*/
|
|
25
|
+
async function request(body) {
|
|
26
|
+
const response = await fetch(API_URL, {
|
|
27
|
+
method: 'POST',
|
|
28
|
+
headers: {
|
|
29
|
+
'Content-Type': 'application/json',
|
|
30
|
+
'X-RapidAPI-Key': apiKey,
|
|
31
|
+
'X-RapidAPI-Host': API_HOST
|
|
32
|
+
},
|
|
33
|
+
body: JSON.stringify(body)
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
if (!response.ok) {
|
|
37
|
+
const error = await response.json().catch(() => ({}));
|
|
38
|
+
throw new Error(error.message || `API error: ${response.status}`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return response.json();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
/**
|
|
46
|
+
* Validate an email address
|
|
47
|
+
* @param {string} email - Email to validate
|
|
48
|
+
* @returns {Promise<Object>} { valid, normalized, reason, suggestion, disposable }
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* const result = await validate.email('john@gmial.com');
|
|
52
|
+
* // { valid: false, reason: 'Domain misspelled', suggestion: 'john@gmail.com' }
|
|
53
|
+
*/
|
|
54
|
+
async email(email) {
|
|
55
|
+
const result = await request({ email });
|
|
56
|
+
return result.email;
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Validate a phone number
|
|
61
|
+
* @param {string} phone - Phone number to validate
|
|
62
|
+
* @param {string} [country] - ISO country code (US, GB, etc.)
|
|
63
|
+
* @returns {Promise<Object>} { valid, normalized, type, country, reason }
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* const result = await validate.phone('+14155551234');
|
|
67
|
+
* // { valid: true, normalized: '+14155551234', type: 'mobile', country: 'US' }
|
|
68
|
+
*/
|
|
69
|
+
async phone(phone, country) {
|
|
70
|
+
const body = { phone };
|
|
71
|
+
if (country) body.country = country;
|
|
72
|
+
const result = await request(body);
|
|
73
|
+
return result.phone;
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Validate a username
|
|
78
|
+
* @param {string} username - Username to validate
|
|
79
|
+
* @returns {Promise<Object>} { valid, normalized, reason, suggestions }
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* const result = await validate.username('123user');
|
|
83
|
+
* // { valid: false, reason: 'Must start with letter', suggestions: ['user123'] }
|
|
84
|
+
*/
|
|
85
|
+
async username(username) {
|
|
86
|
+
const result = await request({ username });
|
|
87
|
+
return result.username;
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Validate multiple fields at once
|
|
92
|
+
* @param {Object} fields - { email, phone, username, country }
|
|
93
|
+
* @returns {Promise<Object>} Results for each provided field
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* const result = await validate.all({
|
|
97
|
+
* email: 'test@gmail.com',
|
|
98
|
+
* phone: '+14155551234',
|
|
99
|
+
* username: 'john_doe'
|
|
100
|
+
* });
|
|
101
|
+
*/
|
|
102
|
+
async all(fields) {
|
|
103
|
+
return request(fields);
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Quick check if email is valid (returns boolean)
|
|
108
|
+
* @param {string} email
|
|
109
|
+
* @returns {Promise<boolean>}
|
|
110
|
+
*/
|
|
111
|
+
async isValidEmail(email) {
|
|
112
|
+
const result = await request({ email });
|
|
113
|
+
return result.email.valid;
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Quick check if phone is valid (returns boolean)
|
|
118
|
+
* @param {string} phone
|
|
119
|
+
* @param {string} [country]
|
|
120
|
+
* @returns {Promise<boolean>}
|
|
121
|
+
*/
|
|
122
|
+
async isValidPhone(phone, country) {
|
|
123
|
+
const body = { phone };
|
|
124
|
+
if (country) body.country = country;
|
|
125
|
+
const result = await request(body);
|
|
126
|
+
return result.phone.valid;
|
|
127
|
+
},
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Quick check if username is valid (returns boolean)
|
|
131
|
+
* @param {string} username
|
|
132
|
+
* @returns {Promise<boolean>}
|
|
133
|
+
*/
|
|
134
|
+
async isValidUsername(username) {
|
|
135
|
+
const result = await request({ username });
|
|
136
|
+
return result.username.valid;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
module.exports = createValidator;
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "smartvalidation-api",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Validate emails, phones & usernames with smart error messages and fix suggestions",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"validation",
|
|
9
|
+
"email",
|
|
10
|
+
"phone",
|
|
11
|
+
"username",
|
|
12
|
+
"form",
|
|
13
|
+
"validator"
|
|
14
|
+
],
|
|
15
|
+
"author": "",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"homepage": "https://rapidapi.com/ritualhere2/api/smart-validation",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": ""
|
|
21
|
+
}
|
|
22
|
+
}
|