Skip to content

Commit

Permalink
Fix Range Scalar, Fix Test Coverage
Browse files Browse the repository at this point in the history
Turns out Yup treats 0 as non-positive, so switched to using the min validation instead. Added tests to cover this problem.
  • Loading branch information
Saeris committed Sep 10, 2020
1 parent 0788a27 commit 1b063df
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
41 changes: 41 additions & 0 deletions src/__TEST__/range.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,47 @@ describe(`rangeFactory`, () => {
})
})

const { resolver: ExampleZeroStart } = rangeFactory({
name: `ExampleRange`,
start: 0,
end: 1
})

describe(`valid:allowsZero`, () => {
describe(`as int`, () => {
it(`serialize`, () => {
expect(ExampleZeroStart.serialize(0)).toBe(0)
})

it(`parseValue`, () => {
expect(ExampleZeroStart.parseValue(0)).toBe(0)
})

it(`parseLiteral`, () => {
expect(
// @ts-ignore
ExampleZeroStart.parseLiteral({ value: 0, kind: Kind.INT }, {})
).toBe(0)
})
})

describe(`as string`, () => {
it(`serialize`, () => {
expect(ExampleZeroStart.serialize(`0`)).toBe(0)
})

it(`parseValue`, () => {
expect(ExampleZeroStart.parseValue(`0`)).toBe(0)
})

it(`parseLiteral`, () => {
expect(
ExampleZeroStart.parseLiteral({ value: `0`, kind: Kind.INT }, {})
).toBe(0)
})
})
})

const { resolver: ExampleFloatRange } = rangeFactory({
name: `ExampleFloatRange`,
start: 0.25,
Expand Down
2 changes: 1 addition & 1 deletion src/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const rangeFactory = ({ name, start, end, float = false }: Config) => {
.typeError(`Value is not a number: ${value}`)
.notOneOf([Infinity, -Infinity], `Value is not a finite number: ${value}`)
.required(`Value is not a number: ${value}`)
.positive(`Value is not a positive number: ${value}`)
.min(0, `Value is not a positive number: ${value}`)
.validateSync(value)
if (!float) {
yupNumber()
Expand Down
14 changes: 5 additions & 9 deletions src/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ const validate = (value: string) =>
yupString()
.strict(true)
.typeError(`Value is not string: ${value}`)
.test(`uri`, `Value is not a valid URL: ${value}`, val => {
try {
const result = parse(val as string)
if (!result.scheme) return false
return true
} catch (err) {
return false
}
})
.test(
`uri`,
`Value is not a valid URL: ${value}`,
val => !!parse(val as string).scheme
)
.validateSync(value)

export const URLScalar = `scalar URL`
Expand Down

0 comments on commit 1b063df

Please sign in to comment.