Socket
Socket
Sign inDemoInstall

validate-value

Package Overview
Dependencies
11
Maintainers
5
Versions
91
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 9.2.2 to 9.3.0

build/lib/getDefaultAjvInstance.d.ts

3

build/lib/index.d.ts

@@ -0,1 +1,2 @@

import { getDefaultAjvInstance } from './getDefaultAjvInstance';
import { isOfType } from './isOfType';

@@ -7,2 +8,2 @@ import { JSONSchema7 as JsonSchema } from 'json-schema';

export type { JsonSchema };
export { isOfType, parse, ParseError, Parser };
export { getDefaultAjvInstance, isOfType, parse, ParseError, Parser };
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Parser = exports.ParseError = exports.parse = exports.isOfType = void 0;
exports.Parser = exports.ParseError = exports.parse = exports.isOfType = exports.getDefaultAjvInstance = void 0;
const getDefaultAjvInstance_1 = require("./getDefaultAjvInstance");
Object.defineProperty(exports, "getDefaultAjvInstance", { enumerable: true, get: function () { return getDefaultAjvInstance_1.getDefaultAjvInstance; } });
const isOfType_1 = require("./isOfType");

@@ -5,0 +7,0 @@ Object.defineProperty(exports, "isOfType", { enumerable: true, get: function () { return isOfType_1.isOfType; } });

import { JSONSchema7 } from 'json-schema';
import { ParseError } from './ParseError';
import { ValidateFunction } from 'ajv';
import Ajv, { ValidateFunction } from 'ajv';
import { Result } from 'defekt';
interface ParserOptions {
ajvInstance: Ajv;
}
declare class Parser<TParsed> {
protected validateInternal: ValidateFunction;
constructor(schema: JSONSchema7);
constructor(schema: JSONSchema7, { ajvInstance }?: ParserOptions);
parse(unparsedValue: any, { valueName, separator }?: {

@@ -9,0 +12,0 @@ valueName?: string;

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Parser = void 0;
const ajv_formats_1 = __importDefault(require("ajv-formats"));
const getByInstancePath_1 = require("./getByInstancePath");
const getDefaultAjvInstance_1 = require("./getDefaultAjvInstance");
const ParseError_1 = require("./ParseError");
const typedescriptor_1 = require("typedescriptor");
const ajv_1 = __importDefault(require("ajv"));
const defekt_1 = require("defekt");
const ajvInstance = new ajv_1.default({
allowUnionTypes: true
});
(0, ajv_formats_1.default)(ajvInstance);
ajvInstance.addFormat('alphanumeric', /[a-zA-Z0-9]/u);
const defaultParserOptions = {
ajvInstance: (0, getDefaultAjvInstance_1.getDefaultAjvInstance)()
};
class Parser {
constructor(schema) {
constructor(schema, { ajvInstance } = defaultParserOptions) {
this.validateInternal = ajvInstance.compile(schema);

@@ -21,0 +15,0 @@ }

@@ -0,1 +1,8 @@

# [9.3.0](https://github.com/thenativeweb/validate-value/compare/9.2.2...9.3.0) (2022-03-21)
### Features
* Support custom Ajv instances ([#419](https://github.com/thenativeweb/validate-value/issues/419)) ([e4b602e](https://github.com/thenativeweb/validate-value/commit/e4b602e36fe1c071717857206dc98397657c003b))
## [9.2.2](https://github.com/thenativeweb/validate-value/compare/9.2.1...9.2.2) (2022-02-03)

@@ -2,0 +9,0 @@

@@ -0,1 +1,2 @@

import { getDefaultAjvInstance } from './getDefaultAjvInstance';
import { isOfType } from './isOfType';

@@ -8,2 +9,8 @@ import { JSONSchema7 as JsonSchema } from 'json-schema';

export type { JsonSchema };
export { isOfType, parse, ParseError, Parser };
export {
getDefaultAjvInstance,
isOfType,
parse,
ParseError,
Parser
};

@@ -1,3 +0,3 @@

import addFormats from 'ajv-formats';
import { getByInstancePath } from './getByInstancePath';
import { getDefaultAjvInstance } from './getDefaultAjvInstance';
import { JSONSchema7 } from 'json-schema';

@@ -9,8 +9,9 @@ import { ParseError } from './ParseError';

const ajvInstance = new Ajv({
allowUnionTypes: true
});
interface ParserOptions {
ajvInstance: Ajv;
}
addFormats(ajvInstance);
ajvInstance.addFormat('alphanumeric', /[a-zA-Z0-9]/u);
const defaultParserOptions: ParserOptions = {
ajvInstance: getDefaultAjvInstance()
};

@@ -20,3 +21,3 @@ class Parser<TParsed> {

public constructor (schema: JSONSchema7) {
public constructor (schema: JSONSchema7, { ajvInstance }: ParserOptions = defaultParserOptions) {
this.validateInternal = ajvInstance.compile(schema);

@@ -23,0 +24,0 @@ }

The MIT License (MIT)
Copyright (c) 2018-2021 the native web.
Copyright (c) 2018-2022 the native web.

@@ -4,0 +4,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

{
"name": "validate-value",
"version": "9.2.2",
"version": "9.3.0",
"description": "validate-value validates values against JSON schemas.",

@@ -21,2 +21,6 @@ "contributors": [

"email": "alexander.kampf@thenativeweb.io"
},
{
"name": "Matthias Hermsen",
"email": "matthiashermsen@gmail.com"
}

@@ -28,6 +32,6 @@ ],

"dependencies": {
"@types/json-schema": "7.0.9",
"ajv": "8.9.0",
"@types/json-schema": "7.0.10",
"ajv": "8.10.0",
"ajv-formats": "2.1.1",
"defekt": "8.1.0",
"defekt": "8.2.0",
"json-schema": "0.4.0",

@@ -38,3 +42,3 @@ "typedescriptor": "4.0.12"

"assertthat": "6.4.0",
"roboter": "12.6.5",
"roboter": "12.7.1",
"semantic-release-configuration": "2.0.7"

@@ -41,0 +45,0 @@ },

@@ -10,5 +10,3 @@ # validate-value

| Version | [![npm](https://img.shields.io/npm/v/validate-value)](https://www.npmjs.com/package/validate-value) |
| Dependencies | ![David](https://img.shields.io/david/thenativeweb/validate-value) |
| Dev dependencies | ![David](https://img.shields.io/david/dev/thenativeweb/validate-value) |
| Build | ![GitHub Actions](https://github.com/thenativeweb/validate-value/workflows/Release/badge.svg?branch=main) |
| Build | ![GitHub Actions](https://github.com/thenativeweb/validate-value/workflows/Release/badge.svg?branch=main) |
| License | ![GitHub](https://img.shields.io/github/license/thenativeweb/validate-value) |

@@ -118,6 +116,10 @@

## Configuring the parser
### Configuring the parser
By default, the error message uses `value` as identifier and `.` as the separator for the object that is parsed, but sometimes you may want to change this. Therefor, provide the desired identifier and separator as second parameter to the `parse` function:
If you want to, you can configure the parser according to your needs.
#### Configuring the error messages
By default, the error message uses `value` as identifier and `.` as the separator for the object that is parsed, but sometimes you may want to change this. Therefore, provide the desired identifier and separator as second parameter to the `parse` function:
```typescript

@@ -132,4 +134,32 @@ const user = {

## Parsing without a parser instance
#### Using a custom validator
Sometimes it is desired to use a custom validator instance, e.g. if you want to add custom formats. For that, you can hand over a custom [Ajv](https://ajv.js.org/) instance to the parser:
```typescript
const schema = // ...
const ajvInstance = new Ajv();
// Adjust Ajv instance according to your needs...
const parser = new Parser<User>(schema, {
ajvInstance
});
```
Optionally, you can build upon the default instance that is used internally. For that, call the `getDefaultAjvInstance` function:
```typescript
const schema = // ...
const ajvInstance = getDefaultAjvInstance();
// Adjust Ajv instance according to your needs...
const parser = new Parser<User>(schema, {
ajvInstance
});
```
### Parsing without a parser instance
For convenience, there is also the `parse` function, which skips the creation of a parser instance. You can use this if you're only going to use a schema for validation once. Otherwise, it is recommended to first create a parser instance, since then the JSON schema is only compiled once:

@@ -151,3 +181,3 @@

### Verifying that a variable is of a specific type
#### Verifying that a variable is of a specific type

@@ -154,0 +184,0 @@ To verify that a variable is of a specific type, use the `isOfType` function. Hand over the value you would like to verify, and a JSON schema describing that type. The function returns `true` if the given variable matches the schema, and `false` if it doesn't:

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc