# ajv-semver

> Semver matching plugin for AJV

Latest version **2.0.2** (published 2023-12-22) · ISC license · 0 weekly downloads

## Install

```sh
npm install ajv-semver
pnpm add ajv-semver
yarn add ajv-semver
bun add ajv-semver
```

## 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 | 2.0.2 |
| Published | 2023-12-22 |
| First published | 2017-06-27 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 142.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Jason Thorpe |
| Maintainers | jdthorpe |
| Keywords | ajv, json, validation, semantic, version, semver, clean, match |

## Links

- npm: https://www.npmjs.com/package/ajv-semver
- Repository: https://github.com/jdthorpe/ajv-semver
- Homepage: https://github.com/jdthorpe/ajv-semver#readme
- Issues: https://github.com/jdthorpe/ajv-semver/issues
- npm.io page: https://npm.io/package/ajv-semver

## Dependencies (2)

- [semver](https://npm.io/package/semver.md) ^7.3.8
- [semver-regex](https://npm.io/package/semver-regex.md) ^3.0.0

## 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

- 2.0.2 (latest) — 2023-12-22
- 2.0.1 — 2023-05-16
- 2.0.0 — 2023-03-07
- 1.0.5 — 2022-01-24
- 1.0.4 — 2020-01-25
- 1.0.3 — 2019-03-19
- 1.0.2 — 2018-04-17
- 1.0.1 — 2017-06-29
- 1.0.0 — 2017-06-27

## README

# Semver string validation with AJV

### Getting started

Create an Ajv Instance and extend it with the `ajv-semver` plugin

```JavaScript
var Ajv = require("ajv");
var ajv = new Ajv();
require("ajv-semver")(ajv);
```

and then use it to validate semantic version ("semver") strings:

```JavaScript
ajv.validate({"type":"string","format":"semver"},
             "1.2.3")
ajv.validate({"type":"string","semver":true},
             "1.2.3")
ajv.validate({"type":"string","semver":{"valid":true}},
             "1.2.3")
ajv.validate({"type":"string","semver":{"valid":true,"loose":true}},
             "=1.2.3")
```

or validate version ranges:

```JavaScript
ajv.validate({"type":"string","semver":{"validRange":true}},
             ">=1.2.0 <1.3.0||>=2.0.0 <3.0.0")
ajv.validate({"type":"string","semver":{"validRange":true,"loose":true}},
             "~1.2.3beta")
```

or use one of the comparison operators (`gt`, `gte`, `lt`, `lte`):

```JavaScript
ajv.validate({"type":"string","semver":{"gt":"1.2.3-r100"}},
             "1.2.3-R2")
ajv.validate({"type":"string","semver":{"gte":"1.2.3"}},
             "2.0.0")
ajv.validate({"type":"string","semver":{"lt":"=4.1.1","loose":true}},
             "2.0.0")
ajv.validate({"type":"string","semver":{"lte":"3.2.3","loose":true}},
             "=2.0.0")
```

or use one of the range comparison operators (`gtr`, `ltr`):

```JavaScript
ajv.validate({"type":"string","semver":{"gtr":"1.0.0 - 2.0.0"}},
             "2.0.1-R2")
ajv.validate({"type":"string","semver":{"ltr":"1.0.0 - 2.0.0"}},
             "0.6.3-beta")
```

or you can validate object attributes and relations between them:

```JavaScript
var validator = ajv.compile({
	"type":"object",
	"properties":{
		"version":{
			"type":"string",
			"semver":{"clean":true}
		},
		// the `previousVersion` should be less than the current version
		"previousVersion":{
			"type":"string",
			"semver":{"lt":{"$data":"1/version"}}
		}
	},
	"required":["version"]
}) 

validator({"version":"1.2.3","previousVersion":"0.7.12"}) // true

// fails validation: `previousVersion` is not less than `version`
validator({"version":"1.2.3","previousVersion":"1.7.12"}) // false

// fails validation: invalid version string
validator({"version":"1.2.3","previousVersion":"a.b.c"}) // false
validator({"version":"a.b.c","previousVersion":"1.7.12"}) // false
```

or clean your version strings and calculate major, minor, and patch -- handy
for indexing or querying with MongoDB and friends:

```JavaScript
var Ajv = require("ajv");
var ajv = new Ajv({
		// required to create a new attribute
		"useDefaults":true
	});
require("./index.js")(ajv);

var validator = ajv.compile({
	"type":"object",
	"properties":{
		"version":{
			"type":"string",
			"semver":true
		},
		// calculate the major minor and patch
		"Major":{
			"default": null, // required to create a new attribute
			"semver":{"major":{"$data":"1/version"},"loose":true}
		},                                                
		"Minor":{                                           
			"default": null, // required to create a new attribute
			"semver":{"minor":{"$data":"1/version"},"loose":true}
		},                                                
		"Patch":{                                           
			"default": null, // required to create a new attribute
			"semver":{"patch":{"$data":"1/version"},"loose":true}
		}
	},
	"required":["version"]
}) 

var obj = {"version":"=2.2.3"}
validator(obj) // true
obj // { version: '2.2.3', Major: 2, Minor: 2, Patch: 3 }

var obj = {"version":"=a.b.c"}
validator(obj) // false
obj // { version: null, Major: null, Minor: null, Patch: null }
```

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