Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

validation.ts

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

validation.ts - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

118

lib/validation.d.ts
import { Result, Ok, Err } from 'space-lift/result';
export interface Validator<T> {
export declare abstract class Validator<T> {
readonly T: T;
readonly validate: (value: Value, context: Context) => Validation<T>;
abstract validate(value: Value, context?: Context): Validation<T>;
map<B>(fn: (value: T) => B): Validator<B>;
filter(fn: (value: T) => boolean): Validator<T>;
}

@@ -24,47 +26,38 @@ export declare type Any = Validator<Object>;

export declare function getContext(name: string, parent?: string): Context;
export declare function validate<T>(value: Value, validator: Validator<T>): Validation<T>;
export declare function is<T>(value: Value, validator: Validator<T>): value is T;
export declare class NullValidator {
T: null;
validate(v: Value, c: Context): Validation<null>;
export declare class NullValidator extends Validator<null> {
validate(v: Value, c?: Context): Result<ValidationError[], null>;
}
export declare class UndefinedValidator {
T: undefined;
validate(v: Value, c: Context): Validation<undefined>;
export declare class UndefinedValidator extends Validator<undefined> {
validate(v: Value, c?: Context): Result<ValidationError[], undefined>;
}
export declare class StringValidator implements Validator<string> {
T: string;
validate(v: Value, c: Context): Validation<string>;
export declare class StringValidator extends Validator<string> {
validate(v: Value, c?: Context): Result<ValidationError[], never>;
}
export declare class NumberValidator {
T: number;
validate(v: Value, c: Context): Validation<number>;
export declare class NumberValidator extends Validator<number> {
validate(v: Value, c?: Context): Result<ValidationError[], never>;
}
export declare class BooleanValidator {
T: boolean;
validate(v: Value, c: Context): Validation<boolean>;
export declare class BooleanValidator extends Validator<boolean> {
validate(v: Value, c?: Context): Result<ValidationError[], never>;
}
export declare class MappedValidator<A extends Any, B> {
export declare class MappedValidator<A, B> extends Validator<B> {
private validator;
private f;
constructor(validator: A, f: (a: TypeOf<A>) => B);
T: B;
validate(v: Value, c: Context): Result<ValidationError[], B>;
constructor(validator: Validator<A>, f: (a: A) => B);
validate(v: Value, c?: Context): Result<ValidationError[], B>;
}
export declare function map<A extends Any, B>(validator: A, f: (a: TypeOf<A>) => B): MappedValidator<A, B>;
export declare class FilteredValidator<A extends Any> {
export declare function map<A, B>(validator: Validator<A>, f: (a: A) => B): MappedValidator<A, B>;
export declare class FilteredValidator<A> extends Validator<A> {
private validator;
private predicate;
constructor(validator: A, predicate: (a: TypeOf<A>) => boolean);
T: TypeOf<A>;
validate(v: Value, c: Context): Result<ValidationError[], Object>;
constructor(validator: Validator<A>, predicate: (a: A) => boolean);
validate(v: Value, c?: Context): Result<ValidationError[], A>;
}
export declare function filter<A extends Any>(validator: A, predicate: (a: TypeOf<A>) => boolean): FilteredValidator<A>;
export declare class ArrayValidator<A extends Any> {
export declare function filter<A>(validator: Validator<A>, predicate: (a: A) => boolean): FilteredValidator<A>;
export declare class ArrayValidator<A> extends Validator<A[]> {
private validator;
constructor(validator: A);
T: TypeOf<A>[];
validate(v: Value, c: Context): Validation<A[]>;
constructor(validator: Validator<A>);
validate(v: Value, c?: Context): Ok<ValidationError[], never> | Err<ValidationError[], never> | Ok<never, any[]> | Err<never, any[]>;
}
export declare function array<A extends Any>(validator: A): Validator<TypeOf<A>[]>;
export declare function array<A>(validator: Validator<A>): Validator<A[]>;
export declare type Props = Record<string, Any>;

@@ -74,55 +67,48 @@ export declare type InterfaceFor<P extends Props> = {

};
export declare class ObjectValidator<P extends Props> {
export declare class ObjectValidator<P extends Props> extends Validator<InterfaceFor<P>> {
private props;
constructor(props: P);
T: InterfaceFor<P>;
validate(v: Value, c: Context): Validation<InterfaceFor<P>>;
validate(v: Value, c?: Context): Ok<ValidationError[], never> | Err<ValidationError[], never> | Ok<never, any> | Err<never, any>;
}
export declare function object<P extends Props>(props: P): Validator<InterfaceFor<P>>;
export declare class KeyOfValidator<KEYS extends object> {
export declare class KeyOfValidator<KEYS extends object> extends Validator<keyof KEYS> {
private keys;
constructor(keys: KEYS);
T: keyof KEYS;
validate(v: Value, c: Context): Validation<keyof KEYS>;
validate(v: Value, c?: Context): Validation<keyof KEYS>;
}
export declare function keyof<KEYS extends object>(keys: KEYS): KeyOfValidator<KEYS>;
export declare class DictionaryValidator<D extends Validator<string>, CD extends Any> {
export declare class DictionaryValidator<K extends string, V> extends Validator<Record<K, V>> {
private domain;
private codomain;
constructor(domain: D, codomain: CD);
T: Record<TypeOf<D>, TypeOf<CD>>;
validate(v: Value, c: Context): Ok<ValidationError[], never> | Err<ValidationError[], never> | Ok<never, any> | Err<never, any>;
constructor(domain: Validator<K>, codomain: Validator<V>);
validate(v: Value, c?: Context): Ok<ValidationError[], never> | Err<ValidationError[], never> | Ok<never, any> | Err<never, any>;
}
export declare function dictionary<D extends Validator<string>, CD extends Any>(domain: D, codomain: CD): Validator<Record<TypeOf<D>, TypeOf<CD>>>;
export declare function dictionary<K extends string, V>(domain: Validator<K>, codomain: Validator<V>): Validator<Record<K, V>>;
export declare type Literal = string | number | boolean;
export declare function literal<V extends Literal>(value: V): Validator<V>;
export declare class UnionValidator<A extends Any> {
export declare class UnionValidator<A> extends Validator<A> {
private validators;
constructor(validators: A[]);
T: TypeOf<A>;
validate(v: Value, c: Context): Validation<TypeOf<A>>;
constructor(validators: Validator<A>[]);
validate(v: Value, c?: Context): Err<ValidationError[], never> | Ok<ValidationError[], A>;
}
export declare class LiteralUnionValidator<A extends Literal> {
export declare class LiteralUnionValidator<A extends Literal> extends Validator<A> {
private values;
constructor(values: A[]);
T: A;
validate(v: Value, c: Context): Validation<A>;
validate(v: Value, c: Context): Err<ValidationError[], never> | Ok<ValidationError[], A>;
}
export declare function union<A extends Any, B extends Any>(a: A, b: B): UnionValidator<A | B>;
export declare function union<A extends Literal, B extends Literal>(a: A, b: B): LiteralUnionValidator<A | B>;
export declare function union<A extends Any, B extends Any, C extends Any>(a: A, b: B, c: C): UnionValidator<A | B | C>;
export declare function union<A extends Literal, B extends Literal, C extends Literal>(a: A, b: B, c: C): LiteralUnionValidator<A | B | C>;
export declare function union<A extends Any, B extends Any, C extends Any, D extends Any>(a: A, b: B, c: C, d: D): UnionValidator<A | B | C | D>;
export declare function union<A extends Literal, B extends Literal, C extends Literal, D extends Literal>(a: A, b: B, c: C, d: D): LiteralUnionValidator<A | B | C | D>;
export declare class OptionalValidator<V extends Any> {
export declare function union<A, B>(a: Validator<A>, b: Validator<B>): Validator<A | B>;
export declare function union<A extends Literal, B extends Literal>(a: A, b: B): Validator<A | B>;
export declare function union<A, B, C>(a: Validator<A>, b: Validator<B>, c: Validator<C>): Validator<A | B | C>;
export declare function union<A extends Literal, B extends Literal, C extends Literal>(a: A, b: B, c: C): Validator<A | B | C>;
export declare function union<A, B, C, D>(a: Validator<A>, b: Validator<B>, c: Validator<C>, d: Validator<D>): Validator<A | B | C | D>;
export declare function union<A extends Literal, B extends Literal, C extends Literal, D extends Literal>(a: A, b: B, c: C, d: D): Validator<A | B | C | D>;
export declare class OptionalValidator<V> extends Validator<V | undefined> {
private validator;
constructor(validator: V);
T: TypeOf<V> | undefined;
validate(v: Value, c: Context): Validation<TypeOf<V> | undefined>;
constructor(validator: Validator<V>);
validate(v: Value, c?: Context): Ok<ValidationError[], undefined> | Err<ValidationError[], undefined> | Ok<ValidationError[], V> | Err<ValidationError[], V>;
}
export declare function optional<V extends Any>(validator: V): OptionalValidator<V>;
export declare function recursion<T>(definition: (self: Any) => Any): Validator<T>;
export declare class IsoDateValidator {
T: Date;
validate(v: Value, c: Context): Result<ValidationError[], Date>;
export declare function optional<V>(validator: Validator<V>): Validator<V | undefined>;
export declare function recursion<T>(definition: (self: Validator<T>) => Any): Validator<T>;
export declare class IsoDateValidator extends Validator<Date> {
validate(v: Value, c?: Context): Result<ValidationError[], Date>;
}

@@ -129,0 +115,0 @@ export declare function errorDebugString(errors: ValidationError[]): string;

"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || Object.assign || function(t) {

@@ -12,2 +22,17 @@ for (var s, i = 1, n = arguments.length; i < n; i++) {

var result_1 = require("space-lift/result");
//--------------------------------------
// Setup
//--------------------------------------
var Validator = (function () {
function Validator() {
}
Validator.prototype.map = function (fn) {
return new MappedValidator(this, fn);
};
Validator.prototype.filter = function (fn) {
return new FilteredValidator(this, fn);
};
return Validator;
}());
exports.Validator = Validator;
function success(value) {

@@ -37,8 +62,5 @@ return result_1.Ok(value);

exports.getContext = getContext;
function validate(value, validator) {
return validator.validate(value, getContext('root'));
}
exports.validate = validate;
var rootContext = getContext('root');
function is(value, validator) {
return validate(value, validator).isOk();
return validator.validate(value).isOk();
}

@@ -49,46 +71,61 @@ exports.is = is;

//--------------------------------------
var NullValidator = (function () {
var NullValidator = (function (_super) {
__extends(NullValidator, _super);
function NullValidator() {
return _super !== null && _super.apply(this, arguments) || this;
}
NullValidator.prototype.validate = function (v, c) {
if (c === void 0) { c = rootContext; }
return v === null ? success(v) : typeFailure(v, c, 'null');
};
return NullValidator;
}());
}(Validator));
exports.NullValidator = NullValidator;
var UndefinedValidator = (function () {
var UndefinedValidator = (function (_super) {
__extends(UndefinedValidator, _super);
function UndefinedValidator() {
return _super !== null && _super.apply(this, arguments) || this;
}
UndefinedValidator.prototype.validate = function (v, c) {
if (c === void 0) { c = rootContext; }
return v === void 0 ? success(v) : typeFailure(v, c, 'undefined');
};
return UndefinedValidator;
}());
}(Validator));
exports.UndefinedValidator = UndefinedValidator;
var StringValidator = (function () {
var StringValidator = (function (_super) {
__extends(StringValidator, _super);
function StringValidator() {
return _super !== null && _super.apply(this, arguments) || this;
}
StringValidator.prototype.validate = function (v, c) {
if (c === void 0) { c = rootContext; }
return typeof v === 'string' ? success(v) : typeFailure(v, c, 'string');
};
return StringValidator;
}());
}(Validator));
exports.StringValidator = StringValidator;
var NumberValidator = (function () {
var NumberValidator = (function (_super) {
__extends(NumberValidator, _super);
function NumberValidator() {
return _super !== null && _super.apply(this, arguments) || this;
}
NumberValidator.prototype.validate = function (v, c) {
if (c === void 0) { c = rootContext; }
return typeof v === 'number' ? success(v) : typeFailure(v, c, 'number');
};
return NumberValidator;
}());
}(Validator));
exports.NumberValidator = NumberValidator;
var BooleanValidator = (function () {
var BooleanValidator = (function (_super) {
__extends(BooleanValidator, _super);
function BooleanValidator() {
return _super !== null && _super.apply(this, arguments) || this;
}
BooleanValidator.prototype.validate = function (v, c) {
if (c === void 0) { c = rootContext; }
return typeof v === 'boolean' ? success(v) : typeFailure(v, c, 'boolean');
};
return BooleanValidator;
}());
}(Validator));
exports.BooleanValidator = BooleanValidator;

@@ -98,10 +135,16 @@ //--------------------------------------

//--------------------------------------
var MappedValidator = (function () {
var MappedValidator = (function (_super) {
__extends(MappedValidator, _super);
function MappedValidator(validator, f) {
this.validator = validator;
this.f = f;
var _this = _super.call(this) || this;
_this.validator = validator;
_this.f = f;
return _this;
}
MappedValidator.prototype.validate = function (v, c) { return this.validator.validate(v, c).map(this.f); };
MappedValidator.prototype.validate = function (v, c) {
if (c === void 0) { c = rootContext; }
return this.validator.validate(v, c).map(this.f);
};
return MappedValidator;
}());
}(Validator));
exports.MappedValidator = MappedValidator;

@@ -115,9 +158,13 @@ function map(validator, f) {

//--------------------------------------
var FilteredValidator = (function () {
var FilteredValidator = (function (_super) {
__extends(FilteredValidator, _super);
function FilteredValidator(validator, predicate) {
this.validator = validator;
this.predicate = predicate;
var _this = _super.call(this) || this;
_this.validator = validator;
_this.predicate = predicate;
return _this;
}
FilteredValidator.prototype.validate = function (v, c) {
var _this = this;
if (c === void 0) { c = rootContext; }
var validated = this.validator.validate(v, c);

@@ -129,3 +176,3 @@ return validated.flatMap(function (v) {

return FilteredValidator;
}());
}(Validator));
exports.FilteredValidator = FilteredValidator;

@@ -139,7 +186,11 @@ function filter(validator, predicate) {

//--------------------------------------
var ArrayValidator = (function () {
var ArrayValidator = (function (_super) {
__extends(ArrayValidator, _super);
function ArrayValidator(validator) {
this.validator = validator;
var _this = _super.call(this) || this;
_this.validator = validator;
return _this;
}
ArrayValidator.prototype.validate = function (v, c) {
if (c === void 0) { c = rootContext; }
if (!Array.isArray(v))

@@ -164,3 +215,3 @@ return typeFailure(v, c, 'array');

return ArrayValidator;
}());
}(Validator));
exports.ArrayValidator = ArrayValidator;

@@ -171,7 +222,11 @@ function array(validator) {

exports.array = array;
var ObjectValidator = (function () {
var ObjectValidator = (function (_super) {
__extends(ObjectValidator, _super);
function ObjectValidator(props) {
this.props = props;
var _this = _super.call(this) || this;
_this.props = props;
return _this;
}
ObjectValidator.prototype.validate = function (v, c) {
if (c === void 0) { c = rootContext; }
if (v == null || typeof v !== 'object')

@@ -197,3 +252,3 @@ return typeFailure(v, c, 'object');

return ObjectValidator;
}());
}(Validator));
exports.ObjectValidator = ObjectValidator;

@@ -207,7 +262,11 @@ function object(props) {

//--------------------------------------
var KeyOfValidator = (function () {
var KeyOfValidator = (function (_super) {
__extends(KeyOfValidator, _super);
function KeyOfValidator(keys) {
this.keys = keys;
var _this = _super.call(this) || this;
_this.keys = keys;
return _this;
}
KeyOfValidator.prototype.validate = function (v, c) {
if (c === void 0) { c = rootContext; }
return this.keys.hasOwnProperty(v)

@@ -218,3 +277,3 @@ ? success(v)

return KeyOfValidator;
}());
}(Validator));
exports.KeyOfValidator = KeyOfValidator;

@@ -228,8 +287,12 @@ function keyof(keys) {

//--------------------------------------
var DictionaryValidator = (function () {
var DictionaryValidator = (function (_super) {
__extends(DictionaryValidator, _super);
function DictionaryValidator(domain, codomain) {
this.domain = domain;
this.codomain = codomain;
var _this = _super.call(this) || this;
_this.domain = domain;
_this.codomain = codomain;
return _this;
}
DictionaryValidator.prototype.validate = function (v, c) {
if (c === void 0) { c = rootContext; }
if (v == null || typeof v !== 'object')

@@ -269,3 +332,3 @@ return typeFailure(v, c, 'object');

return DictionaryValidator;
}());
}(Validator));
exports.DictionaryValidator = DictionaryValidator;

@@ -276,7 +339,11 @@ function dictionary(domain, codomain) {

exports.dictionary = dictionary;
var LiteralValidator = (function () {
var LiteralValidator = (function (_super) {
__extends(LiteralValidator, _super);
function LiteralValidator(value) {
this.value = value;
var _this = _super.call(this) || this;
_this.value = value;
return _this;
}
LiteralValidator.prototype.validate = function (v, c) {
if (c === void 0) { c = rootContext; }
return v === this.value

@@ -287,3 +354,3 @@ ? success(v)

return LiteralValidator;
}());
}(Validator));
function literal(value) {

@@ -296,7 +363,11 @@ return new LiteralValidator(value);

//--------------------------------------
var UnionValidator = (function () {
var UnionValidator = (function (_super) {
__extends(UnionValidator, _super);
function UnionValidator(validators) {
this.validators = validators;
var _this = _super.call(this) || this;
_this.validators = validators;
return _this;
}
UnionValidator.prototype.validate = function (v, c) {
if (c === void 0) { c = rootContext; }
for (var i = 0; i < this.validators.length; i++) {

@@ -310,7 +381,10 @@ var validation = this.validators[i].validate(v, c);

return UnionValidator;
}());
}(Validator));
exports.UnionValidator = UnionValidator;
var LiteralUnionValidator = (function () {
var LiteralUnionValidator = (function (_super) {
__extends(LiteralUnionValidator, _super);
function LiteralUnionValidator(values) {
this.values = values;
var _this = _super.call(this) || this;
_this.values = values;
return _this;
}

@@ -327,3 +401,3 @@ LiteralUnionValidator.prototype.validate = function (v, c) {

return LiteralUnionValidator;
}());
}(Validator));
exports.LiteralUnionValidator = LiteralUnionValidator;

@@ -335,3 +409,5 @@ function union() {

}
return (typeof values[0] === 'object') ? new UnionValidator(values) : new LiteralUnionValidator(values);
return (typeof values[0] === 'object')
? new UnionValidator(values)
: new LiteralUnionValidator(values);
}

@@ -342,7 +418,11 @@ exports.union = union;

//--------------------------------------
var OptionalValidator = (function () {
var OptionalValidator = (function (_super) {
__extends(OptionalValidator, _super);
function OptionalValidator(validator) {
this.validator = validator;
var _this = _super.call(this) || this;
_this.validator = validator;
return _this;
}
OptionalValidator.prototype.validate = function (v, c) {
if (c === void 0) { c = rootContext; }
if (v === undefined)

@@ -353,3 +433,3 @@ return success(v);

return OptionalValidator;
}());
}(Validator));
exports.OptionalValidator = OptionalValidator;

@@ -364,3 +444,7 @@ function optional(validator) {

function recursion(definition) {
var Self = { validate: function (v, c) { return Result.validate(v, c); } };
var Self = new Validator();
Self.validate = function (v, c) {
if (c === void 0) { c = rootContext; }
return Result.validate(v, c);
};
var Result = definition(Self);

@@ -373,6 +457,9 @@ return Result;

//--------------------------------------
var IsoDateValidator = (function () {
var IsoDateValidator = (function (_super) {
__extends(IsoDateValidator, _super);
function IsoDateValidator() {
return _super !== null && _super.apply(this, arguments) || this;
}
IsoDateValidator.prototype.validate = function (v, c) {
if (c === void 0) { c = rootContext; }
if (typeof v !== 'string')

@@ -386,3 +473,3 @@ return typeFailure(v, c, 'string');

return IsoDateValidator;
}());
}(Validator));
exports.IsoDateValidator = IsoDateValidator;

@@ -389,0 +476,0 @@ //--------------------------------------

{
"name": "validation.ts",
"version": "0.0.1",
"version": "0.0.2",
"description": "Validation for TypeScript",

@@ -5,0 +5,0 @@ "main": "lib/validation.js",

@@ -7,3 +7,3 @@ # validation.ts

The `validate` function returns a [Result](https://github.com/AlexGalays/spacelift#api.result)
Every validator has a `validate` function which returns a [Result](https://github.com/AlexGalays/spacelift#api.result)

@@ -14,7 +14,7 @@ A validated value can be transformed at any point during the validation process (e.g. `isoDate`).

```ts
import { validate, errorDebugString } from 'validation.ts'
import { errorDebugString } from 'validation.ts'
const myValidator = ...
const result = validate(myJson, myValidator)
const result = myValidator.validate(myJson)

@@ -46,3 +46,4 @@ result.fold(

const validator = literal('X') // The only value that can ever pass this validation is the 'X' string literal
// The only value that can ever pass this validation is the 'X' string literal
const validator = literal('X')
```

@@ -91,3 +92,3 @@

import Set from 'space-lift/object/set'
import { keyof, validate } from 'validation.ts'
import { keyof } from 'validation.ts'

@@ -98,3 +99,3 @@ const keys = Set('aa', 'bb', 'cc').value()

validate('bb', keyValidator) // Ok<'aa' | 'bb' | 'cc'> = Ok('bb')
keyValidator.validate('bb') // Ok<'aa' | 'bb' | 'cc'> = Ok('bb')

@@ -110,3 +111,3 @@ // keyof typeof keys === typeof keyValidator.T === 'aa' | 'bb' | 'cc'

const validator = map(filter(string, str => str.length > 3), str => `${str}...`)
const validator = string.filter(str => str.length > 3).map(str => `${str}...`)
```

@@ -113,0 +114,0 @@

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc