Skip to content
Open
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Validasaur is Deno validation library slightly inspired by Laravel Validation.
- [`isIPv6`](#isipv6)
- [`isNumber`](#isnumber)
- [`isNumeric`](#isnumeric)
- [`isAlphaNumeric`](#isAlphaNumeric)
- [`isString`](#isstring)
- [`lengthBetween(minLength: number, maxLength: number)`](#lengthbetweenminlength-number-maxlength-number)
- [`match(regex: RegExp, trim: boolean = false)`](#matchregex-regexp-trim-boolean--false)
Expand All @@ -43,6 +44,7 @@ Validasaur is Deno validation library slightly inspired by Laravel Validation.
- [`notIn(disallowedValues: PrimitiveTypes[])`](#notindisallowedvalues-primitivetypes)
- [`notNull`](#notnull)
- [`nullable`](#nullable)
- [`nullable`](#alphaNumareric)
- [`numberBetween(minValue: number, maxValue: number)`](#numberbetweenminvalue-number-maxvalue-number)
- [`requiredIf(field: string, fieldValue: any)`](#requirediffield-string-fieldvalue-any)
- [`requiredUnless(field: string, fieldValue: any)`](#requiredunlessfield-string-fieldvalue-any)
Expand Down Expand Up @@ -520,6 +522,13 @@ Same as `asNumber`, but it allows numeric string.
* Invalid values: `"1.0abc"`, `"x.1"`, etc.
* Valid values: `1`, `1.5`, `"2"`, `"2.5"`, etc.

#### `isAlphaNumeric`

Value must contain alpha letters and numeric characters.

* Invalid values: `"123"`, `"abc"`, "123&$"`, `"abc#@"`, etc.
* Valid values: `abc1123`, `123ABC`, `"abcDEF123"`, etc.

#### `isString`

Value under this field must be a string.
Expand Down
1 change: 1 addition & 0 deletions src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const defaultMessages: ValidationMessages = {
isNumber: ":attr must be a number",
isNumeric: ":attr must be numeric",
isString: ":attr must be a string",
isAlphaNumeric: ":attr must contain alpha letters and numeric characters",
lengthBetween:
":attr characters length must be between :minLength-:maxLength",
match: ":attr format is incorrect",
Expand Down
1 change: 1 addition & 0 deletions src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export * from "./rules/is_numeric.ts";
export * from "./rules/is_date.ts";
export * from "./rules/is_email.ts";
export * from "./rules/is_string.ts";
export * from "./rules/is_alpha_numeric";
export * from "./rules/length_between.ts";
export * from "./rules/max_length.ts";
export * from "./rules/max_number.ts";
Expand Down
9 changes: 9 additions & 0 deletions src/rules/is_alpha_numeric.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Validity } from "../types.ts";
import { invalid } from "../utils.ts";

export function isAlphaNumeric(value: any): Validity {

if (typeof value === "string" && !(value as string).match(/^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]+$/)) {
return invalid("isAlphaNumeric", { value });
}
}
36 changes: 36 additions & 0 deletions tests/rules/is_alpha_numeric.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { isAlphaNumeric } from "../../src/rules/is_alpha_numeric";
import { invalid } from "../../src/utils.ts";
import { assertInvalid, assertValid } from "../utils.ts";

Deno.test("rules.isAlphaNumeric('') should be invalid", () => {
assertInvalid(isAlphaNumeric(""), invalid("isAlphaNumeric", { value: "" }));
});

Deno.test("rules.isAlphaNumeric('123') should be invalid", () => {
assertInvalid(isAlphaNumeric('123'), invalid("isAlphaNumeric", { value: '123' }));
});

Deno.test("rules.isInt('abc') should be invalid", () => {
assertInvalid(isAlphaNumeric('abc'), invalid("isAlphaNumeric", { value: 'abc' }));
});

Deno.test("rules.isInt('abcDEF') should be invalid", () => {
assertInvalid(isAlphaNumeric('abcDEF'), invalid("isAlphaNumeric", { value: 'abcDEF' }));
});


Deno.test("rules.isAlphaNumeric('abc123') should be valid", () => {
assertValid(isAlphaNumeric('abc123'));
});


Deno.test("rules.isAlphaNumeric('123ABC') should be valid", () => {
assertValid(isAlphaNumeric('123ABC'));
});


Deno.test("rules.isAlphaNumeric('abcDEF123') should be valid", () => {
assertValid(isAlphaNumeric('abcDEF123'));
});