Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Min max validation changes #23

Merged
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
32 changes: 22 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ Some rules require extra parameters e.g.
let item_1 = 7;
let item_2 = 4;

Iodine.isMinimum(item_1, 5); // true
Iodine.isMinimum(item_2, 5); // false
Iodine.isMin(item_1, 5); // true
Iodine.isMin(item_2, 5); // false
```

For multiple checks, you can supply the parameters by appending them to the rule with a semicolon separator e.g.
Expand All @@ -104,8 +104,8 @@ For multiple checks, you can supply the parameters by appending them to the rule
let item_1 = 7;
let item_2 = 4;

Iodine.is(item_1, ['required', 'integer', 'minimum:5']); // true
Iodine.is(item_2, ['required', 'integer', 'minimum:5']); // string - 'minimum:5'
Iodine.is(item_1, ['required', 'integer', 'min:5']); // true
Iodine.is(item_2, ['required', 'integer', 'min:5']); // string - 'min:5'
```

## Optional values
Expand Down Expand Up @@ -135,15 +135,15 @@ Iodine.getErrorMessage('array'); // string
When dealing with rules that have parameters, the `getErrorMessage` method allows you to supply the rule either as a combined `string` or as two arguments (the rule and parameter) e.g.

```js
Iodine.getErrorMessage('minimum:7'); // string
Iodine.getErrorMessage('minimum', 7); // string
Iodine.getErrorMessage('min:7'); // string
Iodine.getErrorMessage('min', 7); // string
```

If you want the field name to appear within the error message, you can pass an object as the second parameter to the `getErrorMessage` method.

```js
Iodine.getErrorMessage('minimum:7', { field: ''}); // string
Iodine.getErrorMessage('minimum', { field: '', param: 7}); // string
Iodine.getErrorMessage('min:7', { field: ''}); // string
Iodine.getErrorMessage('min', { field: '', param: 7}); // string
```

## Custom messages (localisation)
Expand Down Expand Up @@ -188,8 +188,10 @@ The following validation rules are available:
| isIn(array) | Verify that the item is within the given `array`
| isInteger | Verify that the item is an `integer`
| isJson | Verify that the item is a parsable JSON object `string`
| isMaximum(limit) | Verify that the item does not exceed the given limit (number or char length)
| isMinimum(limit) | Verify that the item is not under the given limit (number or char length)
| isMaxLength(limit) | Verify that the item's character length does not exceed the given limit
| isMinLength(limit) | Verify that the item's character length is not under the given limit
| isMax(limit) | Verify that the item's numerical value does not exceed the given limit
| isMinLength(limit) | Verify that the item's numberical value is not under the given limit
| isNotIn(array) | Verify that the item is not within the given `array`
| isNumeric | Verify that the item is `number` or a numeric `string`
| isOptional | Allow for optional values (only for use with multiple checks)
Expand All @@ -204,6 +206,16 @@ The following validation rules are available:

Examine the tests for examples of how to use each rule.

## Deprecated Rules

The following rules are deprecated and should not be used:

| Rule | Description | Replacement |
| ----------------------------- | ------------------------------------------------------------------------------- | ----------------------------------------------------------- |
| isMaximum(limit) | Verify that the item does not exceed the given limit (number or char length) | isMax for numerical value. isMaxLength for character length
| isMinimum(limit) | Verify that the item is not under the given limit (number or char length) | isMin for numerical value. isMinLength for character length


## Custom rules

Iodine allows you to add your own custom validation rules through the `addRule` method. This method excepts two parameters. The first, is the name of the rule. The second, is the `closure` that Iodine should execute when calling the rule e.g.
Expand Down
42 changes: 42 additions & 0 deletions src/iodine.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export class Iodine {
json: `[FIELD] must be a parsable JSON object string`,
maximum: `[FIELD] must not be greater than '[PARAM]' in size or character length`,
minimum: `[FIELD] must not be less than '[PARAM]' in size or character length`,
max: `[FIELD] must be less than or equal to [PARAM]`,
min: `[FIELD] must be greater than or equal to [PARAM]`,
maxlength: `[FIELD] must not be greater than '[PARAM]' in character length`,
minlength: `[FIELD] must not be less than '[PARAM]' character length`,
notIn: `[FIELD] must not be one of the following options: [PARAM]`,
numeric: `[FIELD] must be numeric`,
optional: `[FIELD] is optional`,
Expand Down Expand Up @@ -242,6 +246,7 @@ export class Iodine {
*
**/
isMaximum(value, limit) {
console.warn("isMaximum (maximum) is deprecated. Use isMax (max) for validating the maximum value of a number or isMaxLength (maxlength) for validating the length of a string");
value = typeof value === "string" ? value.length : value;

return parseFloat(value) <= limit;
Expand All @@ -252,11 +257,48 @@ export class Iodine {
*
**/
isMinimum(value, limit) {
console.warn("isMinimum (minimum) is deprecated. Use isMin (min) for validating the minimum value of a number or isMinLength (minlength) for validating the length of a string");
value = typeof value === "string" ? value.length : value;

return parseFloat(value) >= limit;
}

/**
* Determine if the given number is less than or equal to the maximum limit.
*
*/
isMax(value, limit) {
return parseFloat(value) <= limit;
}

/**
* Determine if the given number is greater than or equal to the minimum limit.
*
*/
isMin(value, limit) {
return parseFloat(value) >= limit;
}

/**
* Determine if the given value string length is less than or equal to the maximum limit.
*
*/
isMaxLength(value, limit) {
if (typeof value !== "string") return false;

return value.length <= limit;
}

/**
* Determine if the given value string length is greater than or equal to the minimum limit.
*
*/
isMinLength(value, limit) {
if (typeof value !== "string") return false;

return value.length >= limit;
}

/**
* Determine if the given value is not within the given array of options.
*
Expand Down
52 changes: 52 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,58 @@ describe("validate available rules", () => {
expect(Iodine.isMinimum("1234", 5)).toBe(false);
});

/**
* Confirm that the 'isMax' method works correctly.
*
**/
test("maximum numeric values", () => {
expect(Iodine.isMax(1, 5)).toBe(true);
expect(Iodine.isMax(5, 5)).toBe(true);
expect(Iodine.isMax(6, 5)).toBe(false);
expect(Iodine.isMax("1", 5)).toBe(true);
expect(Iodine.isMax("5", 5)).toBe(true);
expect(Iodine.isMax("6", 5)).toBe(false);
});

/**
* Confirm that the 'isMin' method works correctly.
*
**/
test("minimum numeric values", () => {
expect(Iodine.isMin(6, 5)).toBe(true);
expect(Iodine.isMin(5, 5)).toBe(true);
expect(Iodine.isMin(4, 5)).toBe(false);
expect(Iodine.isMin("6", 5)).toBe(true);
expect(Iodine.isMin("5", 5)).toBe(true);
expect(Iodine.isMin("4", 5)).toBe(false);
});

/**
* Confirm that the 'isMaxLength' method works correctly.
*
**/
test("maximum string length", () => {
expect(Iodine.isMaxLength(1, 5)).toBe(false);
expect(Iodine.isMaxLength(5, 5)).toBe(false);
expect(Iodine.isMaxLength(6, 5)).toBe(false);
expect(Iodine.isMaxLength("1", 5)).toBe(true);
expect(Iodine.isMaxLength("12345", 5)).toBe(true);
expect(Iodine.isMaxLength("123456", 5)).toBe(false);
});

/**
* Confirm that the 'isMinLength' method works correctly.
*
**/
test("minimum string length", () => {
expect(Iodine.isMinLength(6, 5)).toBe(false);
expect(Iodine.isMinLength(5, 5)).toBe(false);
expect(Iodine.isMinLength(4, 5)).toBe(false);
expect(Iodine.isMinLength("123456", 5)).toBe(true);
expect(Iodine.isMinLength("12345", 5)).toBe(true);
expect(Iodine.isMinLength("1234", 5)).toBe(false);
});

/**
* Confirm that the 'isNotIn' method works correctly.
*
Expand Down