Skip to content

Latest commit

 

History

History
179 lines (163 loc) · 2.66 KB

README.md

File metadata and controls

179 lines (163 loc) · 2.66 KB

yaml-schema

YAML document schema validator and transformer

NPM Version NPM Downloads

Example

import { readFileSync } from "node:fs"
import { YAML } from "yaml-schema"

const configFile = readFileSync("config.yml", "utf-8")

const yaml = new YAML({
    type: "object",
    props: {
        id: {
            type: "string"
        },
        token: {
            type: "string"
        }
    }
})

const config = yaml.parse(configFile)

How To Use

import { YAML } from "yaml-schema"

const yaml = new YAML(schema)
yaml.parse(yamlString)

Schema

const yaml = new YAML({
    type: schemaType,
    default?: defaultValue
})

Example:

const yaml = new YAML({
    type: "object",
    props: {
        name: {
            type: "string"
        }
    }
})

Types:

  • any
  • boolean
  • number
  • string
  • choices
  • array
  • object

Schema Additional Properties

number schema:

const yaml = new YAML({
    type: "number",
    limit?: {
        min?: number,
        max?: number
    }
})

string schema:

const yaml = new YAML({
    type: "string",
    length?: {
        min?: number,
        max?: number
    }
})

choices schema: choices allow numbers and strings

const yaml = new YAML({
    type: "choices",
    choices: [
        "first choice",
        "second choice",
        "third choice"
    ]
})

array schema:

const yaml = new YAML({
    type: "array",
    element: anotherSchema,
    length?: {
        min?: number,
        max?: number
    }
})
const yaml = new YAML({
    type: "array",
    element: {
        type: "string"
    },
    length?: {
        min?: number,
        max?: number
    }
})

object schema:

const yaml = new YAML({
    type: "object",
    props: {
        name: schema
    }
})
const yaml = new YAML({
    type: "object",
    props: {
        name: {
            type: "string",
            length: {
                max: 100
            }
        }
    }
})

or

const yaml = new YAML({
    type: "object",
    any: {
        interface: schema,
        length?: {
            min?: number,
            max?: number
        }
    }
})
const yaml = new YAML({
    type: "object",
    any: {
        interface: {
            type: "number",
            limit: {
                min: 10,
                max: 100
            }
        },
        length?: {
            min?: number,
            max?: number
        }
    }
})