Skip to content

Commit

Permalink
feat(README.md): add contribution guidelines and commit message conve…
Browse files Browse the repository at this point in the history
…ntion information
  • Loading branch information
Codycody31 committed Dec 1, 2023
1 parent fc84226 commit 30149dc
Showing 1 changed file with 50 additions and 25 deletions.
75 changes: 50 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,42 @@ npm install @vmgware/js-validator
Here's a basic example to validate a user registration form:

```javascript
const Validator = require('@vmgware/js-validator');
const Validator = require("@vmgware/js-validator");

// Define validation rules
const rules = {
username: { type: 'string', required: true, min: 3 },
email: { type: 'string', required: true, validate: 'email' },
age: { type: 'number', min: 18 }
username: { type: "string", required: true, min: 3 },
email: { type: "string", required: true, validate: "email" },
age: { type: "number", min: 18 },
};

// Custom error messages for each rule
const messages = {
username: { type: 'Username must be a string', required: 'Username is required', min: 'Username must be at least 3 characters' },
email: { type: 'Email must be a string', required: 'Email is required', validate: 'Invalid email format' },
age: { type: 'Age must be a number', min: 'Age must be at least 18' }
username: {
type: "Username must be a string",
required: "Username is required",
min: "Username must be at least 3 characters",
},
email: {
type: "Email must be a string",
required: "Email is required",
validate: "Invalid email format",
},
age: { type: "Age must be a number", min: "Age must be at least 18" },
};

// Initialize the validator
const validator = new Validator(rules, messages);

// Sample data to validate
const userData = { username: 'johndoe', email: '[email protected]', age: 20 };
const userData = { username: "johndoe", email: "[email protected]", age: 20 };

// Perform validation
validator.validate(userData).then(isValid => {
validator.validate(userData).then((isValid) => {
if (isValid) {
console.log('Registration valid');
console.log("Registration valid");
} else {
console.log('Validation errors:', validator.getErrors());
console.log("Validation errors:", validator.getErrors());
}
});
```
Expand All @@ -80,38 +88,49 @@ interface UserProfile {
Now, let's use JS Validator to validate this data:

```typescript
import Validator from '@vmgware/js-validator';
import Validator from "@vmgware/js-validator";

// Define validation rules according to the UserProfile interface
const rules = {
username: { type: 'string', required: true, min: 3 },
email: { type: 'string', required: true, validate: 'email' },
birthdate: { type: 'date', required: true }
username: { type: "string", required: true, min: 3 },
email: { type: "string", required: true, validate: "email" },
birthdate: { type: "date", required: true },
};

// Custom error messages
const messages = {
username: { type: 'Username must be a string', required: 'Username is required', min: 'Username must be at least 3 characters' },
email: { type: 'Email must be a string', required: 'Email is required', validate: 'Invalid email format' },
birthdate: { type: 'Birthdate must be a date', required: 'Birthdate is required' }
username: {
type: "Username must be a string",
required: "Username is required",
min: "Username must be at least 3 characters",
},
email: {
type: "Email must be a string",
required: "Email is required",
validate: "Invalid email format",
},
birthdate: {
type: "Birthdate must be a date",
required: "Birthdate is required",
},
};

// Initialize the validator
const validator = new Validator<UserProfile>(rules, messages);

// Sample user data to validate
const userProfile: UserProfile = {
username: 'janedoe',
email: '[email protected]',
birthdate: new Date('1990-01-01')
username: "janedoe",
email: "[email protected]",
birthdate: new Date("1990-01-01"),
};

// Perform validation
validator.validate(userProfile).then(isValid => {
validator.validate(userProfile).then((isValid) => {
if (isValid) {
console.log('User profile is valid');
console.log("User profile is valid");
} else {
console.log('Validation errors:', validator.getErrors());
console.log("Validation errors:", validator.getErrors());
}
});
```
Expand Down Expand Up @@ -143,7 +162,7 @@ rules.username.custom = async (username) => {
return await isUsernameAvailable(username);
};

messages.username.custom = 'Username is already taken';
messages.username.custom = "Username is already taken";

// Usage remains the same as before
```
Expand Down Expand Up @@ -180,6 +199,12 @@ const validator = new Validator(rules, messages, options);
- `updateMessages(messages)`: Updates the error messages.
- `updateOptions(options)`: Updates the validator's options.

## Contributing

We welcome contributions to this library. Feel free to submit issues and pull requests to help improve the library.

For those writing commits, please follow the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) specification. This helps us maintain a consistent commit history and generate changelogs automatically.

## License

This library is open-sourced under the MIT License, allowing for wide usage and modification. See the [LICENSE](LICENSE) file for details. Feel free to submit issues and pull requests to help improve the library.
Expand Down

0 comments on commit 30149dc

Please sign in to comment.