Skip to content

metarhia/metaschema

Repository files navigation

Metaschema

Metadata schema and interface (contract) definition language

ci snyk npm version npm downloads/month npm downloads license

Installation

$ npm i metaschema

Examples

Basic Schema Usage

const { Schema } = require('metaschema');

const schema = Schema.from({
  name: {
    first: 'string',
    last: 'string',
    third: '?string',
  },
  age: 'number',
  levelOne: {
    levelTwo: {
      levelThree: { type: 'enum', enum: [1, 2, 3] },
    },
  },
  collection: { array: { array: 'number' } },
});

const data = {
  name: {
    first: 'a',
    last: 'b',
  },
  age: 5,
  levelOne: { levelTwo: { levelThree: 1 } },
  collection: [
    [1, 2, 3],
    [3, 5, 6],
  ],
};

console.log(schema.check(data));
// Output: ValidationResult { errors: [], valid: true }

Schema Constructor

const { Schema } = require('metaschema');

const schema = new Schema('User', {
  name: 'string',
  email: 'string',
  age: 'number',
  active: 'boolean',
});

console.log(schema.name); // 'User'
console.log(schema.kind); // 'struct'
console.log(schema.scope); // 'local'

Model Usage

const { Model } = require('metaschema');

const types = {
  string: { metadata: { pg: 'varchar' } },
  number: { metadata: { pg: 'integer' } },
  boolean: { metadata: { pg: 'boolean' } },
};

const entities = new Map();
entities.set('User', {
  Entity: { scope: 'application', store: 'persistent' },
  name: 'string',
  email: { type: 'string', unique: true },
  age: 'number',
});

const model = new Model(types, entities);
console.log(model.dts); // Generated TypeScript definitions

Loader Functions

const { createSchema, loadSchema, loadModel } = require('metaschema');

// Create schema from string
const schema1 = createSchema('User', "({ name: 'string', age: 'number' })");

// Load schema from file
const schema2 = await loadSchema('./schemas/user.js');

// Load entire model from directory
const model = await loadModel('./schemas');

Schema Kinds and Metadata

const { Schema, KIND, SCOPE, STORE } = require('metaschema');

console.log(KIND); // ['struct', 'scalar', 'form', 'projection', ...]
console.log(SCOPE); // ['application', 'global', 'local']
console.log(STORE); // ['persistent', 'memory']

const entitySchema = new Schema('Company', {
  Entity: { scope: 'application', store: 'persistent' },
  name: 'string',
  address: 'string',
});

License & Contributors

Copyright (c) 2017-2025 Metarhia contributors. Metaschema is MIT licensed.
Metaschema is a part of Metarhia technology stack.