MCPcopy Create free account
hub / github.com/arktypeio/arktype / writeDefault

Function writeDefault

ark/sets/toJsonSchema.ts:1179–1199  ·  view source on GitHub ↗
(
	schema: JsonSchema,
	defaultInput: unknown,
	ctx: JsonSchemaContext
)

Source from the content-addressed store, hash-verified

1177}
1178
1179const writeDefault = (
1180 schema: JsonSchema,
1181 defaultInput: unknown,
1182 ctx: JsonSchemaContext
1183): JsonSchema => {
1184 const value =
1185 typeof defaultInput === "function" ? defaultInput() : defaultInput
1186 const jsonDefault =
1187 isJsonData(value) ? value : (
1188 ctx.fallback.defaultValue({ code: "defaultValue", value: value as never })
1189 )
1190 if (jsonDefault === undefined) return schema
1191 // OpenAPI 3.0 Reference Objects forbid sibling keywords and draft-07 ignores
1192 // them, so a $ref is wrapped to hold its default. Whether a fallback's
1193 // result is one is only known as it's called, so a writer reads it, which a
1194 // build records and its replay runs again
1195 if (ctx.target === "draft-07" || ctx.target === "openapi-3.0")
1196 return ctx.writers.default(schema, jsonDefault)
1197 schema.default = jsonDefault
1198 return schema
1199}
1200
1201const reduceSequenceJsonSchema = (
1202 node: Sequence.Node,

Callers 3

writePropsFunction · 0.85
reduceSequenceJsonSchemaFunction · 0.85
jsSequenceDefaultsMethod · 0.85

Calls 2

isJsonDataFunction · 0.85
defaultMethod · 0.65

Tested by

no test coverage detected