Comparing version 1.0.3 to 1.1.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateArray = void 0; | ||
const type_def_helpers_1 = require("../../type-def-helpers"); | ||
const validation_error_1 = require("../validation-error/validation-error"); | ||
@@ -10,5 +11,5 @@ const validate_one_of_1 = require("./validate-one-of"); | ||
for (const [index, elem] of data.entries()) { | ||
(0, validate_one_of_1.validateOneOf)(`${path}.${index}`, { oneOf: type.arrayOf }, elem); | ||
(0, validate_one_of_1.validateOneOf)(`${path}.${index}`, type_def_helpers_1.DataType.OneOf(...type.arrayOf), elem); | ||
} | ||
}; | ||
exports.validateArray = validateArray; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateRecord = void 0; | ||
const is_field_descriptor_1 = require("../../shared/is-field-descriptor"); | ||
const validation_error_1 = require("../validation-error/validation-error"); | ||
const validate_type_1 = require("./validate-type"); | ||
const getType = (v) => { | ||
if ((0, is_field_descriptor_1.isFieldDescriptor)(v)) | ||
return v.type; | ||
return v; | ||
}; | ||
const validateRecord = (path, type, data) => { | ||
@@ -11,8 +17,13 @@ if (typeof data !== "object" || data === null) | ||
if (!(key in data)) { | ||
if (fieldDescriptor.required === true || | ||
fieldDescriptor.required === undefined) { | ||
throw new validation_error_1.ValidationError(path, type, data); | ||
if ((0, is_field_descriptor_1.isFieldDescriptor)(fieldDescriptor)) { | ||
if (fieldDescriptor.required === true || | ||
fieldDescriptor.required === undefined) { | ||
throw new validation_error_1.ValidationError(path, type, data); | ||
} | ||
else { | ||
continue; | ||
} | ||
} | ||
else { | ||
continue; | ||
throw new validation_error_1.ValidationError(path, type, data); | ||
} | ||
@@ -22,5 +33,5 @@ } | ||
const value = data[key]; | ||
(0, validate_type_1.validateType)(`${path}.${key}`, fieldDescriptor.type, value); | ||
(0, validate_type_1.validateType)(`${path}.${key}`, getType(fieldDescriptor), value); | ||
} | ||
}; | ||
exports.validateRecord = validateRecord; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateSet = void 0; | ||
const type_def_helpers_1 = require("../../type-def-helpers"); | ||
const validation_error_1 = require("../validation-error/validation-error"); | ||
@@ -16,5 +17,5 @@ const validate_one_of_1 = require("./validate-one-of"); | ||
for (const elem of data) { | ||
(0, validate_one_of_1.validateOneOf)(`${path}.SET`, { oneOf: type.setOf }, elem); | ||
(0, validate_one_of_1.validateOneOf)(`${path}.SET`, type_def_helpers_1.DataType.OneOf(...type.setOf), elem); | ||
} | ||
}; | ||
exports.validateSet = validateSet; |
@@ -7,2 +7,8 @@ declare const _default: { | ||
ensureDataType: <DT_2 extends import("./types").AllDataTypes>(dataType: DT_2, data: unknown) => void; | ||
And: <R1 extends import("./types").RecordTypeSchema, R2 extends import("./types").RecordTypeSchema>(recordDataTypeA: import("./types").RecordOf<R1>, recordDataTypeB: import("./types").RecordOf<R2>) => import("./utilities/types").SumRecord<R1, R2>; | ||
Exclude: <U extends import("./types").OneOf<any[]>, E extends "string" | "number" | "boolean" | "symbol" | "undefined" | "function" | "unknown" | "null">(union: U, ...excludeTypes: E[]) => import("./utilities/types").ExcludeOneOf<U, E>; | ||
Omit: <R_1 extends import("./types").RecordTypeSchema, K extends keyof R_1>(recordDataType: import("./types").RecordOf<R_1>, ...omitKeys: readonly K[]) => import("./utilities/types").OmitRecord<R_1, K>; | ||
Partial: <R_2 extends import("./types").RecordTypeSchema>(recordDataType: import("./types").RecordOf<R_2>) => import("./utilities/types").PartialRecord<R_2>; | ||
Pick: <R_3 extends import("./types").RecordTypeSchema, K_1 extends keyof R_3>(recordDataType: import("./types").RecordOf<R_3>, ...pickKeys: readonly K_1[]) => import("./utilities/types").PickRecord<R_3, K_1>; | ||
Required: <R_4 extends import("./types").RecordTypeSchema>(recordDataType: import("./types").RecordOf<R_4>) => import("./utilities/types").RequiredRecord<R_4>; | ||
DataType: { | ||
@@ -30,4 +36,5 @@ RecordOf<TS extends import("./types").RecordTypeSchema>(args: TS): import("./types").RecordOf<TS>; | ||
export { ensureDataType } from "./checker-machine/ensure-data-type"; | ||
export { DataType } from "./schame-construction-helpers"; | ||
export { DataType } from "./type-def-helpers"; | ||
export { GetDataType } from "./type-utils"; | ||
export { AllDataTypes, ArrayOf, BasicDataType, ComplexDataType, Enum, EnumMember, FieldDescriptor, Literal, OneOf, RecordOf, RecordTypeSchema as TypeSchema, SetOf, } from "./types"; | ||
export * from "./utilities"; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -7,3 +21,4 @@ exports.DataType = exports.ensureDataType = exports.createValidatedFunction = exports.createTypeGuardedFunction = exports.createValidator = exports.createChecker = void 0; | ||
const ensure_data_type_1 = require("./checker-machine/ensure-data-type"); | ||
const schame_construction_helpers_1 = require("./schame-construction-helpers"); | ||
const type_def_helpers_1 = require("./type-def-helpers"); | ||
const utilities_1 = require("./utilities"); | ||
exports.default = { | ||
@@ -15,3 +30,9 @@ createChecker: create_checker_1.createChecker, | ||
ensureDataType: ensure_data_type_1.ensureDataType, | ||
DataType: schame_construction_helpers_1.DataType, | ||
And: utilities_1.And, | ||
Exclude: utilities_1.Exclude, | ||
Omit: utilities_1.Omit, | ||
Partial: utilities_1.Partial, | ||
Pick: utilities_1.Pick, | ||
Required: utilities_1.Required, | ||
DataType: type_def_helpers_1.DataType, | ||
}; | ||
@@ -26,3 +47,4 @@ var create_checker_2 = require("./checker-machine/create-checker"); | ||
Object.defineProperty(exports, "ensureDataType", { enumerable: true, get: function () { return ensure_data_type_2.ensureDataType; } }); | ||
var schame_construction_helpers_2 = require("./schame-construction-helpers"); | ||
Object.defineProperty(exports, "DataType", { enumerable: true, get: function () { return schame_construction_helpers_2.DataType; } }); | ||
var type_def_helpers_2 = require("./type-def-helpers"); | ||
Object.defineProperty(exports, "DataType", { enumerable: true, get: function () { return type_def_helpers_2.DataType; } }); | ||
__exportStar(require("./utilities"), exports); |
import type { AllDataTypes } from "."; | ||
import type { ArrayOf, BasicDataType, ComplexDataType, Enum, EnumMember, Literal, OneOf, RecordOf, RecordTypeSchema, SetOf } from "./types"; | ||
import type { dataTypeSymbol } from "./type-def-helpers"; | ||
import type { ArrayOf, BasicDataType, ComplexDataType, Enum, EnumMember, FieldDescriptor, Literal, OneOf, RecordOf, RecordTypeSchema, SetOf } from "./types"; | ||
declare type GetDescriptorType<T extends AllDataTypes | FieldDescriptor> = T extends FieldDescriptor ? T["type"] : T; | ||
declare type IsRequiredDescriptor<T extends AllDataTypes | FieldDescriptor> = T extends FieldDescriptor ? T["required"] : true; | ||
export declare type ValueOf<R extends Record<any, any>> = R extends Record<any, infer T> ? T : never; | ||
@@ -10,8 +13,8 @@ export declare type UnknownFunction = (...args: unknown[]) => unknown; | ||
export declare type ExcludeRequired<S extends RecordTypeSchema> = EnsureStringType<Exclude<ValueOf<{ | ||
[K in keyof S]: S[K]["required"] extends false ? K : undefined; | ||
[K in keyof S]: IsRequiredDescriptor<S[K]> extends false ? K : undefined; | ||
}>, undefined>>; | ||
export declare type ExcludeOptional<S extends RecordTypeSchema> = EnsureStringType<Exclude<ValueOf<{ | ||
[K in keyof S]: S[K]["required"] extends false ? undefined : K; | ||
[K in keyof S]: IsRequiredDescriptor<S[K]> extends false ? undefined : K; | ||
}>, undefined>>; | ||
export declare type EnsureIsKey<K> = K extends "arrayOf" | "recordOf" | "setOf" | "oneOf" | "literal" | "enumInstance" | "enumMember" ? K : "recordOf"; | ||
export declare type EnsureIsKey<K> = K extends "arrayOf" | "recordOf" | "setOf" | "oneOf" | "literal" | "enumInstance" | "enumMember" ? K : "invalid"; | ||
export declare type GetTypeFromArrayOf<D extends ComplexDataType> = D extends ArrayOf<infer T> ? T[number] : never; | ||
@@ -32,3 +35,4 @@ export declare type GetFieldDescriptorsFromSetOf<D extends ComplexDataType> = D extends SetOf<infer T> ? T[number] : never; | ||
enumMember: GetTypeFromEnumMember<D>; | ||
}[EnsureIsKey<keyof D>]; | ||
invalid: never; | ||
}[EnsureIsKey<Exclude<keyof D, typeof dataTypeSymbol>>]; | ||
export declare type ParseBasicDataType<D extends BasicDataType> = { | ||
@@ -46,6 +50,7 @@ unknown: unknown; | ||
export declare type ParseRecordType<S extends RecordOf> = { | ||
[K in ExcludeRequired<S["recordOf"]>]?: ParseDataType<S["recordOf"][K]["type"]>; | ||
[K in ExcludeRequired<S["recordOf"]>]?: ParseDataType<GetDescriptorType<S["recordOf"][K]>>; | ||
} & { | ||
[K in ExcludeOptional<S["recordOf"]>]: ParseDataType<S["recordOf"][K]["type"]>; | ||
[K in ExcludeOptional<S["recordOf"]>]: ParseDataType<GetDescriptorType<S["recordOf"][K]>>; | ||
}; | ||
export declare type GetDataType<D extends AllDataTypes> = ReWrap<ParseDataType<D>>; | ||
export {}; |
@@ -1,23 +0,30 @@ | ||
import type { BasicDataTypes } from "./schame-construction-helpers"; | ||
import type { BasicDataTypes, dataTypeSymbol } from "./type-def-helpers"; | ||
import type { ValueOf } from "./type-utils"; | ||
export declare type ArrayOf<DT extends AllDataTypes[] = any[]> = { | ||
arrayOf: DT; | ||
readonly [dataTypeSymbol]: true; | ||
readonly arrayOf: DT; | ||
}; | ||
export declare type RecordOf<TS extends RecordTypeSchema = RecordTypeSchema> = { | ||
recordOf: TS; | ||
readonly [dataTypeSymbol]: true; | ||
readonly recordOf: TS; | ||
}; | ||
export declare type SetOf<DT extends AllDataTypes[] = any[]> = { | ||
setOf: DT; | ||
readonly [dataTypeSymbol]: true; | ||
readonly setOf: DT; | ||
}; | ||
export declare type OneOf<DT extends AllDataTypes[] = any[]> = { | ||
oneOf: DT; | ||
readonly [dataTypeSymbol]: true; | ||
readonly oneOf: DT; | ||
}; | ||
export declare type Literal<V extends string | number | boolean = string | number | boolean> = { | ||
literal: V; | ||
readonly [dataTypeSymbol]: true; | ||
readonly literal: V; | ||
}; | ||
export declare type Enum<E = any> = { | ||
enumInstance: E; | ||
readonly [dataTypeSymbol]: true; | ||
readonly enumInstance: E; | ||
}; | ||
export declare type EnumMember<M = any> = { | ||
enumMember: M; | ||
readonly [dataTypeSymbol]: true; | ||
readonly enumMember: M; | ||
}; | ||
@@ -28,5 +35,7 @@ export declare type BasicDataType = ValueOf<typeof BasicDataTypes>; | ||
export declare type FieldDescriptor = { | ||
required?: boolean; | ||
type: AllDataTypes; | ||
readonly required?: boolean; | ||
readonly type: AllDataTypes; | ||
}; | ||
export declare type RecordTypeSchema = Record<string, FieldDescriptor>; | ||
export interface RecordTypeSchema { | ||
readonly [key: string]: FieldDescriptor | AllDataTypes; | ||
} |
{ | ||
"name": "dilswer", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"main": "./dist/index.js", | ||
@@ -22,4 +22,6 @@ "license": "MIT", | ||
"scripts": { | ||
"build": "tsc", | ||
"test": "jest --coverage" | ||
"build": "tsc -p ./tsconfig.build.json", | ||
"test": "jest --coverage", | ||
"tsc": "tsc --noEmit", | ||
"lint": "eslint ." | ||
}, | ||
@@ -30,5 +32,6 @@ "devDependencies": { | ||
"@typescript-eslint/parser": "^5.4.0", | ||
"eslint": "^8.2.0", | ||
"eslint": "^8.13.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"husky": "^7.0.4", | ||
"jest": "^27.3.1", | ||
@@ -38,4 +41,4 @@ "prettier": "^2.4.1", | ||
"ts-jest": "^27.0.7", | ||
"typescript": "^4.5.2" | ||
"typescript": "^4.6.3" | ||
} | ||
} |
178
README.md
@@ -8,2 +8,41 @@ # Dilswer | ||
## Table Of Contents | ||
1. [Quick Start](#quick-start) | ||
1. [Create type definitions](#create-type-definitions) | ||
2. [Create a TypeScript type from a Dilswer definition](#create-a-typescript-type-from-a-dilswer-definition) | ||
3. [Create a validation function](#create-a-validation-function) | ||
4. [Create a function with a validated input](#create-a-function-with-a-validated-input) | ||
2. [Motivation](#motivation) | ||
3. [Main exported functions](#main-exported-functions) | ||
1. [createValidator()](#dilswercreatevalidator) | ||
2. [createChecker()](#dilswercreatechecker) | ||
3. [createTypeGuardedFunction()](#dilswercreatetypeguardedfunction) | ||
4. [createValidatedFunction()](#dilswercreatevalidatedfunction) | ||
5. [ensureDataType()](#dilswerensuredatatype) | ||
6. [DataType](#dilswerdatatype) | ||
4. [Data Types](#data-types) | ||
1. [String](#datatypestring) | ||
2. [Number](#datatypenumber) | ||
3. [Boolean](#datatypeboolean) | ||
4. [Symbol](#datatypesymbol) | ||
5. [Null](#datatypenull) | ||
6. [Undefined](#datatypeundefined) | ||
7. [Function](#datatypefunction) | ||
8. [Unknown](#datatypeunknown) | ||
9. [OneOf](#datatypeoneofdatatypes) | ||
10. [ArrayOf](#datatypearrayofdatatypes) | ||
11. [RecordOf](#datatyperecordofrecordstring-fielddescriptor) | ||
12. [SetOf](#datatypesetofdatatypes) | ||
13. [Literal](#datatypeliteralstring--number--boolean) | ||
14. [Enum](#datatypeenumenum) | ||
15. [EnumMember](#datatypeenummemberenum-member) | ||
5. [Utility Functions](#utility-functions) | ||
1. [And](#and) | ||
1. [Omit](#omit) | ||
1. [Pick](#pick) | ||
1. [Partial](#partial) | ||
1. [Required](#required) | ||
1. [Exclude](#exclude) | ||
## Quick Start | ||
@@ -14,2 +53,3 @@ | ||
```ts | ||
// person-type.ts | ||
import { DataType } from "dilswer"; | ||
@@ -48,6 +88,6 @@ | ||
```ts | ||
import { createChecker } from "dilswer"; | ||
import { createValidator } from "dilswer"; | ||
import { PersonDataType } from "./person-type.ts"; | ||
const isPerson = createChecker(PersonDataType); | ||
const isPerson = createValidator(PersonDataType); | ||
@@ -78,3 +118,3 @@ // Result: | ||
```ts | ||
import { createChecker } from "dilswer"; | ||
import { createValidator } from "dilswer"; | ||
import { PersonDataType } from "./person-type.ts"; | ||
@@ -130,3 +170,3 @@ | ||
## Dilswer Exports | ||
## Main exported functions | ||
@@ -255,3 +295,3 @@ #### dilswer.createValidator() | ||
will match any object which structure matches the key-value pairs of object | ||
properties and FieldDescriptor objects passed to the argument. | ||
properties and FieldDescriptor's passed to the argument. | ||
@@ -262,2 +302,3 @@ Example | ||
const foo = DataType.RecordOf({ | ||
foo: DataType.Boolean, | ||
bar: { type: DataType.String }, | ||
@@ -267,3 +308,3 @@ baz: { type: DataType.Number, required: false }, | ||
type T = GetDataType<typeof foo>; // type T = {bar: string, baz?: number | undefined} | ||
type T = GetDataType<typeof foo>; // type T = {foo: boolean, bar: string, baz?: number | undefined} | ||
``` | ||
@@ -325,5 +366,6 @@ | ||
const validate = createChecker(foo); | ||
const validate = createValidator(foo); | ||
validate(MyEnum.A); // => true | ||
validate(MyEnum.B); // => true | ||
``` | ||
@@ -346,3 +388,3 @@ | ||
const validate = createChecker(foo); | ||
const validate = createValidator(foo); | ||
@@ -353,1 +395,121 @@ validate("VALUE_A"); // => true | ||
``` | ||
### Utility Functions | ||
#### And() | ||
`And()` utility function can combine two Record Type Definitions into one. If | ||
any of the properties between the two combined Type Defs have the same key-name, | ||
the definition of the second one takes priority. | ||
```ts | ||
const typeDefOne = DataType.RecordOf({ | ||
foo: DataType.Number, | ||
bar: DataType.Number, | ||
}); | ||
const typeDefTwo = DataType.RecordOf({ | ||
bar: DataType.ArrayOf(DataType.String), | ||
baz: DataType.Boolean, | ||
}); | ||
const typeDefSum = And(typeDefOne, typeDefTwo); | ||
// typeDefSum = { | ||
// foo: number; | ||
// bar: string[]; | ||
// baz: boolean; | ||
// } | ||
``` | ||
#### Omit() | ||
`Omit()` utility function removes specified keys from a Record Type Definition. | ||
```ts | ||
const typeDefOne = DataType.RecordOf({ | ||
foo: DataType.Number, | ||
bar: DataType.Number, | ||
baz: DataType.Number, | ||
qux: DataType.Number, | ||
}); | ||
const typeDefOmitted = Omit(typeDefOne, "bar", "qux"); | ||
// typeDefOmitted = { | ||
// foo: number; | ||
// baz: number; | ||
// } | ||
``` | ||
#### Pick() | ||
`Pick()` utility function removes all not specified keys from a Record Type | ||
Definition. | ||
```ts | ||
const typeDefOne = DataType.RecordOf({ | ||
foo: DataType.Number, | ||
bar: DataType.Number, | ||
baz: DataType.Number, | ||
qux: DataType.Number, | ||
}); | ||
const typeDefPick = Pick(typeDefOne, "bar", "qux"); | ||
// typeDefPick = { | ||
// bar: number; | ||
// qux: number; | ||
// } | ||
``` | ||
#### Partial() | ||
`Partial()` utility type makes all the Record's Type Definition keys optional. | ||
```ts | ||
const typeDefOne = DataType.RecordOf({ | ||
foo: DataType.Number, | ||
bar: DataType.String, | ||
baz: DataType.ArrayOf(DataType.Number), | ||
}); | ||
const typeDefPartial = Partial(typeDefOne); | ||
// typeDefPartial = { | ||
// foo?: number | undefined; | ||
// bar?: string | undefined; | ||
// baz?: number[] | undefined; | ||
// } | ||
``` | ||
#### Required() | ||
`Required()` utility type makes all the Record's Type Definition keys to be | ||
required (vs optional). | ||
```ts | ||
const typeDefOne = DataType.RecordOf({ | ||
foo: { type: DataType.Number, required: false }, | ||
bar: { type: DataType.String, required: false }, | ||
baz: { type: DataType.ArrayOf(DataType.Number), required: false }, | ||
}); | ||
const typeDefRequired = Required(typeDefOne); | ||
// typeDefRequired = { | ||
// foo: number; | ||
// bar: string; | ||
// baz: number[]; | ||
// } | ||
``` | ||
#### Exclude() | ||
`Exclude()` utility function removes Type Definitions from an Type Def union. | ||
```ts | ||
const typeDefOne = DataType.OneOf( | ||
DataType.String, | ||
DataType.Number, | ||
DataType.Boolean | ||
); | ||
const typeDefExcluded = Exclude(typeDefOne, DataType.Number); | ||
// typeDefExcluded = string | boolean; | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
65567
66
1028
505
12