# @simpli/meta-schema

> A data-structure library to describe more information about your model in a separated file

Latest version **1.0.2** (published 2020-02-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install @simpli/meta-schema
pnpm add @simpli/meta-schema
yarn add @simpli/meta-schema
bun add @simpli/meta-schema
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2020-02-14 |
| First published | 2020-01-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=6.0.0 |
| Dependencies | 0 |
| Unpacked size | 39.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | gil |
| Maintainers | ftgibran, melanke |

## Links

- npm: https://www.npmjs.com/package/@simpli/meta-schema
- npm.io page: https://npm.io/package/@simpli/meta-schema

## Recent versions

- 1.0.2 (latest) — 2020-02-14
- 1.0.1 — 2020-02-14
- 1.0.0 — 2020-01-22

## README

# Meta-Schema

A data-structure library to describe more information about your model in a separated file

## Install
```
npm i @simpli/meta-schema
```

## About
Meta-Schema helps you to organize meta information about the Model fields, can be used to declare how the field will be rendered.

## Usage

### Import
```typescript
import { Schema, FieldSet, FieldComponent } from '@simpli/meta-schema'
```

### Create your schema
```typescript
class MyModel {
  title: string | null = null
  description: string | null = null
}

class MySchema extends Schema {
  // create a function to translate your field names, 
  // my translation only put it on uppercase
  translateFrom = (fieldName: string) => fieldName.toUpperCase()

  // declare the fieldSet
  readonly fieldSet: FieldSet<MyModel> = {
    // our first field is 'title', is a FieldComponent
    title: (schema): FieldComponent => ({
      // on "is" you may put the component class to render the fieldset
      is: MyComponent,
      // on "bind" you can put any prop
      bind: {
        label: this.translateFrom(schema.fieldName),
        required: true,
        validation: 'required',
      },
      // you can declare a `name` and use `on` for listeners
    }),
    // our second field is 'description', is a FieldData because it only answer a primitive value
    description: schema => schema.model.description,
  }
}
```
### Schema basic methods
```typescript
const subject = new MySchema()

subject.allFields // returns ['title', 'description']
subject.allHeaders // returns ['TITLE', 'DESCRIPTION']
subject.header // returns {title: 'TITLE', description: 'DESCRIPTION'}

const model = new MyModel()
model.title = 'my title'
model.description = 'my description'

const schemaBuilder = subject.build(model, 'title')
schemaBuilder.schema // returns subject
schemaBuilder.model // returns model
schemaBuilder.fieldName // returns 'title'

const m1 = new MyModel()
m1.title = 'my title'
m1.description = 'my description'
const m2 = new MyModel()
m2.title = 'other title'
m2.description = 'other description'
const m3 = new MyModel()
m3.title = 'with null description'
m3.description = null

subject.fieldDataList([m1, m2, m3], false) // false to use the fieldname as key
/* returns 
[
  {"description": "my description"},
  {"description": "other description"},
  {"description": null}
]
*/

subject.fieldDataList([m1, m2, m3], true) // true to use the translation as key
/* returns 
[
  {"DESCRIPTION": "my description"},
  {"DESCRIPTION": "other description"},
  {"DESCRIPTION": null}
]
*/

subject.fieldComponent(model, false)
/* returns
{
  "title": {
    is: MyComponent,
    bind: {
      label: 'TITLE',
      required: true,
      validation: 'required',
    },
  }
}
*/

subject.build(model, 'title').getData() 
// returns null because it is not a FieldData

subject.build(model, 'description').getData() // returns 'my description'

subject.build(model, 'title').getComponent()
/* returns
{
  is: MyComponent,
  bind: {
    label: 'TITLE',
    required: true,
    validation: 'required',
  },
}
 */

subject.build(model, 'description').getComponent() 
// returns null because it is not a FieldComponent
```

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