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

@ikaru5/heimdall-contract

v0.4.0

Published

Validation and Representer Objects in your Frontend

Downloads

204

Readme

Heimdall Contract

Inheritable and nestable data Objects with intuitive validation schema in your JS-Project. Target is to create Contracts like Rubys Trailblazer-Reform for your JS-Frontend like React, Vue or any other JS project (also backend).

Goals

  • RELIABLE: Heimdall Contracts got 100% test coverage.
  • REUSABLE: Contracts can be reused in other contracts. Thanks to validation context and inheritance you can reuse contracts with different validation rules.
  • EXTENSIBLE: You can extend the validation schema with your own validators.
  • INTUITIVE: The validation schema is intuitive and easy to understand.
  • INDEPENDENT: Heimdall Contracts are independent of any other library. It is not tied to any state management system or framework.
  • TESTABLE: Contracts are easy to test. Just take a look at the tests. ;)
  • DOCUMENTED: The documentation is written in a way that you can understand it without any prior knowledge.
  • FLEXIBLE: You can use Heimdall Contracts in any JS project. It is not tied to any framework or state management system.
  • LIGHTWEIGHT: Heimdall Contracts is lightweight. It has no dependencies and is only 3.1kb minified and gzipped.
  • TRANSLATABLE: You can use your own translation library or use the included i18next integration for error messages.

Example

First lets build a signup data contract!

class SignupContract extends ContractBase {

  defineSchema() {
    return (
      {
        ...super.defineSchema(), // optional: used for inheritance ;)
        ...{
          name: {dType: "String", presence: true},
          agb: {dType: "Boolean", default: false, only: true},
          email: {dType: "String", presence: true, isEmail: true},
          username: {dType: "String", presence: true, min: 8},
          password: {dType: "String", presence: true, min: 8},
          passwordRepeat: {
            dType: "String", presence: true,
            validate: (value, contract) => {
              return value === contract.password ? true : false // for custom error message return string instead of false. 
              // custom validation translation is not implemented yet, so you have to return the error message in the language you want to display it
            }
          },
          address: {
            street: {dType: "String", presence: true},
            streetNumber: {dType: "Number", presence: true},
            plz: {dType: "String", presence: true},
            city: {dType: "String", presence: true},
          }
        }
      }
    )
  }

}

Now you can simply instantiate your contract like this:

const signUpContract = new SignupContract()

Accessing and assigning single values is pretty straight forward:

signUpContract.name = "Kirill"
signUpContract.address.street = "Uhlandstr. 36"

console.log(
  signUpContract.name
)

console.log(
  signUpContract.address.street
)

If the data is coming as JSON or as an nested Object from your API or State Management System for example, you can assign it like that:

signUpContract.name = "Kirill"
signUpContract.address.street = "Uhlandstr. 36"

console.log(
  signUpContract.name
)

console.log(
  signUpContract.address.street
)

Inherit from your base class and define your contracts! Have Fun!

Documentation

Validation Context

It is possible to do validations only if a specific context is set. It is a quality o life feature and could also be implemented through validateIf.

class SubContextContract extends ContractBase {

  defineSchema() {
    return (
      {
        ...super.defineSchema(),
        ...{
          numberWithoutContext: {dType: "Number", min: 10},
          number: {dType: "Boolean", only: true, on: "contextA"}
        }
      }
    )
  }

}

class ContextContract extends ContractBase {
  defineSchema() {
    return (
      {
        ...super.defineSchema(),
        ...{
          numberWithoutContext: {dType: "Number", min: 10},
          number: {dType: "Number", min: 10, on: "contextA"},
          string: {dType: "String", match: /^[a-zA-Z0-9\s]*$/, on: ["contextA", "contextB"]},
          addressSimple: {
            plz: {presence: true, dType: "String", on: "contextA"}
          },
          sub: {dType: "Contract", contract: SubContextContract, allowBlank: false, on: "contextB"},
          addressesContracted: {dType: "Array", min: 2, arrayOf: SubContextContract, allowBlank: false, on: "contextB"}, // this context will skip only outer validations like "min: 2" in this example
          subsContractedWithInner: {dType: "Array", min: 3, arrayOf: SubContextContract, allowBlank: false, on: "contextB", innerValidate: {on: "contextB"}} // use innerValidate to skip validations of nested contract
        }
      }
    )
  }
}

As you can see, every attribute can have one or more contexts through on attribute. The context will also be passed to the nested contracts.

const contextContract = new ContextContract()
contextContract.assign(data)
contextContract.isValid("contextA") // context will be set to contextA
contextContract.isValid(["contextA", "contextB"]) // use multiple contexts
contextContract.isValid() // context = undefined
contextContract.isValid("matchAnyContext") // magic context to match all contexts

Development

  • [x] implement arrays of mixed types
  • [ ] add beautiful custom localization
  • [ ] rethink renderAs and parseAs (maybe implement some sort of data reforming schema)
  • [ ] switch from (value, isRequired, dType, depth, contract) to ({value, isRequired, dType, depth, contract}) in validations
  • [ ] add example for i18next
  • [ ] add JSDoc
  • [ ] add TS types
  • [ ] add reader and writer hooks
  • [ ] add validation modes: validate all (default), stop on first error, stop on first error per attribute

Contributing

  1. Fork it (https://github.com/your-github-user/schemas/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors and Contact

If you have ideas on how to develop heimdall or what features it is missing, I would love to hear about it!

  • @ikaru5 Kirill Kulikov - creator, maintainer

Copyright

Copyright (c) 2020 Kirill Kulikov [email protected]

heimdall-contract is released under the MIT License.