Skip to content

Commit

Permalink
Adding isUrl validator and toUrl modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
ianwalter committed Jun 17, 2021
1 parent 9c25e7f commit 7643b38
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-tools-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ianwalter/nrg-validation": minor
---

Adding isUrl validator and toUrl modifier
13 changes: 13 additions & 0 deletions packages/nrg-validation/lib/modifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,16 @@ export function lowercase (data) {
lowercase.modify = function modify (data) {
return data.toLowerCase()
}

export function toUrl (input) {
return input && toUrl.modify(input)
}
toUrl.modify = function modify (input) {
let url = input
try {
url = new URL(input).href
} catch (err) {
// Ignore error.
}
return url
}
13 changes: 13 additions & 0 deletions packages/nrg-validation/lib/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,16 @@ export function isShortUsZip (input) {
isShortUsZip.validate = function validateUsShortZip (input) {
return { isValid: input?.length === 5 && digitsRegex.test(input) }
}

export function isUrl (input) {
return resultIsValid(isUrl.validate(input))
}
isUrl.validate = function validateUrl (input) {
let url
try {
url = new URL(input)
} catch (err) {
// Ignore error
}
return { isValid: !!url, url }
}
8 changes: 7 additions & 1 deletion packages/nrg-validation/tests/modifiers.tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from '@ianwalter/bff'
import { trim, lowercase } from '../index.js'
import { trim, lowercase, toUrl } from '../index.js'

test('trim', t => {
t.expect(trim(' Cam ')).toBe('Cam')
Expand All @@ -12,3 +12,9 @@ test('lowercase', t => {
test('lowercase undefined', t => {
t.expect(lowercase()).toBe(undefined)
})

test('toUrl', t => {
t.expect(toUrl('http:ianwalter.dev')).toBe('http://ianwalter.dev/')
t.expect(toUrl('ianwalter')).toBe('ianwalter')
t.expect(toUrl(undefined)).toBe(undefined)
})
11 changes: 10 additions & 1 deletion packages/nrg-validation/tests/validators.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
isShortUsState,
isShortUsZip,
isDateString,
isShortUsDobString
isShortUsDobString,
isUrl
} from '../index.js'

test('isString', t => {
Expand Down Expand Up @@ -142,3 +143,11 @@ test('isShortUsDobString', t => {
const tomorrow = addDays(today, 1)
t.expect(isShortUsDobString(format(tomorrow, 'MM/dd/yyyy'))).toBe(false)
})

test('isUrl', t => {
t.expect(isUrl('a')).toBe(false)
t.expect(isUrl('http://ianwalter.dev?key=value&is=true')).toBe(true)
t.expect(isUrl('http//ianwalter.dev')).toBe(false)
t.expect(isUrl('http://localhost')).toBe(true)
t.expect(isUrl('http://localhost:8080/admin')).toBe(true)
})

0 comments on commit 7643b38

Please sign in to comment.