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

loopback-jsonschema-registry

v1.1.2

Published

JSON Schema based validation for LoopBack through a custom model registry

Downloads

3

Readme

LoopBack JSON Schema Registry

Example Usage

server.js:

import loopback from 'loopback'
import Registry from 'loopback-jsonschema-registry'
import path from 'path'

// Configure the app to not load the built-in models, so that the registry
// can be overridden first, allowing to change the way model.json configs are
// parsed.
const app = loopback({
  localRegistry: true,
  loadBuiltinModels: false
})


// Override app.registry with a JSON Schema Registry
app.registry = new Registry({
  // Load and register all validators (keywords or formats) from ./validators
  loadValidators: path.resolve(__dirname, 'validators'),

  // Disable and emulate default LoopBack validations through JSON schema:
  emulateValidations: true,

  // Define further processing / translation of model JSON properties:
  configureProperty(schema) {
    if (schema.computed) {
      // Don't include computed properties in LB schema.
      return false
    }
    // App-wide rules:
    const {type} = schema
    if (type === 'integer') {
	  // JSON 'integer' is translated to LB 'number'
      schema.type = 'number'
    } else if (type === 'number' && !schema.postgresql) {
	  // JSON 'number' is used for double precision fields by adding postgresql
	  // specific information
      schema.postgresql = {
        dataType: 'double precision'
      }
    }
  }
})

validators/range.js:

export default {
  keyword: 'range',
  type: ['number', 'integer'],
  metaSchema: {
    type: 'array',
    items: [
      { type: 'number' },
      { type: 'number' }
    ],
    additionalItems: false
  },
  macro(config) {
    return {
      minimum: config[0],
      maximum: config[1]
    }
  }
}

models/test.json:

{
  "name": "Test",
  "base": "PersistedModel",
  "properties": {
    "factor": {
      "type": "integer",
      "range": [0, 50],
      "required": true
    }
  }
}

DEBUG=loopback:contrib:jsonschema-registry yarn start

loopback:contrib:jsonschema-registry LoopBack Schema for Model `Test`:

{
  "name": "Test",
  "base": "PersistedModel",
  "properties": {
    "factor": {
      "type": "number",
      "required": true
    }
  }
}

loopback:contrib:jsonschema-registry JSON Schema for Model `Test`:

{
  "id": "Test",
  "$schema": "http://json-schema.org/draft-06/schema#",
  "$async": true,
  "type": "object",
  "properties": {
    "factor": {
      "type": "integer",
      "range": [
        0,
        50
      ],
      "format": "required"
    },
    "id": {
      "type": [
        "number",
        "null"
      ]
    }
  },
  "required": [
    "factor"
  ],
  "additionalProperties": false
}