A
arktype•2mo ago
Eric

Can I make `string.yaml` like `string.json`?

I haven't been able to figure out a way of extending it with my own yaml https://arktype.io/docs/scopes#submodules 🤔
ArkType
ArkType Docs
TypeScript's 1:1 validator, optimized from editor to runtime
3 Replies
ssalbdivad
ssalbdivad•2mo ago
Extending a built-in submodule like this is a little trickier than creating a new top-level keyword or submodule that doesn't overlap an existing one. Are you wanting string.yaml to appear in the global scope or just in a specific scope you create?
Eric
EricOP•2mo ago
I’ll take either. I have a custom $ scope that works great. I tried making a string sub module on that that attempted to …type.string.internal or something (I’m mobile, sorry for no code blocks!), but that broke all existing string.* types. I went with yaml.string.parse and yaml.object.stringify, but it only loses the discoverability and similarity with string.json and object.json, for example.
ssalbdivad
ssalbdivad•2mo ago
Does this work for you?
import { scope, type } from "arktype"

// add actual yaml validation logic here
const yaml = type.string.narrow(s => s === "yaml")

const $ = scope({
string: type.module({
...type.keywords.string,
yaml
})
})

const t = $.type({
yaml: "string.yaml"
})
import { scope, type } from "arktype"

// add actual yaml validation logic here
const yaml = type.string.narrow(s => s === "yaml")

const $ = scope({
string: type.module({
...type.keywords.string,
yaml
})
})

const t = $.type({
yaml: "string.yaml"
})

Did you find this page helpful?