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
3 changes: 2 additions & 1 deletion src/structs/refinements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ export function pattern<T extends string, S extends any>(
regexp: RegExp
): Struct<T, S> {
return refine(struct, 'pattern', (value) => {
const regexpCopy = new RegExp(regexp) // necessary because of https://stackoverflow.com/questions/3891641/regex-test-only-works-every-other-time
return (
regexp.test(value) ||
regexpCopy.test(value) ||
`Expected a ${struct.type} matching \`/${regexp.source}/\` but received "${value}"`
)
})
Expand Down
16 changes: 16 additions & 0 deletions test/pattern.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { object, pattern, string, is } from '../src'
import { describe, expect, it } from 'vitest'

describe('pattern', () => {
it('runs pattern twice', () => {
const Query = object({
url: pattern(string(), /^example/g),
})

const testQuery = {
url: 'example',
}

expect(is(testQuery, Query)).toEqual(is(testQuery, Query))
})
})
Loading