@kingshott/iodine
Advanced tools
Comparing version 5.1.2 to 6.0.0
{ | ||
"name": "@kingshott/iodine", | ||
"version": "5.1.2", | ||
"version": "6.0.0", | ||
"description": "A micro client-side validation library", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -142,2 +142,9 @@ <!-- Screenshot --> | ||
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 | ||
``` | ||
## Custom messages (localisation) | ||
@@ -147,3 +154,3 @@ | ||
Iodine will automatically swap `[PARAM]` placeholders with the parameters supplied in the `getErrorMessage` method. As such, you should insert this placeholder at the appropriate position in your new error message e.g. | ||
Iodine will automatically swap the `[FIELD]` and `[PARAM]` placeholders with the parameters supplied in the `getErrorMessage` method. As such, you should insert this placeholder at the appropriate position in your new error message e.g. | ||
@@ -155,2 +162,8 @@ ```js | ||
If no field name is provided when calling `getErrorMessage`, by default it will be replaced with "Value". You can change this by calling `setDefaultFieldName` | ||
```js | ||
Iodine.setDefaultFieldName('Valeur'); | ||
``` | ||
You can also add or update a single error | ||
@@ -222,3 +235,3 @@ ```js | ||
Iodine.addRule('equals', (value, param) => value == param); | ||
Iodine.setErrorMessages({ equals : `Value must be equal to '[PARAM]'` }); | ||
Iodine.setErrorMessages({ equals : `[FIELD] must be equal to '[PARAM]'` }); | ||
``` | ||
@@ -225,0 +238,0 @@ |
@@ -18,2 +18,3 @@ /* | ||
this.messages = this._defaultMessages(); | ||
this.defaultFieldName = "Value"; | ||
} | ||
@@ -46,27 +47,27 @@ | ||
afterOrEqual: `The date must be after or equal to: '[PARAM]'`, | ||
array: `Value must be an array`, | ||
array: `[FIELD] must be an array`, | ||
before: `The date must be before: '[PARAM]'`, | ||
beforeOrEqual: `The date must be before or equal to: '[PARAM]'`, | ||
boolean: `Value must be true or false`, | ||
date: `Value must be a date`, | ||
different: `Value must be different to '[PARAM]'`, | ||
endingWith: `Value must end with '[PARAM]'`, | ||
email: `Value must be a valid email address`, | ||
falsy: `Value must be a falsy value (false, 'false', 0 or '0')`, | ||
in: `Value must be one of the following options: [PARAM]`, | ||
integer: `Value must be an integer`, | ||
json: `Value must be a parsable JSON object string`, | ||
maximum: `Value must not be greater than '[PARAM]' in size or character length`, | ||
minimum: `Value must not be less than '[PARAM]' in size or character length`, | ||
notIn: `Value must not be one of the following options: [PARAM]`, | ||
numeric: `Value must be numeric`, | ||
optional: `Value is optional`, | ||
regexMatch: `Value must satisify the regular expression: [PARAM]`, | ||
required: `Value must be present`, | ||
same: `Value must be '[PARAM]'`, | ||
startingWith: `Value must start with '[PARAM]'`, | ||
string: `Value must be a string`, | ||
truthy: `Value must be a truthy value (true, 'true', 1 or '1')`, | ||
url: `Value must be a valid url`, | ||
uuid: `Value must be a valid UUID`, | ||
boolean: `[FIELD] must be true or false`, | ||
date: `[FIELD] must be a date`, | ||
different: `[FIELD] must be different to '[PARAM]'`, | ||
endingWith: `[FIELD] must end with '[PARAM]'`, | ||
email: `[FIELD] must be a valid email address`, | ||
falsy: `[FIELD] must be a falsy value (false, 'false', 0 or '0')`, | ||
in: `[FIELD] must be one of the following options: [PARAM]`, | ||
integer: `[FIELD] must be an integer`, | ||
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`, | ||
notIn: `[FIELD] must not be one of the following options: [PARAM]`, | ||
numeric: `[FIELD] must be numeric`, | ||
optional: `[FIELD] is optional`, | ||
regexMatch: `[FIELD] must satisify the regular expression: [PARAM]`, | ||
required: `[FIELD] must be present`, | ||
same: `[FIELD] must be '[PARAM]'`, | ||
startingWith: `[FIELD] must start with '[PARAM]'`, | ||
string: `[FIELD] must be a string`, | ||
truthy: `[FIELD] must be a truthy value (true, 'true', 1 or '1')`, | ||
url: `[FIELD] must be a valid url`, | ||
uuid: `[FIELD] must be a valid UUID`, | ||
}; | ||
@@ -87,6 +88,9 @@ } | ||
**/ | ||
getErrorMessage(rule, arg = undefined) { | ||
getErrorMessage(rule, args = undefined) { | ||
let { param, field } = | ||
typeof args === "object" ? args : { param: args, field: undefined }; | ||
const chunks = rule.split(":"); | ||
let key = chunks.shift(); | ||
let param = arg || chunks.join(":"); | ||
param = param || chunks.join(":"); | ||
@@ -104,5 +108,9 @@ if (["after", "afterOrEqual", "before", "beforeOrEqual"].includes(key)) { | ||
return [null, undefined, ""].includes(param) | ||
let message = [null, undefined, ""].includes(param) | ||
? this.messages[key] | ||
: this.messages[key].replace("[PARAM]", param); | ||
return [null, undefined, ""].includes(field) | ||
? message.replace("[FIELD]", this.defaultFieldName) | ||
: message.replace("[FIELD]", field); | ||
} | ||
@@ -403,2 +411,9 @@ | ||
} | ||
/** | ||
* Replace the default field name with a new value. | ||
*/ | ||
setDefaultFieldName(fieldName) { | ||
this.defaultFieldName = fieldName; | ||
} | ||
} | ||
@@ -405,0 +420,0 @@ |
@@ -5,2 +5,3 @@ import { Iodine as Library } from "../src/iodine"; | ||
const defaultMessages = Iodine.messages; | ||
const defaultFieldName = Iodine.defaultFieldName; | ||
@@ -14,2 +15,3 @@ /** | ||
Iodine.setErrorMessages(defaultMessages); | ||
Iodine.setDefaultFieldName(defaultFieldName); | ||
}); | ||
@@ -479,2 +481,19 @@ | ||
); | ||
expect(Iodine.getErrorMessage("endingWith", { field: "Song title" })).toBe( | ||
`Song title must end with '[PARAM]'` | ||
); | ||
expect( | ||
Iodine.getErrorMessage("endingWith:world", { field: "Song title" }) | ||
).toBe(`Song title must end with 'world'`); | ||
expect( | ||
Iodine.getErrorMessage("endingWith", { | ||
field: "Song title", | ||
param: "world", | ||
}) | ||
).toBe(`Song title must end with 'world'`); | ||
expect(Iodine.getErrorMessage("endingWith", { param: "world" })).toBe( | ||
`Value must end with 'world'` | ||
); | ||
expect(Iodine.getErrorMessage(`after:${time}`)).toBe( | ||
@@ -496,2 +515,3 @@ `The date must be after: 'May 2, 2020, ${hour}:17'` | ||
endingWith: "Hello, [PARAM]", | ||
startingWith: "[FIELD]: [PARAM] says, 'hello'", | ||
}); | ||
@@ -501,5 +521,42 @@ expect(Iodine.getErrorMessage("array")).toBe("Hello world"); | ||
expect(Iodine.getErrorMessage("endingWith", "John")).toBe("Hello, John"); | ||
expect(Iodine.getErrorMessage("endingWith", { param: "John" })).toBe( | ||
"Hello, John" | ||
); | ||
expect(Iodine.getErrorMessage("endingWith", "John")).toBe("Hello, John"); | ||
expect(Iodine.getErrorMessage("startingWith:Paul")).toBe( | ||
"Value: Paul says, 'hello'" | ||
); | ||
expect(Iodine.getErrorMessage("startingWith", "Paul")).toBe( | ||
"Value: Paul says, 'hello'" | ||
); | ||
expect(Iodine.getErrorMessage("startingWith", { param: "Paul" })).toBe( | ||
"Value: Paul says, 'hello'" | ||
); | ||
expect(Iodine.getErrorMessage("startingWith:Paul", { field: "Name" })).toBe( | ||
"Name: Paul says, 'hello'" | ||
); | ||
expect( | ||
Iodine.getErrorMessage("startingWith", { field: "Name", param: "Paul" }) | ||
).toBe("Name: Paul says, 'hello'"); | ||
}); | ||
/** | ||
* Confirm the defualt field name can be replaced. | ||
*/ | ||
test("it can replace the default field name", () => { | ||
Iodine.setDefaultFieldName("Input"); | ||
expect(Iodine.getErrorMessage("array")).toBe("Input must be an array"); | ||
expect(Iodine.getErrorMessage("endingWith")).toBe( | ||
`Input must end with '[PARAM]'` | ||
); | ||
expect(Iodine.getErrorMessage("endingWith:world")).toBe( | ||
`Input must end with 'world'` | ||
); | ||
expect(Iodine.getErrorMessage("endingWith", { param: "world" })).toBe( | ||
`Input must end with 'world'` | ||
); | ||
}); | ||
/** | ||
* Confirm that a single error message can be replaced. | ||
@@ -506,0 +563,0 @@ * |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
206680
1037
257