0.6.0 • Published 3 years ago

runtypes-filter v0.6.0

Weekly downloads
1,937
License
MIT
Repository
github
Last release
3 years ago

runtypes-filter

Clone and filter runtypes objects.

Limitations

Filtering objects with the following types is not supported because they may not be safely cloned.

  • InstanceOf
  • Intersect
  • Function

Usage

Recommended

Since not all types may be safely cloned, it is recommended to statically construct a filter method per type.

Filter methods are recursive. It is only necessary to filter at the top level.

import { Literal, Number, Record } from "runtypes";
import CheckFilter from "runtypes-filter";

const Asteroid = Record({
	type: Literal("asteroid"),
	mass: Number,
});

const filterAsteroid = CheckFilter(Asteroid);

const untrustedAsteroid: unknown = {
	type: "asteroid",
	mass: 100,
};

const trustedAteroid = filterAsteroid(untrustedAsteroid);

Manual

Validation of the type, checking unknown objects and filtering may also be handled manually.

import { Literal, Number, Record } from "runtypes";
import { filter, validate } from "runtypes-filter";

// Statically validate that the runtype can be filtered
const Asteroid = validate(
	Record({
		type: Literal("asteroid"),
		mass: Number,
	})
);

const untrustedAsteroid: unknown = {
	type: "asteroid",
	mass: 100,
};

const trustedAsteroid = Asteroid.check(untrustedAsteroid);

// This method will `throw` if `Asteroid` cannot be safely cloned
const filteredAsteroid = filter(Asteroid, trustedAsteroid);
0.6.0

3 years ago

0.5.1

3 years ago

0.5.0

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.0

4 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago