# schemy-reference-support

> Add reference support to Schemy

Latest version **1.1.0** (published 2021-08-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install schemy-reference-support
pnpm add schemy-reference-support
yarn add schemy-reference-support
bun add schemy-reference-support
```

## 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.1.0 |
| Published | 2021-08-29 |
| First published | 2021-08-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Alan Berdinelli |
| Maintainers | aeberdinelli |
| Keywords | schema, schemy, validation, json, node, js |

## Links

- npm: https://www.npmjs.com/package/schemy-reference-support
- npm.io page: https://npm.io/package/schemy-reference-support

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.1.0 (latest) — 2021-08-29
- 1.0.0 — 2021-08-28

## README

# Reference support
## Introduction 
This plugin adds the functionality to reference values from within a schema using `Schemy.$ref()`.

## Install
- `npm install --save schemy-reference-support`

## Usage
Add the plugin to Schemy:

```javascript
const Schemy = require('schemy');
const SchemyReferenceSupport = require('schemy-reference-support');

// Load the plugin into Schemy
Schemy.extend(SchemyReferenceSupport);

// Now you can use Schemy.$ref(propertyName)
```

## Examples
### In property settings
We can use a previously defined property within the definition of the schema. In the following example we set the max elements for an array using the `maxItems` property.

```javascript
const schema = new Schemy({
    maxItems: {
        type: Number,
        required: true
    },
    items: {
        type: [String],
        max: Schemy.$ref('maxItems')
    }
});

schema.validate({
	maxItems: 1,
    items: ['one','two']
}); // => false

schema.getValidationErrors(); // => [ 'Property items must contain no more than 1 elements' ]
```

### Direct reference
We can also reference directly to a property if we want two properties to match. This is useful for password confirmation:

```javascript
const schema = new Schemy({
    username: String,
    password: String,
    confirm: Schemy.$ref('password')
});

schema.validate({
    username: 'schemy',
    password: 'password1',
    confirm: 'password2'
}); // => false

schema.getValidationErrors(); // => [ 'Property confirm does not match referenced value: password' ]
```


## Notes
- Requires Schemy version **>= 3.2.1**

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