Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ark/schema/roots/morph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ export class MorphNode extends BaseRoot<Morph.Declaration> {
}

protected innerToJsonSchema(ctx: ToJsonSchema.Context): JsonSchema {
// an input schema describes what the morph accepts, so convert its input
// directly (rather than rawIn, which would drop defaults it contains)
if (ctx.io === "in") {
return (
this.declaredIn ??
this.inner.in ??
$ark.intrinsic.unknown.internal
).toJsonSchemaRecurse(ctx)
}

return ctx.fallback.morph({
code: "morph",
base: this.rawIn.toJsonSchemaRecurse(ctx),
Expand Down
10 changes: 7 additions & 3 deletions ark/schema/roots/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ export abstract class BaseRoot<
return { value: out }
},
jsonSchema: {
input: opts => toStandardJsonSchema(this.rawIn, opts),
// rawIn would drop defaults, so the input converts this type with io: "in"
input: opts => toStandardJsonSchema(this, opts, "in"),
output: opts => toStandardJsonSchema(this.rawOut, opts)
}
}
Expand Down Expand Up @@ -727,7 +728,8 @@ const validateStandardJsonSchemaTarget = (

const toStandardJsonSchema = (
node: BaseRoot,
opts: StandardJSONSchemaV1.Options
opts: StandardJSONSchemaV1.Options,
io?: "in"
): Record<string, unknown> => {
const ctx = mergeToJsonSchemaConfigs(node.$.resolvedConfig.toJsonSchema, {
target: validateStandardJsonSchemaTarget(opts.target),
Expand All @@ -739,7 +741,9 @@ const toStandardJsonSchema = (
ToJsonSchema.standardFallback[code]
: handler
]) as ToJsonSchema.HandlerByCode
return node.toJsonSchema({ ...ctx, fallback }) as never
const standardCtx: ToJsonSchema.Context = { ...ctx, fallback }
if (io) standardCtx.io = io
return node.toJsonSchema(standardCtx) as never
}

export const exclusivizeRangeSchema = <schema extends UnknownRangeSchema>(
Expand Down
2 changes: 2 additions & 0 deletions ark/schema/shared/toJsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,7 @@ export declare namespace ToJsonSchema {

export interface Context extends Required<Options> {
fallback: HandlerByCode
// set by ~standard.jsonSchema.input to convert a type as it is accepted
io?: "in"
}
}
7 changes: 6 additions & 1 deletion ark/schema/structure/structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,12 @@ export class StructureNode extends BaseConstraint<Structure.Declaration> {
}
}

if (this.undeclared && !schema.additionalProperties)
// undeclared keys that would be deleted are allowed on input
const rejectsUndeclared =
this.undeclared === "reject" ||
(this.undeclared === "delete" && ctx.io !== "in")

if (rejectsUndeclared && !schema.additionalProperties)
schema.additionalProperties = false

return schema
Expand Down
110 changes: 110 additions & 0 deletions ark/type/__tests__/standardSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,116 @@ contextualize(() => {
})
})

it("includes defaults in input schema", () => {
const T = type({
name: 'string = "Anon"',
count: "number = 0",
tags: ["string[]", "=", () => ["a"]]
})
const standard: StandardJSONSchemaV1 = T
attest(
standard["~standard"].jsonSchema.input({ target: "draft-2020-12" })
).snap({
$schema: "https://json-schema.org/draft/2020-12/schema",
type: "object",
properties: {
count: { type: "number", default: 0 },
name: { type: "string", default: "Anon" },
tags: { type: "array", items: { type: "string" }, default: ["a"] }
}
})
attest(
standard["~standard"].jsonSchema.output({ target: "draft-2020-12" })
).snap({
$schema: "https://json-schema.org/draft/2020-12/schema",
type: "object",
properties: {
count: { type: "number" },
name: { type: "string" },
tags: { type: "array", items: { type: "string" } }
},
required: ["count", "name", "tags"]
})
})

it("includes defaults in input schema for tuples and morph inputs", () => {
const Tuple = type(["number", "string = 'x'"])
attest(
(Tuple as StandardJSONSchemaV1)["~standard"].jsonSchema.input({
target: "draft-2020-12"
})
).snap({
$schema: "https://json-schema.org/draft/2020-12/schema",
type: "array",
minItems: 1,
prefixItems: [{ type: "number" }, { type: "string", default: "x" }],
items: false
})

const Parsed = type({ n: "string.numeric.parse = '1'" })
attest(
(Parsed as StandardJSONSchemaV1)["~standard"].jsonSchema.input({
target: "draft-2020-12"
})
).snap({
$schema: "https://json-schema.org/draft/2020-12/schema",
type: "object",
properties: {
n: {
type: "string",
pattern:
"^(?:(?!^-0\\.?0*$)(?:-?(?:(?:0|[1-9]\\d*)(?:\\.\\d+)?)|\\.\\d+?))$",
default: "1"
}
}
})
})

it("allows undeclared keys that would be deleted in input schema", () => {
const T = type({ name: 'string = "Anon"' }).onUndeclaredKey("delete")
const standard: StandardJSONSchemaV1 = T
attest(
standard["~standard"].jsonSchema.input({ target: "draft-2020-12" })
).snap({
$schema: "https://json-schema.org/draft/2020-12/schema",
type: "object",
properties: { name: { type: "string", default: "Anon" } }
})
attest(
standard["~standard"].jsonSchema.output({ target: "draft-2020-12" })
).snap({
$schema: "https://json-schema.org/draft/2020-12/schema",
type: "object",
properties: { name: { type: "string" } },
required: ["name"],
additionalProperties: false
})
})

it("does not use a configured morph fallback for input schema", () => {
const T = type({ foo: "string.numeric.parse" })
const standard: StandardJSONSchemaV1 = T
attest(
standard["~standard"].jsonSchema.input({
target: "draft-2020-12",
libraryOptions: {
fallback: { morph: () => ({ type: "null" }) }
}
})
).snap({
$schema: "https://json-schema.org/draft/2020-12/schema",
type: "object",
properties: {
foo: {
type: "string",
pattern:
"^(?:(?!^-0\\.?0*$)(?:-?(?:(?:0|[1-9]\\d*)(?:\\.\\d+)?)|\\.\\d+?))$"
}
},
required: ["foo"]
})
})

it("generates different input/output schemas for morphs", () => {
const T = type({ foo: "string.numeric.parse" })
const standard: StandardJSONSchemaV1 = T
Expand Down
Loading