form-snap-js 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nikhil Raj
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # form-snap
2
+
3
+ A lightweight, chainable form validation library for JavaScript.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install form-snap
9
+ ```
10
+
11
+ ## Features
12
+
13
+ - **Chainable API**: Easy to read and write validation rules.
14
+ - **Lightweight**: Zero dependencies (except for development).
15
+ - **Extensible**: Easily add custom validation logic.
16
+ - **Built-in Validators**: Support for common validations like email, phone, password, and required fields.
17
+
18
+ ## Usage
19
+
20
+ ```javascript
21
+ const snap = require('form-snap');
22
+
23
+ const data = {
24
+ email: 'user@example.com',
25
+ phone: '1234567890',
26
+ password: 'securePassword123'
27
+ };
28
+
29
+ const result = snap(data)
30
+ .field('email')
31
+ .required('Email is required')
32
+ .email('Please enter a valid email address')
33
+ .field('phone')
34
+ .required()
35
+ .phone('Invalid phone number')
36
+ .field('password')
37
+ .required()
38
+ .min(8, 'Password must be at least 8 characters')
39
+ .password('Password is too weak')
40
+ .validate();
41
+
42
+ if (result.valid) {
43
+ console.log('Form is valid!');
44
+ } else {
45
+ console.log('Validation errors:', result.errors);
46
+ }
47
+ ```
48
+
49
+ ## API Reference
50
+
51
+ ### `snap(data)`
52
+ Initializes the validation process with the provided data object.
53
+
54
+ ### `.field(name)`
55
+ Selects a field from the data object to apply validation rules to.
56
+
57
+ ### `.required(message?)`
58
+ Validates that the field is not empty.
59
+
60
+ ### `.email(message?)`
61
+ Validates that the field is a valid email address.
62
+
63
+ ### `.phone(message?)`
64
+ Validates that the field is a valid phone number.
65
+
66
+ ### `.password(message?)`
67
+ Validates that the field meets basic password strength requirements.
68
+
69
+ ### `.min(length, message?)`
70
+ Validates that the field has a minimum length.
71
+
72
+ ### `.validate()`
73
+ Executes all validation rules and returns an object:
74
+ - `valid`: Boolean indicating if all rules passed.
75
+ - `errors`: An object containing error messages for each field that failed validation.
76
+
77
+ ## License
78
+
79
+ MIT © [Nikhil Raj](https://github.com/nikhilraj-13)
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "form-snap-js",
3
+ "version": "1.0.0",
4
+ "description": "A lightweight, chainable form validation library for JavaScript.",
5
+ "main": "src/index.js",
6
+ "files": [
7
+ "src",
8
+ "README.md",
9
+ "LICENSE"
10
+ ],
11
+ "scripts": {
12
+ "test": "jest",
13
+ "prepublishOnly": "npm test"
14
+ },
15
+ "keywords": [
16
+ "validation",
17
+ "form",
18
+ "chainable",
19
+ "validator",
20
+ "form-validation",
21
+ "snap"
22
+ ],
23
+ "author": "Nikhil Raj",
24
+ "license": "MIT",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/nikhilraj-13/form-snap.git"
28
+ },
29
+ "bugs": {
30
+ "url": "https://github.com/nikhilraj-13/form-snap/issues"
31
+ },
32
+ "homepage": "https://github.com/nikhilraj-13/form-snap#readme",
33
+ "type": "commonjs",
34
+ "devDependencies": {
35
+ "jest": "^30.4.2"
36
+ }
37
+ }
@@ -0,0 +1,92 @@
1
+ const required = require("./validators/required");
2
+ const email = require("./validators/email");
3
+ const phone = require("./validators/phone");
4
+ const password = require("./validators/password");
5
+
6
+ class FormSnap {
7
+ constructor(data) {
8
+ this.data = data;
9
+ this.rules = {};
10
+ this.currentField = null;
11
+ }
12
+
13
+ field(name) {
14
+ this.currentField = name;
15
+
16
+ if (!this.rules[name]) {
17
+ this.rules[name] = [];
18
+ }
19
+
20
+ return this;
21
+ }
22
+
23
+ required(message) {
24
+ this.rules[this.currentField].push({
25
+ validator: required,
26
+ message: message || "This field is required"
27
+ });
28
+
29
+ return this;
30
+ }
31
+
32
+ email(message) {
33
+ this.rules[this.currentField].push({
34
+ validator: email,
35
+ message: message || "Invalid email"
36
+ });
37
+
38
+ return this;
39
+ }
40
+
41
+ phone(message) {
42
+ this.rules[this.currentField].push({
43
+ validator: phone,
44
+ message: message || "Invalid phone number"
45
+ });
46
+
47
+ return this;
48
+ }
49
+
50
+ password(message) {
51
+ this.rules[this.currentField].push({
52
+ validator: password,
53
+ message: message || "Weak password"
54
+ });
55
+
56
+ return this;
57
+ }
58
+
59
+ min(length, message) {
60
+ this.rules[this.currentField].push({
61
+ validator: (value) =>
62
+ value && value.length >= length,
63
+ message:
64
+ message ||
65
+ `Minimum ${length} characters required`
66
+ });
67
+
68
+ return this;
69
+ }
70
+
71
+ validate() {
72
+ const errors = {};
73
+
74
+ for (const field in this.rules) {
75
+ const value = this.data[field];
76
+
77
+ for (const rule of this.rules[field]) {
78
+ if (!rule.validator(value)) {
79
+ errors[field] = rule.message;
80
+ break;
81
+ }
82
+ }
83
+ }
84
+
85
+ return {
86
+ valid: Object.keys(errors).length === 0,
87
+ errors
88
+ };
89
+ }
90
+ }
91
+
92
+ module.exports = FormSnap;
package/src/index.js ADDED
@@ -0,0 +1,3 @@
1
+ const FormSnap = require("./FormSnap");
2
+
3
+ module.exports = (data) => new FormSnap(data);
@@ -0,0 +1,4 @@
1
+ module.exports = (value) => {
2
+ const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
3
+ return regex.test(value);
4
+ };
@@ -0,0 +1,6 @@
1
+ module.exports = (value) => {
2
+ const regex =
3
+ /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/;
4
+
5
+ return regex.test(value);
6
+ };
@@ -0,0 +1,4 @@
1
+ module.exports = (value) => {
2
+ const regex = /^\+?[1-9]\d{7,14}$/;
3
+ return regex.test(value);
4
+ };
@@ -0,0 +1,5 @@
1
+ module.exports = (value) => {
2
+ return value !== undefined &&
3
+ value !== null &&
4
+ value !== "";
5
+ };