All Projects β†’ spotify β†’ ts-interface-loader

spotify / ts-interface-loader

Licence: Apache-2.0 License
Webpack support for validating TypeScript definitions at runtime.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to ts-interface-loader

angular-translate-loader
"angular-translate" loader for webpack
Stars: ✭ 15 (-21.05%)
Mutual labels:  webpack-loader
graphql-raw-loader
πŸ– With Webpack, loads GraphQL files as raw strings and handle it's import directive & comment statement.
Stars: ✭ 19 (+0%)
Mutual labels:  webpack-loader
mjml-loader
MJML loader for webpack
Stars: ✭ 27 (+42.11%)
Mutual labels:  webpack-loader
flow-webpack-plugin
A webpack plugin allowing to call Flow type checker.
Stars: ✭ 36 (+89.47%)
Mutual labels:  typechecking
webpack-modernizr-loader
Get your modernizr build bundled with webpack, use modernizr with webpack easily
Stars: ✭ 35 (+84.21%)
Mutual labels:  webpack-loader
nunjucks-loader
Webpack loader for Nunjucks templates
Stars: ✭ 20 (+5.26%)
Mutual labels:  webpack-loader
react-spa-template
This is a sample template for single page applications built using React + Router to work with webpack dev server
Stars: ✭ 19 (+0%)
Mutual labels:  webpack-loader
slim-lang-loader
Webpack loader: slim => html => javascript
Stars: ✭ 20 (+5.26%)
Mutual labels:  webpack-loader
preprocessor-loader
Bring the awesome "Conditional Compilation" to the Webpack, and more.
Stars: ✭ 32 (+68.42%)
Mutual labels:  webpack-loader
vue-template-compiler-loader
Webpack loader to pre-compile Vue 2.0 templates
Stars: ✭ 26 (+36.84%)
Mutual labels:  webpack-loader
strongtyping
Decorator which checks whether the function is called with the correct type of parameters.
Stars: ✭ 85 (+347.37%)
Mutual labels:  typechecking
color-loader
🎨 A webpack loader that extracts the color palette of an image
Stars: ✭ 14 (-26.32%)
Mutual labels:  webpack-loader
React-bookstore
Bookstore using google-book Apis made with reactjsπŸ”₯πŸš€
Stars: ✭ 14 (-26.32%)
Mutual labels:  webpack-loader
svelte-loader-hot
Webpack loader for svelte components with HMR support
Stars: ✭ 22 (+15.79%)
Mutual labels:  webpack-loader
standard-loader
webpack loader for linting your code with https://github.com/feross/standard
Stars: ✭ 66 (+247.37%)
Mutual labels:  webpack-loader
virtual-dependency-loader
webpack loader that takes a single file, and declare pieces of that file as "dependencies" as if it existed.
Stars: ✭ 33 (+73.68%)
Mutual labels:  webpack-loader
graphql-loader
πŸ’ A webpack loader for .graphql documents
Stars: ✭ 60 (+215.79%)
Mutual labels:  webpack-loader
exif-loader
Extract EXIF- & IPTC-data from your JPGs during build-time.
Stars: ✭ 14 (-26.32%)
Mutual labels:  webpack-loader
yaml-frontmatter-loader
[DEPRECATED] Yaml frontmatter loader
Stars: ✭ 12 (-36.84%)
Mutual labels:  webpack-loader
python-autoclass
A python 3 library providing functions and decorators to automatically generate class code, such as constructor body or properties getters/setters along with optional support of validation contracts on the generated setters. Its objective is to reduce the amount of copy/paste code in your classes - and hence to help reducing human mistakes :).
Stars: ✭ 30 (+57.89%)
Mutual labels:  typechecking

ts-interface-loader

Build Status codecov License Contributor Covenant

Validate TypeScript definitions at runtime with webpack and Node 8+. Built and used at Spotify.

Common use cases:

  • Provide validation for JSON files (i.e. system config)
  • Testing JSON output using your existing TypeScript definitions.

Intro

By design, TypeScript only provides typechecking at compile time. However it is possible to do so at runtime through ts-interface-builder generating a JSON manifest of your definitions and ts-interface-checker for validating those types with plain JSON / JavaScript objects.

In our case, we wanted to build the manifest automatically through Webpack. Welcome ts-interface-loader!

Code Example

How does it look like in code terms?

// First, we'll want to make some TypeScript definitions.
// In this case, we have a types.ts with a IUser type interface.
//
export interface IUser {
  first_name: string
  last_name: string
  office_location: 'stockholm' | 'gothenburg' | 'london' | 'new york' | 'boston'
}

// Secondly, we'll write our app.ts (which will run with Webpack)
//
import {createCheckers, ITypeSuite} from 'ts-interface-checker'
import typesTI from 'ts-interface-loader!./types'
// import {IUser} from './types'

const validPayload = {first_name: 'Daniel', last_name: 'Ek'}
const invalidPayload = {first_name: 'Daniel', last_name: 123}

try {
  createCheckers(typesTI).IUser.check(invalidPayload)
} catch (err) {
  console.log(err.message) // => "value.last_name is not a string"
}

// Or, if we want to validate in "strict" mode
// It's the same as above, except column presence is required.
//
const validStrictPayload = {first_name: 'Daniel', last_name: 'Ek', office_location: 'stockholm'}
const invalidStrictPayload = {first_name: 'Daniel', last_name: 'Ek'}

try {
  createCheckers(typesTI).IUser.strictCheck(invalidStrictPayload)
} catch (err) {
  console.log(err.message) // => "value.office_location is missing"
}

For a more in-depth sample, take a look at our sample project.

Getting started

Install

$ npm install --dev ts-interface-loader
$ yarn add --dev ts-interface-loader

Import ts-interface-loader in your TypeScript code (.ts or .tsx)

// ES6 Modules
import typesTI from 'ts-interface-loader!./types'

// ES5 (CommonJS)
const typesTI = require('ts-interface-loader!./types')

Note: Using tsc or tslint? You'll need to add @ts-ignore and tslint:disable-line:no-implicit-dependencies for now, as there is no support (natively or through plugins) for supporting importing webpack loaders inline.

// @ts-ignore
import typesTI from 'ts-interface-loader!./types' // tslint:disable-line

Development

Clone the repository

$ git clone https://github.com/spotify/ts-interface-loader.git
$ cd ts-interface-loader/
$ yarn install

Run

# Get started
$ yarn build
$ cd example/
$ yarn watch

# Run some commands!
$ cat src/__fixtures__/ok-all-matching-types.json | yarn run --silent cli | jq '.manifestJson'
$ cat src/__fixtures__/error-no-tvshows-key.json | yarn run --silent cli | jq '.manifestValidator'
$ cat src/__fixtures__/error-no-tvshows-key.json | yarn run --silent cli | jq '.manifestStrictValidator'

# Take a look at other fixtures!
$ cd src/__fixtures__

Test

Run the following command

$ yarn test

Changelog

v1.0.2

Fixed security bugs with:

  • mixin-deep by bumping from 1.3.1 to 1.3.2
  • set-value by bumping from 2.0.0 to 2.0.1
  • serialize-javascript by bumping from 1.4.0 to 2.1.1

v1.0.1

Fixed security bug with handlebars by bumping from 4.1.2 to 4.5.3

v1.0.0

Initial release

Contributing

We strictly adhere to the Contributor Covenant in this repository, and wish to foster an open source culture that's welcoming and diverse.

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

  1. Fork it!
  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. Submit a pull request :)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].