0.8.0 • Published 2 years ago

ugd10a v0.8.0

Weekly downloads
2
License
ISC
Repository
github
Last release
2 years ago

UGD10A!

A small bunch of data, time and object utilities placed into public scope for easier reuse.

Runtime data validation

Utility that ensures that incoming data is of required shape a type.

Simple usage example look like this:

import {Validator} from "ugd10a/validator";
// Data shape to validate incoming data against
type OuterData = {
    id: number,
    name: string,
    description?: string
}
// Create validator
const validator = new Validator<OuterData>({
    id: {type: "number"},
    name: {type: "string"},
    description: {type: "string", optional: true},
});

// This will return true
validator.validate({id: 1, name: "name"});

// As well as this
validator.validate({id: 1, name: "name", description: "description"});

// But this will return false
validator.validate({id: "1", name: "name", description: "description"});
// Reason for failure can be checked by validator.lastError property, so:
console.log(validator.lastError);
// Will return:
{
    "field": "id",
    "error": "Type mismatch. Expected: number, actual: string"
}

Configuration options

  • optional: Defines if it's fine for this field to be missing (default = false).

  • exactValue: Check entry for exact value.

  • type: String representing one of built in types: undefined | object | boolean | number | bigint | string | symbol | function or any of custom, array types : array | string[] | number[].
    (Value can contain a single entry or an array of entries.)

  • validator: Entry validator function of form: (value: any, field?: keyof T) => boolean | string where true indicate that value is valid, false is simple denial of value, and string represent reasoned denial that will be included in error message.

  • itemValidator: Array item validator that will be used only in conjunction with array data type.

  • notEmpty: Defines that empty values, such as empty strings or arrays, or undefined should not be accepted as valid ones.

In memory data caching

import {Cached, toMilliseconds} from "ugd10a";

const randomNumberCache = new Cached(
    // Async function that knows where to get new data
    async () => Math.floor(Math.random() * 0xFFFFFF),
    // Time in milliseconds which defines data update interval
    toMilliseconds(10, "seconds")
);
// This will output new random number every 10 seconds.
setInterval(
    async () => console.log(await randomNumberCache.getData()),
    toMilliseconds(1, "seconds")
);

Typical use case for this functionality would be to limit requests to scarce resource, like DB and keep last known value in memory between updates within single entry point.

TODO: More docs object utils, execution queue and time utils?
0.8.0

2 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.6.10

4 years ago

0.6.9

4 years ago

0.6.8

4 years ago

0.6.7

4 years ago

0.6.6

4 years ago

0.6.5

4 years ago

0.6.4

4 years ago

0.6.3

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.0

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago