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

@mojule/entity-schema

v0.8.0

Published

Subset of JSON Schema for defining entities

Downloads

130

Readme

entity-schema

A subset of JSON Schema for defining entities and some tools for working with them

Intent is to reduce duplication of effort by allowing you to define your application entities once using JSON schema, and then from that automatically generate express API routes, typescript interfaces, mongoose schema, HTML forms for editing and creating entities etc.

Needs better docs, renaming, refactoring etc. - was extracted from an existing project

We extend JSON schema but not in any way that affects validation, just extra metadata etc, and as little as possible (eg if it can be done with plain JSON Schema do it that way instead)

Currently designed to be used with json-schema-to-typescript and mongoose, but at some point the project should be broken up into various component parts and abstractions added so you can use with any db, with connect or any other web server instead of express, etc.

Schema Types

WS Schema

src/predicates/ws-schema.ts

All schema and subschema should have at least a title and type

{
  "title": "Foo",
  "type": "string"
}

$ref Schema

src/predicates/ref-schema.ts

These are the only exception to the title and type rule, but the schema they point to must be RootSchema, which have both of those fields

Subschema

src/predicates/subschema.ts

One of either WS Schema or $ref Schema

RootSchema

Represents a schema used in the app. It exists as a file so has a URI id property. A RootSchema id and title should be unique within the application.

{
  "id": "http://example.com/schema/foo",
  "title": "Foo",
  "type": "string"
}

ObjectSchema

src/predicates/object-schema.ts

Represents a RootSchema of type object

An ObjectSchema should always be a RootSchema, eg exist as it's own file, have a URI ID etc.

It should always be type: 'object'

It should not have Entity Schema as subschema - use a Reference Schema instead

Additional properties should be false

{
  "id": "http://example.com/schema/foo",
  "title": "Foo",
  "type": "object",
  "properties": {
    "name": {
      "$ref": "http://example.com/schema/name"
    },
    "bar": {
      "$ref": "http://example.com/schema/bar-reference"
    }
  },
  "required": [ "name", "bar" ],
  "additionalProperties": false
}

Entity Schema

src/predicates/entity-schema.ts

Represents a main business logic entity that will be persisted to db, partake in the REST API etc

An Entity Schema should always be an Object Schema

Additionally it should have its format keyword set to entity-schema

It should not have other Entity Schema as subschema - use a Reference Schema instead

Additional properties should be false

An entity should have a name:string property

{
  "id": "http://example.com/schema/foo",
  "title": "Foo",
  "type": "object",
  "format": "entity-schema",
  "properties": {
    "name": {
      "$ref": "http://example.com/schema/name"
    },
    "bar": {
      "$ref": "http://example.com/schema/bar-reference"
    }
  },
  "required": [ "name", "bar" ],
  "additionalProperties": false
}

Entity Reference Schema

src/predicates/entity-reference-schema.ts

A schema that points to an Entity Schema

It must have entityId and entityType properties

entityId should be a Mongo ID string entityType should be a Const Property Schema - the enum and default should match the title field of the linked entity

{
  "title": "Foo Reference",
  "type": "object",
  "properties": {
    "entityId": {
      "title": "Foo",
      "type": "string",
      "pattern": "^[0-9a-f]{24}$",
      "message": "Foo must be a 24 character hex string. (0-9, a-f)"
    },
    "entityType": {
      "title": "Entity Type",
      "type": "string",
      "enum": [ "Foo" ],
      "readOnly": true,
      "default": "Foo"
    }
  },
  "required": [ "entityId", "entityType" ],
  "additionalProperties": false
}

Child Entity Schema

src/predicates/child-entity-schema.ts

An Entity Schema which has a link to a parent Entity Schema

It should have a _esParentKey which is the name of the property that points to the parent entity

The property it points to should be an Entity Reference Schema

{
  "id": "http://example.com/schema/foo",
  "title": "Foo",
  "type": "object",
  "format": "entity-schema",
  "_esParentKey": "bar",
  "properties": {
    "name": {
      "$ref": "http://example.com/schema/name"
    },
    "bar": {
      "$ref": "http://example.com/schema/bar-reference"
    }
  },
  "required": [ "name", "bar" ],
  "additionalProperties": false
}

Unique Property Schema

src/predicates/unique-property-schema.ts

Used for Entity Schema properties to denote that the value of this property should be unique

If the Entity Schema is a Child Entity Schema, it must be unique amongst the children of the parent Entity

If the Entity Schema is not a Child Entity Schema, it must be unique amonst all of the Entities of this type

{
  "title": "Name",
  "type": "string",
  "_esUnique": true
}

Const Property Schema

src/predicates/const-property-schema.ts

Often used as a discriminator - a property which must match a certain string

The single enum array item and the default should match

readOnly should be true

The pattern is:

{
  "title": "Kind",
  "type": "string",
  "enum": [ "Foo" ],
  "readOnly": true,
  "default": "Foo"
}

Enum Schema

src/predicates/enum-schema.ts

A schema representing a string enum

It should be of type string, with an enum keyword listing possible values

It should have a _enumTitles keyword that lists human readable titles for the enums - so these two arrays should have the same length

{
  "title": "My Enum",
  "type": "string",
  "enum": [
    "foo", "bar", "baz"
  ],
  "_enumTitles": [
    "Foo", "Bar", "Baz"
  ]
}

OneOf Schema

src/predicates/oneof-schema.ts

A schema that can be one of several possible values

There should be a discriminator of some kind to tell just from models which type is used

If oneOf is a list of Entity Reference Schema then the discriminator will be entityType - otherwise use kind on the subschemas to differentiate

{
  "title": "Payload",
  "type": "object",
  "oneOf": [
    {
      "$ref": "http://example.com/schema/document-payload-generic"
    },
    {
      "$ref": "http://example.com/schema/document-payload-unity"
    }
  ]
}