# simple-ts-schema

> Simple schema for objects that validate type and value of an object

Latest version **1.0.0** (published 2016-07-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install simple-ts-schema
pnpm add simple-ts-schema
yarn add simple-ts-schema
bun add simple-ts-schema
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2016-07-29 |
| First published | 2016-07-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | MedSolve |
| Maintainers | medsolve |
| Keywords | NodeJS, typescript, Schema, Validation |

## Links

- npm: https://www.npmjs.com/package/simple-ts-schema
- Repository: https://github.com/MedSolve/di-type
- Homepage: https://github.com/MedSolve/di-type#readme
- Issues: https://github.com/MedSolve/di-type/issues
- npm.io page: https://npm.io/package/simple-ts-schema

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2016-07-29

## README

# Introduction: simple-ts-schema
With this code it its possible to create simple schemas of objects that can validate both the type and value of the content. This is designed to be used as an decorator. This means _experimentalDecorators: true_ should be set in your projects _tsconfig.json_. The decorator is exposed as _SchemaComponent_ while models included are exposed as _SchemaModels_.

# Usage
To use this in your project and save it in the package.json file do:
`npm install simple-ts-schema --save`

Please be aware that we use [semantic versioning](http://semver.org). This means that you should be able to safely subscribe to updates on this module for versions 1.x.x or 2.x.x etc. Major versions for example from 1.x.x to 2.x.x is not safe as the module API might change.

# How to
The best way to show you how this works is by example. Please follow one below.

```typescript
import {SchemaComponent, SchemaModels}            from    'simple-ts-schema';

/**
* Create a datatype boolean
*/
class MyBoolean implements SchemaModels.Element {
    public _value: any;
    constructor(data: any) {

        // convert to correct form
        if (data === 'false') {
            data = false;
        } else if (data === 'true') {
            data = true;
        }

        if (data !== false && data !== true) {
            throw new Error('not a boolean');
        }
        this._value = data;
    }
}

/**
* Create a datatype string
*/
class MyString implements SchemaModels.Element {
    public _value: any;
    constructor(data: any) {

        this._value = data;
    }
}

/**
* Create values our goat can have
*/
let: goatNames: SchemaModels.Values = {};
goatNames['hans'] = true;
goatNames['Grethe'] = false;    // The actual true/false dont matter is not checked

/**
* Create a schema
*/
@SchemaComponent
export class Goat {
    public name: SchemaModels.PropertyDefinition = {
        max: 2,     // can have up to two names
        min: 1,     // Needs at least one name
        types: [MyString]   // array of types supported by this property
        values: goatNames   // only allow hans or Grethe
    };
    public cool: SchemaModels.PropertyDefinition = {
        max: 1, 
        min: 0,
        types: [MyBoolean]
    };
    constructor(data: any, enforce: SchemaModels.Enforce) {

        // do nothing
    }
}

// This is okay
new Goat({cool: true, name: ['hans']}) ;
// This is not missing name
new Goat({cool: true}); 
// this is not name is not array
new Goat({cool: false, name: 'Grethe'});
// this is not cool is not boolean
new Goat({cool: 'false', name: ['Grethe']});

```


# License
The MIT License (MIT)

---
_Source: https://npm.io/package/simple-ts-schema · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
