npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

unimodel-schema-types

v2.0.1

Published

Use unimodel documents as common-schema types

Downloads

2

Readme

unimodel-schema-types

unimodel-schema-types is a collection of schema types for common-schema. They encapsulate model-related concepts found in unimodel-core.

Overview

When creating schemas for API call parameter validation, sometimes those API call parameters can be full model instances. For example, an API call to create a document will contain that document as a parameter, and that document itself would correspond to a schema. Another example would be an API call to update a document---it would take a valid Update as a parameter.

This project lets us validate and normalize such parameters. It contains common-schema SchemaType classes for Document, DocumentUpdate, DocumentQuery, and DocumentAggregate.

To use these schema types in projects, first register them with a schema factory.

const { defaultSchemaFactory } = require('common-schema');
const { registerTypes } = require('unimodel-schema-types');

registerTypes(defaultSchemaFactory);

After the schema types are registered, they may be used inside schemas. The easiest way to do this is to use the provided shorthands, explained below.

Here is a small example, where FooModel is a Unimodel.Model instance, using a useful SchemaTypeDocument shorthand:

let instance = new FooModel(createSchema({ foo: String }));
let schema = createSchema({ doc: instance });

In this example, a document instance is used as a shorthand inside a schema definition. When normalizing against schema, doc.foo normalizes to a String.

schema.normalize({ doc: { foo: 32 } });
// => { doc: { foo: '32' } }

SchemaTypes

SchemaTypeDocument

Schema type for Document instances.

let schema = createSchema({
	foo: {
		type: 'document',
		modelName: 'Foo',
		modelType: 'FooModel',
		documentSchema: createSchema({ foo: String }),
		options: {}
	}
});

SchemaTypeDocumentUpdate

Schema type for DocumentUpdate instances.

let schema = createSchema({
	foo: {
		type: 'documentUpdate',
		modelName: 'Foo',
		modelType: 'FooModel',
		documentSchema: createSchema({ foo: String }),
		options: {}
	}
});

SchemaTypeDocumentQuery

Schema type for DocumentQuery instances.

let schema = createSchema({
	foo: {
		type: 'documentQuery',
		modelName: 'Foo',
		modelType: 'FooModel',
		documentSchema: createSchema({ foo: String }),
		options: {}
	}
});

SchemaTypeDocumentAggregate.

Schema type for DocumentAggregate instances.

let schema = createSchema({
	foo: {
		type: 'documentAggregate',
		modelName: 'Foo',
		modelType: 'FooModel',
		documentSchema: createSchema({ foo: String }),
		options: {}
	}
});

Shorthand

Convenient shorthand methods are included as an alternative to the verbose syntax above.

const {
	documentType,
	documentUpdateType,
	documentQueryType,
	documentAggregateType
} = require('unimodel-schema-types');

let instance = new FooModel(createSchema({ foo: String }));

let schema = createSchema({
	doc: documentType(instance),
	docUpdate: documentUpdateType(instance),
	docQuery: documentQueryType(instance),
	docAggregate: documentAggregateType(instance)
});

As mentioned in the overview, SchemaTypeDocument has an additional shorthand, where an instance may be used directly.

let instance = new FooModel(createSchema({ foo: String }));
let schema = createSchema({
	doc: instance
});