Skip to content

Commit

Permalink
trim string before checking length and match
Browse files Browse the repository at this point in the history
  • Loading branch information
hoeck committed Dec 6, 2023
1 parent d230dfb commit 5e5b52c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
15 changes: 9 additions & 6 deletions src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ const stringRuntype = setupInternalRuntype<string>(
*
* Options:
*
* trim .. when true, remove leading and trailing spaces from the string
* trimming is applied before the optional length and regex checks
* minLength .. reject strings that are shorter than that
* maxLength .. reject strings that are longer than that
* trim .. when true, remove leading and trailing spaces from the string
* match .. reject strings that do not match against provided RegExp
*/
export function string(options?: {
trim?: boolean
minLength?: number
maxLength?: number
trim?: boolean
match?: RegExp
}): Runtype<string> {
if (!options) {
Expand All @@ -44,12 +45,14 @@ export function string(options?: {

return setupInternalRuntype(
(v, failOrThrow) => {
const s: string = (stringRuntype as InternalRuntype<any>)(v, failOrThrow)
const r: string = (stringRuntype as InternalRuntype<any>)(v, failOrThrow)

if (isFail(s)) {
return propagateFail(failOrThrow, s, v)
if (isFail(r)) {
return propagateFail(failOrThrow, r, v)
}

const s = trim ? r.trim() : r

if (minLength !== undefined && s.length < minLength) {
return createFail(
failOrThrow,
Expand All @@ -74,7 +77,7 @@ export function string(options?: {
)
}

return trim ? s.trim() : s
return s
},
{ isPure },
)
Expand Down
2 changes: 1 addition & 1 deletion test/nonStrict.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('nonStrict', () => {
})
})

// same as sloppyRecord
// same as the former sloppyRecord test
it('ignores object attributes', () => {
const runType = st.nonStrict(
st.record({
Expand Down
49 changes: 48 additions & 1 deletion test/string.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { expectAcceptValuesPure, expectRejectValues, st } from './helpers'
import {
expectAcceptValuesImpure,
expectAcceptValuesPure,
expectRejectValues,
st,
} from './helpers'

describe('string', () => {
it('accepts strings', () => {
Expand Down Expand Up @@ -54,6 +59,44 @@ describe('string', () => {
expect(rt('foO')).toEqual('foO')
})

it('trims before it checks the string minLength', () => {
const rt = st.string({ trim: true, minLength: 1 })

expect(rt('a')).toEqual('a')
expectAcceptValuesImpure(
rt,
[
[' a', 'a'],
[' a ', 'a'],
[' aa', 'aa'],
[' a a ', 'a a'],
],
true,
)

expectRejectValues(rt, [' ', ' ', ' ', '', 0, []])
})

it('trims before it checks the string maxLength', () => {
const rt = st.string({ trim: true, maxLength: 3 })

expect(rt('abc')).toEqual('abc')
expectAcceptValuesImpure(
rt,
[
[' abc', 'abc'],
['abc ', 'abc'],
[' a c ', 'a c'],
[' ', ''],
[' ', ''],
['a ', 'a'],
],
true,
)

expectRejectValues(rt, ['abcd', 'abcd ', ' abcd ', 'a d ', 0, []])
})

it('rejects non-strings', () => {
expectRejectValues(
st.string(),
Expand All @@ -73,4 +116,8 @@ describe('string', () => {
'expected the string to match /^[0-9a-f]{8}$/',
)
})

it('trims before applying match', () => {
const rt = st.string({ match: /^[0-9a-f]{8}$/ })
})
})

0 comments on commit 5e5b52c

Please sign in to comment.