shape-json can't serialize a Type shape
ryan-mars opened this issue · comments
Ryan Mierzejewski commented
Given a file:
// main.ts
import {
array,
integer,
map,
Minimum,
MinLength,
number,
string,
Type,
} from "@punchcard/shape";
import { Json } from "@punchcard/shape-json";
class MyType extends Type({
key: string.apply(MinLength(1)), // apply constraints with Traits
count: integer.apply(Minimum(0)),
rating: number,
array: array(string),
map: map(string),
}) {}
const mapper = Json.mapper(MyType);
const instance = new MyType({
key: "must be a string",
count: 1,
rating: 1.1,
array: ["array", "of", "strings"],
map: {
key: "map of string value",
},
});
const json = mapper.write(instance);
console.log(json);
Compile and run produces an error:
.../try-shapes/node_modules/@punchcard/shape-json/lib/json.js:158
write: (arr) => arr.map(i => item.write(i)),
^
TypeError: Cannot read property 'map' of undefined
at Object.write (.../try-shapes/node_modules/@punchcard/shape-json/lib/json.js:158:37)
at Object.write (.../try-shapes/node_modules/@punchcard/shape-json/lib/json.js:197:43)
at Object.<anonymous> (.../try-shapes/out/main.js:32:19)
at Module._compile (node:internal/modules/cjs/loader:1102:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10)
at Module.load (node:internal/modules/cjs/loader:967:32)
at Function.Module._load (node:internal/modules/cjs/loader:807:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47
sam commented
Looks like a problem with array serialization. https://github.com/punchcard/punchcard/blob/dbbd1282a7733e6886571f5f6e537168af95ff40/packages/%40punchcard/shape-json/src/json.ts#L217
We have a test though https://github.com/punchcard/punchcard/blob/dbbd1282a7733e6886571f5f6e537168af95ff40/packages/%40punchcard/shape-json/test/json.test.ts#L35
I’ll take a look - thanks for the repro
Ryan Mierzejewski commented
tsconfig was the offender
👇🏻 works
{
"compilerOptions": {
"target": "es2018",
"lib": [
"es2019",
"es5",
"es6",
"esnext.asynciterable",
"dom"
],
"downlevelIteration": true,
"module": "commonjs",
"strict": true,
"strictPropertyInitialization": false,
"strictNullChecks": true,
"noImplicitAny": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"inlineSourceMap": true,
"forceConsistentCasingInFileNames": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"resolveJsonModule": true,
"declaration": true,
"declarationMap": true
},
"exclude": [
"node_modules",
"build"
]
}
sam commented
To clarify - the target ES version was ES5 and punchcard is ES2018.