# functional-json-schema

> Build a JSON Schema with functions

Latest version **1.0.0** (published 2021-01-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install functional-json-schema
pnpm add functional-json-schema
yarn add functional-json-schema
bun add functional-json-schema
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2021-01-13 |
| First published | 2018-05-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 28.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 12 |
| Author | Charly POLY |
| Maintainers | wittydeveloper |

## Links

- npm: https://www.npmjs.com/package/functional-json-schema
- Repository: https://github.com/wittydeveloper/functional-json-schema
- Homepage: https://github.com/wittydeveloper/functional-json-schema#readme
- Issues: https://github.com/wittydeveloper/functional-json-schema/issues
- npm.io page: https://npm.io/package/functional-json-schema

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.19

## Recent versions

- 1.0.0 (latest) — 2021-01-13
- 0.0.2-3 (prerelease) — 2018-05-31
- 0.0.2-2 — 2018-05-23
- 0.0.2-1 — 2018-05-22
- 0.0.2-0 — 2018-05-22

## README

# functional-json-schema
Build a JSON Schema with functions



## Schema DSL

### Simple schema

```js

import * as schema from 'functional-json-schema';

const JsonSchema = schema.schema({
  person: {
    friends: schema.types.arrayOf(schema.types.definition('user')),
        id: 'string*',
        friend_ids: schema.types.arrayOf(schema.types.definition('User'), { required: true }),
    }
}, null, { schema: 'http://json-schema.org/draft-06/schema#' })

/*
=> {
  $schema: 'http://json-schema.org/draft-06/schema#',
  type: 'object',
  properties: {
    person: {
      type: 'object',
      properties: {
        friends: {
          "type": "array",
          "items": {
            "$ref": "#/definitions/User"
          }
        },
        (...)
      },
      required: ['friends', 'id']
    }
  }
}
*/
```
### Schema with definitions

```js

import * as schema from 'functional-json-schema';

const schemaWithDefinitions = schema(
  {
    community_list: {
      portfolio_ids: types.arrayOf("string", { required: true }),
      project_id: types.type("string", { required: true }),
      status: types.enumOf("active", "inactive"),
      user: types.anyOf(types.definition("User"), types.definition("Admin")),
    },
  }, {
    User: {
      firstName: types.type("string"),
      lastName: types.type("string"),
      jobTitle: types.type("string"),
      companyId: types.type("string"),
    },
  },
  { schema: 'http://json-schema.org/draft-06/schema#' }
);

/*
=> {
  $schema: 'http://json-schema.org/draft-06/schema#',
  type: "object",
  properties: {
    community_list: {
      type: "object",
      properties: {
        portfolio_ids: {
          type: "array",
          items: {
            type: "string",
          },
        },
        project_id: {
          type: "string",
        },
        user: {
          anyOf: [
            { $ref: "#/definitions/User" },
            { $ref: "#/definitions/Admin" },
          ],
        },
        status: {
          enum: ["active", "inactive"],
        },
      },
      required: ["portfolio_ids", "project_id"],
    },
  },
  definitions: {
    User: {
      type: "object",
      properties: {
        firstName: {
          type: "string",
        },
        lastName: {
          type: "string",
        },
        jobTitle: {
          type: "string",
        },
        companyId: {
          type: "string",
        },
      },
      required: [],
    },
  }
}
*/
```

## standalone definition DSL

```js

import * as schema from 'functional-json-schema';

const JsonSchemaDefinition = schema.definition(
  'User',
  {
    firstName: schema.types.type('string'),
    lastName: schema.types.type('string'),
    jobTitle: schema.types.type('string'),
    companyId: schema.types.type('string'),
  },
  { title: "User", description: "Fake User Object" }
)

/*
=> {
  "$ref": "#/definitions/User",
  "definitions": {
      "User": {
          "type": "object",
          "title": "User",
          "description": "Fake User Object",
          "properties": {
              "firstName": {
                  "type": "string"
              },
              "lastName": {
                  "type": "string"
              },
              "jobTitle": {
                  "type": "string"
              },
              "companyId": {
                  "type": "string"
              }
          },
          required: [],
      }
  }
}
*/
```

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