Skip to content

Commit

Permalink
add url validation public method (adamgibbons#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
espindolaa authored Sep 18, 2024
1 parent 84c3e76 commit cc8be98
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
formatHeader,
formatEvent,
formatFooter,
urlRegex,
} from './pipeline'

function buildHeaderAndValidate(header) {
Expand Down Expand Up @@ -76,3 +77,7 @@ export function createEvents (events, headerAttributesOrCb, cb) {

return resolvedCb(returnValue.error, returnValue.value)
}

export function isValidURL(url) {
return urlRegex.test(url);
}
5 changes: 3 additions & 2 deletions src/pipeline/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { buildHeader, buildEvent } from './build'
import { formatHeader, formatEvent, formatFooter } from './format'
import { validateHeader, validateHeaderAndEvent } from './validate'
import { validateHeader, validateHeaderAndEvent, urlRegex } from './validate'

export {
buildHeader,
Expand All @@ -9,5 +9,6 @@ export {
formatEvent,
formatFooter,
validateHeader,
validateHeaderAndEvent
validateHeaderAndEvent,
urlRegex
}
2 changes: 1 addition & 1 deletion src/schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as yup from 'yup'
// taken from https://github.com/jquense/yup/issues/224#issuecomment-417172609
// This does mean that the url validation error is
// "url must match the following: ...." as opposed to "url must be a valid URL"
const urlRegex = /^(?:([a-z0-9+.-]+):\/\/)(?:\S+(?::\S*)?@)?(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*\.?)(?::\d{2,5})?(?:[/?#]\S*)?$/
export const urlRegex = /^(?:([a-z0-9+.-]+):\/\/)(?:\S+(?::\S*)?@)?(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*\.?)(?::\d{2,5})?(?:[/?#]\S*)?$/

const dateTimeSchema = ({ required }) => yup.lazy((value) => {
if (typeof value === 'number') {
Expand Down
38 changes: 37 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai'
import { createEvent, createEvents } from '../src'
import { createEvent, createEvents, isValidURL } from '../src'

const invalidAttributes = { start: [] }
const validAttributes = { start: [2000, 10, 5, 5, 0], duration: { hours: 1 } }
Expand Down Expand Up @@ -117,4 +117,40 @@ describe('ics', () => {
})
})
})

describe(".isValidURL", () => {
[
{
result: true,
condition: "http urls",
url: "http://domain.com",
},
{
result: true,
condition: "https urls",
url: "https://domain.com",
},
{
result: true,
condition: "localhost urls",
url: "http://localhost:8080",
},
{
result: false,
condition: "urls that start with www",
url: "www.domain.com",
},
{
result: false,
condition: "urls that start with domain",
url: "domain.com",
},
].forEach((test) => {
it(`${test.result ? "passes" : "fails"} for ${test.condition}`, () => {
const response = isValidURL(test.url);

expect(response).equal(test.result);
})
})
})
})

0 comments on commit cc8be98

Please sign in to comment.