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

Function writeProps

ark/sets/toJsonSchema.ts:908–950  ·  view source on GitHub ↗
(
	node: Structure.Node,
	schema: JsonSchema.Object,
	ctx: JsonSchemaContext
)

Source from the content-addressed store, hash-verified

906 (!protoProps || protoProps.every(([, v]) => v.extends(value)))
907
908const writeProps = (
909 node: Structure.Node,
910 schema: JsonSchema.Object,
911 ctx: JsonSchemaContext
912): JsonSchema.Object => {
913 const path = ctx.path
914 schema.properties = {}
915 for (const prop of node.props) {
916 ctx.path = appendStringifiedKey(path, prop.key)
917 let valueSchema = toJsonSchemaRecurse(jsonValueOf(prop, ctx), ctx)
918
919 if (typeof prop.key === "symbol") {
920 schema = ctx.fallback.symbolKey({
921 code: "symbolKey",
922 base: schema,
923 key: prop.key,
924 value: valueSchema,
925 optional: prop.optional
926 })
927 continue
928 }
929
930 if (prop.hasDef())
931 valueSchema = writeDefault(valueSchema, prop.default, ctx)
932
933 // assigning "__proto__" would set the prototype
934 defineValue(schema.properties!, prop.key, valueSchema)
935 }
936 ctx.path = path
937 if (node.required?.length && schema.properties) {
938 const required = node.required.flatMap(prop =>
939 (
940 typeof prop.key !== "string" ||
941 !(prop.key in schema.properties!) ||
942 (ctx.io === "out" && prop.value.allowsUnused(undefined))
943 ) ?
944 []
945 : [prop.key]
946 )
947 if (required.length) schema.required = required
948 }
949 return schema
950}
951
952// a string key with a pattern is written as patternProperties, and one
953// without is collected for the fallbacks once the schema's own keys are known

Callers 1

reduceObjectJsonSchemaFunction · 0.85

Calls 4

defineValueFunction · 0.90
jsonValueOfFunction · 0.85
writeDefaultFunction · 0.85
toJsonSchemaRecurseFunction · 0.70

Tested by

no test coverage detected