Skip to content

Commit

Permalink
Merge pull request #261 from jacob-alford/ja/fix-undefined
Browse files Browse the repository at this point in the history
fix: prevent undefined from causing print errors
  • Loading branch information
jacob-alford committed Apr 6, 2023
2 parents 7874f87 + 4893c09 commit e6e9cc8
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "schemata-ts",
"version": "1.4.0",
"version": "1.4.1",
"description": "A collection of Schemata inspired by io-ts-types and validators.js",
"homepage": "https://jacob-alford.github.io/schemata-ts/",
"repository": {
Expand Down
7 changes: 2 additions & 5 deletions src/schemables/WithOptional/instances/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
import * as E from 'fp-ts/Either'
import * as P from 'schemata-ts/base/PrinterBase'
import * as PE from 'schemata-ts/PrintError'
import { WithOptional2 } from 'schemata-ts/schemables/WithOptional/definition'

/**
Expand All @@ -14,9 +13,7 @@ import { WithOptional2 } from 'schemata-ts/schemables/WithOptional/definition'
*/
export const Printer: WithOptional2<P.URI> = {
optional: ea => ({
domainToJson: a =>
a === undefined ? E.left(new PE.InvalidValue(a)) : ea.domainToJson(a),
codomainToJson: e =>
e === undefined ? E.left(new PE.InvalidValue(e)) : ea.codomainToJson(e),
domainToJson: a => (a === undefined ? E.right(undefined) : ea.domainToJson(a)),
codomainToJson: e => (e === undefined ? E.right(undefined) : ea.codomainToJson(e)),
}),
}
2 changes: 0 additions & 2 deletions tests/Codec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ describe('Codec', () => {
* have already been decoded
*/
integers: [1.1, NaN, -Infinity] as any,
/** This turns into an error because the schema is OptionFromUndefined */
activeStatus: O.none,
/* Same here */
id: '' as any,
Expand All @@ -83,7 +82,6 @@ describe('Codec', () => {
),
]),
),
new PE.ErrorAtKey('activeStatus', new PE.InvalidValue(undefined)),
]),
),
)
Expand Down
5 changes: 2 additions & 3 deletions tests/schemata/generic/OptionFromUndefined.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { flow } from 'fp-ts/function'
import * as O from 'fp-ts/Option'

import * as SC from '../../../src/base/SchemaBase'
import * as PE from '../../../src/PrintError'
import * as OptionFromUndefined_ from '../../../src/schemata/generic/OptionFromUndefined'
import { getAllInstances, validateArbitrary } from '../../../test-utils'

Expand Down Expand Up @@ -111,10 +110,10 @@ describe('OptionFromUndefined', () => {
describe('printer', () => {
it('fails on undefined', () => {
expect(OptionFromUndefined.Printer.domainToJson(O.none)).toStrictEqual(
E.left(new PE.InvalidValue(undefined)),
E.right(undefined),
)
expect(OptionFromUndefined.Printer.codomainToJson(undefined)).toStrictEqual(
E.left(new PE.InvalidValue(undefined)),
E.right(undefined),
)
})
it("should print 'some(a)'", () => {
Expand Down

0 comments on commit e6e9cc8

Please sign in to comment.