Skip to content
This repository has been archived by the owner on Dec 12, 2020. It is now read-only.

Commit

Permalink
feat: Add slug validator
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutnik committed Feb 3, 2020
1 parent c4e8f23 commit 117300b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ namespace val {
return check(x => validatorJs.isMultibyte(x), opt && opt.message || "Invalid multi byte")
}

export function numeric(opt?: Opt) {
export function numeric(opt?: Opt & validatorJs.IsNumericOptions) {
return check(x => validatorJs.isNumeric(x), opt && opt.message || "Invalid numeric")
}

Expand All @@ -177,6 +177,10 @@ namespace val {
return check(x => validatorJs.isPostalCode(x, opt && opt.locale || "any"), opt && opt.message || "Invalid postal code")
}

export function slug(opt?: Opt) {
return check(x => validatorJs.isSlug(x), opt && opt.message || "Invalid slug")
}

export function surrogatePair(opt?: Opt) {
return check(x => validatorJs.isSurrogatePair(x), opt && opt.message || "Invalid surrogate pair")
}
Expand Down
12 changes: 12 additions & 0 deletions test/validator-decorator-custom.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,18 @@ describe("Validator Decorator Tests", () => {
expect(result.issues).toMatchObject([{ path: "property", messages: ["Lorem ipsum dolor"] }])
})

test("slug", () => {
@reflect.parameterProperties()
class Dummy {
constructor(
@val.slug({message: "Lorem ipsum"})
public property: string
) { }
}
const result = convert({ property: "-not-slug" }, { ...option, type: Dummy, })
expect(result.issues).toMatchObject([{ path: "property", messages: ["Lorem ipsum"] }])
})

test("surrogatePair", () => {
@reflect.parameterProperties()
class Dummy {
Expand Down
12 changes: 12 additions & 0 deletions test/validator-decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,18 @@ describe("Validator Decorator Tests", () => {
expect(result.issues).toMatchObject([{ path: "property", messages: ["Invalid postal code"] }])
})

test("slug", () => {
@reflect.parameterProperties()
class Dummy {
constructor(
@val.slug()
public property: string
) { }
}
const result = convert({ property: "-not-slug" }, { ...option, type: Dummy, })
expect(result.issues).toMatchObject([{ path: "property", messages: ["Invalid slug"] }])
})

test("surrogatePair", () => {
@reflect.parameterProperties()
class Dummy {
Expand Down

0 comments on commit 117300b

Please sign in to comment.