Skip to content

Instantly share code, notes, and snippets.

@dragonman225
Last active December 30, 2025 14:47
Show Gist options
  • Select an option

  • Save dragonman225/60dc23c7ae02ecdaf9f8764b0712af16 to your computer and use it in GitHub Desktop.

Select an option

Save dragonman225/60dc23c7ae02ecdaf9f8764b0712af16 to your computer and use it in GitHub Desktop.
Valibot schema for OCIF v0.6
import * as v from 'valibot'
/* ---------------------------------------------------------------- */
/* Canvas Extensions */
/* ---------------------------------------------------------------- */
export const CanvasViewportExtensionSchema = v.object({
type: v.literal('@ocif/canvas/viewport'),
position: v.array(v.number()),
size: v.array(v.number()),
})
export function isCanvasViewportExtension(input: unknown) {
return v.is(CanvasViewportExtensionSchema, input)
}
export type CanvasViewportExtension = v.InferInput<
typeof CanvasViewportExtensionSchema
>
/* ---------------------------------------------------------------- */
/* Node Extensions */
/* ---------------------------------------------------------------- */
export const ArrowExtensionSchema = v.object({
type: v.literal('@ocif/node/arrow'),
strokeWidth: v.optional(v.number()),
strokeColor: v.optional(v.string()),
start: v.array(v.number()),
end: v.array(v.number()),
startMarker: v.optional(v.string()),
endMarker: v.optional(v.string()),
/** @deprecated Lifted to `Node.relation` since v0.5. */
relation: v.optional(v.string()),
})
export function isArrowExtension(input: unknown) {
return v.is(ArrowExtensionSchema, input)
}
export type ArrowExtension = v.InferInput<typeof ArrowExtensionSchema>
/* ---------------------------------------------------------------- */
/* Relation Extensions */
/* ---------------------------------------------------------------- */
export const EdgeRelationExtensionSchema = v.object({
type: v.literal('@ocif/rel/edge'),
start: v.string(),
end: v.string(),
directed: v.optional(v.boolean()),
rel: v.optional(v.string()),
node: v.optional(v.string()),
})
export function isEdgeRelationExtension(input: unknown) {
return v.is(EdgeRelationExtensionSchema, input)
}
export type EdgeRelationExtension = v.InferInput<
typeof EdgeRelationExtensionSchema
>
import * as v from 'valibot'
/* ---------------------------------------------------------------- */
/* Extension */
/* ---------------------------------------------------------------- */
/**
* `type` should be Schema Name or URI
*
* @see https://github.com/ocwg/ocif-spec/blob/main/spec/v0.6/spec.md#extension-mechanism
*/
export const OcifExtensionSchema = v.looseObject({
type: v.string(),
})
export function isOcifExtension(input: unknown) {
return v.is(OcifExtensionSchema, input)
}
export type OCIFExtension = v.InferInput<typeof OcifExtensionSchema>
/* ---------------------------------------------------------------- */
/* Node */
/* ---------------------------------------------------------------- */
export enum ResourceFit {
None = 'none',
ContainX = 'containX',
ContainY = 'containY',
Contain = 'contain',
Cover = 'cover',
Fill = 'fill',
Tile = 'tile',
}
export const OcifNodeSchema = v.object({
id: v.string(),
position: v.optional(v.array(v.number())),
size: v.optional(v.array(v.number())),
resource: v.optional(v.string()),
resourceFit: v.optional(v.enum(ResourceFit)),
data: v.optional(v.array(OcifExtensionSchema)),
rotation: v.optional(v.number()),
/** @since v0.5 */
relation: v.optional(v.string()),
})
export function isOcifNode(input: unknown) {
return v.is(OcifNodeSchema, input)
}
export type OCIFNode = v.InferInput<typeof OcifNodeSchema>
/* ---------------------------------------------------------------- */
/* Relation */
/* ---------------------------------------------------------------- */
export const OcifRelationSchema = v.object({
id: v.string(),
data: v.optional(v.array(OcifExtensionSchema)),
node: v.optional(v.string()),
})
export function isOcifRelation(input: unknown) {
return v.is(OcifRelationSchema, input)
}
export type OCIFRelation = v.InferInput<typeof OcifRelationSchema>
/* ---------------------------------------------------------------- */
/* Representation */
/* ---------------------------------------------------------------- */
/**
* Either content or location MUST be present. If content is used, location
* must be left out and vice versa.
*
* @see https://github.com/ocwg/ocif-spec/blob/main/spec/v0.6/spec.md#representation
*/
export const OcifRepresentationSchema = v.object({
location: v.optional(v.string()),
mimeType: v.optional(v.string()),
/** @deprecated Renamed to `mimeType` since v0.5. */
'mime-type': v.optional(v.string()),
content: v.optional(v.string()),
})
export function isOcifRepresentation(input: unknown) {
return v.is(OcifRepresentationSchema, input)
}
export type OCIFRepresentation = v.InferInput<typeof OcifRepresentationSchema>
/* ---------------------------------------------------------------- */
/* Resource */
/* ---------------------------------------------------------------- */
export const OcifResourceSchema = v.object({
id: v.string(),
representations: v.array(OcifRepresentationSchema),
})
export function isOcifResource(input: unknown) {
return v.is(OcifResourceSchema, input)
}
export type OCIFResource = v.InferInput<typeof OcifResourceSchema>
/* ---------------------------------------------------------------- */
/* SchemaEntry */
/* ---------------------------------------------------------------- */
export const OcifSchemaEntrySchema = v.object({
uri: v.string(),
/**
* JSON schema inline as a JSON object
* @see
* https://github.com/ocwg/ocif-spec/blob/main/spec/v0.6/spec.md#schemas
*/
schema: v.optional(v.looseObject({})),
location: v.optional(v.string()),
name: v.optional(v.string()),
})
export function isOcifSchemaEntry(input: unknown) {
return v.is(OcifSchemaEntrySchema, input)
}
export type OCIFSchemaEntry = v.InferInput<typeof OcifSchemaEntrySchema>
/* ---------------------------------------------------------------- */
/* Document */
/* ---------------------------------------------------------------- */
export const OcifDocumentSchema = v.object({
ocif: v.string(),
rootNode: v.optional(v.string()),
data: v.optional(v.array(OcifExtensionSchema)),
nodes: v.optional(v.array(OcifNodeSchema)),
relations: v.optional(v.array(OcifRelationSchema)),
resources: v.optional(v.array(OcifResourceSchema)),
schemas: v.optional(v.array(OcifSchemaEntrySchema)),
})
export function isOcifDocument(input: unknown) {
return v.is(OcifDocumentSchema, input)
}
export type OCIFDocument = v.InferInput<typeof OcifDocumentSchema>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment