Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimava committed Sep 18, 2024
1 parent 2a72e0d commit 4ee7bd7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
15 changes: 7 additions & 8 deletions ark/schema/structure/optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ export const assertDefaultValueAssignability = (
value: unknown,
key = ""
): unknown => {
if (!isPrimitive(value) && typeof value !== "function") {
throwParseError(writeNonPrimitiveNonFunctionDefaultValueMessage(key, value))
}
if (!isPrimitive(value) && typeof value !== "function")
throwParseError(writeNonPrimitiveNonFunctionDefaultValueMessage(key))

const out = node.in(typeof value === "function" ? value() : value)
if (out instanceof ArkErrors) {
if (out instanceof ArkErrors)
throwParseError(writeUnassignableDefaultValueMessage(out.message, key))
}

return value
}

Expand All @@ -124,7 +124,6 @@ export type writeUnassignableDefaultValueMessage<
> = `Default value ${defaultValue} is not assignable to ${baseDef}`

export const writeNonPrimitiveNonFunctionDefaultValueMessage = (
key: string,
value: unknown
key: string
): string =>
`Default value${key && ` for key ${key}`} is not primitive so it should be constructor function (was ${printable(value)})`
`Default value${key && ` for key ${key}`} is not primitive so it should be specified as a function like () => ({my: 'object'})`
2 changes: 1 addition & 1 deletion ark/type/__tests__/defaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ contextualize(() => {
const time = v1.date.getTime()
v1.date.setMilliseconds(123)
const v2 = t.assert({})
attest(v1.date.getTime()).equals(time)
attest(v2.date.getTime()).equals(time)
})

it("true", () => {
Expand Down
7 changes: 5 additions & 2 deletions ark/type/parser/shift/operator/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ export const parseDefault = (s: DynamicStateWithRoot): BaseRoot => {
// token from which it was parsed
if (!defaultNode.hasKind("unit"))
return s.error(writeNonLiteralDefaultMessage(defaultNode.expression))

return baseNode.default(defaultNode.unit)
const defaultValue =
defaultNode.unit instanceof Date ?
() => new Date(defaultNode.unit as Date)
: defaultNode.unit
return baseNode.default(defaultValue)
}

export type parseDefault<root, unscanned extends string> =
Expand Down

0 comments on commit 4ee7bd7

Please sign in to comment.