@oscarpalmer/jhunal 0.14.0 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/helpers.js DELETED
@@ -1,12 +0,0 @@
1
- import { MESSAGE_CONSTRUCTOR } from "./constants.js";
2
- import { isConstructor } from "@oscarpalmer/atoms/is";
3
- function instanceOf(constructor) {
4
- if (!isConstructor(constructor)) throw new TypeError(MESSAGE_CONSTRUCTOR);
5
- return (value) => {
6
- return value instanceof constructor;
7
- };
8
- }
9
- function isSchematic(value) {
10
- return typeof value === "object" && value !== null && "$schematic" in value && value["$schematic"] === true;
11
- }
12
- export { instanceOf, isSchematic };
package/dist/index.js DELETED
@@ -1,4 +0,0 @@
1
- import { instanceOf } from "./helpers.js";
2
- import { SchematicError } from "./models.js";
3
- import { schematic } from "./schematic.js";
4
- export { SchematicError, instanceOf, schematic };
package/dist/models.js DELETED
@@ -1,8 +0,0 @@
1
- import { ERROR_NAME } from "./constants.js";
2
- var SchematicError = class extends Error {
3
- constructor(message) {
4
- super(message);
5
- this.name = ERROR_NAME;
6
- }
7
- };
8
- export { SchematicError };
@@ -1,12 +0,0 @@
1
- function compact(array, strict) {
2
- if (!Array.isArray(array)) return [];
3
- if (strict === true) return array.filter(Boolean);
4
- const { length } = array;
5
- const compacted = [];
6
- for (let index = 0; index < length; index += 1) {
7
- const item = array[index];
8
- if (item != null) compacted.push(item);
9
- }
10
- return compacted;
11
- }
12
- export { compact };
@@ -1,24 +0,0 @@
1
- import { compact } from "./array/compact.js";
2
- /**
3
- * Get the string value from any value
4
- * @param value Original value
5
- * @returns String representation of the value
6
- */
7
- function getString(value) {
8
- if (typeof value === "string") return value;
9
- if (value == null) return "";
10
- if (typeof value === "function") return getString(value());
11
- if (typeof value !== "object") return String(value);
12
- const asString = String(value.valueOf?.() ?? value);
13
- return asString.startsWith("[object ") ? JSON.stringify(value) : asString;
14
- }
15
- /**
16
- * Join an array of values into a string
17
- * @param value Array of values
18
- * @param delimiter Delimiter to use between values
19
- * @returns Joined string
20
- */
21
- function join(value, delimiter) {
22
- return compact(value).map(getString).join(typeof delimiter === "string" ? delimiter : "");
23
- }
24
- export { join };
@@ -1,22 +0,0 @@
1
- export declare const ERROR_NAME = "SchematicError";
2
- export declare const EXPRESSION_PROPERTY: RegExp;
3
- export declare const MESSAGE_CONSTRUCTOR = "Expected a constructor function";
4
- export declare const MESSAGE_SCHEMA_INVALID_EMPTY = "Schema must have at least one property";
5
- export declare const MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED = "'<key>.<property>' property is not allowed for schemas in $type";
6
- export declare const MESSAGE_SCHEMA_INVALID_PROPERTY_REQUIRED = "'<>.$required' property must be a boolean";
7
- export declare const MESSAGE_SCHEMA_INVALID_PROPERTY_TYPE = "'<>' property must be of a valid type";
8
- export declare const MESSAGE_SCHEMA_INVALID_TYPE = "Schema must be an object";
9
- export declare const MESSAGE_VALIDATOR_INVALID_KEY = "Validator '<>' does not exist";
10
- export declare const MESSAGE_VALIDATOR_INVALID_TYPE = "Validators must be an object";
11
- export declare const MESSAGE_VALIDATOR_INVALID_VALUE = "Validator '<>' must be a function or an array of functions";
12
- export declare const PROPERTY_REQUIRED = "$required";
13
- export declare const PROPERTY_TYPE = "$type";
14
- export declare const PROPERTY_VALIDATORS = "$validators";
15
- export declare const SCHEMATIC_NAME = "$schematic";
16
- export declare const TEMPLATE_PATTERN = "<>";
17
- export declare const TEMPLATE_PATTERN_KEY = "<key>";
18
- export declare const TEMPLATE_PATTERN_PROPERTY = "<property>";
19
- export declare const TYPE_OBJECT = "object";
20
- export declare const TYPE_UNDEFINED = "undefined";
21
- export declare const VALIDATABLE_TYPES: Set<keyof import("./models").Values>;
22
- export declare const TYPE_ALL: Set<keyof import("./models").Values>;
@@ -1,4 +0,0 @@
1
- import type { Constructor } from './models';
2
- import type { Schematic } from './schematic';
3
- export declare function instanceOf<Instance>(constructor: Constructor<Instance>): (value: unknown) => value is Instance;
4
- export declare function isSchematic(value: unknown): value is Schematic<never>;
package/types/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export { instanceOf } from './helpers';
2
- export { SchematicError, type Schema, type TypedSchema } from './models';
3
- export { schematic, type Schematic } from './schematic';
package/types/models.d.ts DELETED
@@ -1,158 +0,0 @@
1
- import type { GenericCallback, PlainObject, Simplify } from '@oscarpalmer/atoms/models';
2
- import type { Schematic } from './schematic';
3
- export type Constructor<Instance = any> = new (...args: any[]) => Instance;
4
- type DeduplicateTuple<Value extends unknown[], Seen extends unknown[] = []> = Value extends [
5
- infer Head,
6
- ...infer Tail
7
- ] ? Head extends Seen[number] ? DeduplicateTuple<Tail, Seen> : DeduplicateTuple<Tail, [...Seen, Head]> : Seen;
8
- type ExtractValueNames<Value> = Value extends ValueName ? Value : Value extends (infer Item)[] ? ExtractValueNames<Item> : Value extends readonly (infer Item)[] ? ExtractValueNames<Item> : never;
9
- /**
10
- * Infer the TypeScript type from a schema definition
11
- */
12
- export type Infer<Model extends Schema> = Simplify<{
13
- [Key in InferRequiredKeys<Model>]: InferSchemaEntry<Model[Key]>;
14
- } & {
15
- [Key in InferOptionalKeys<Model>]?: InferSchemaEntry<Model[Key]>;
16
- }>;
17
- type InferOptionalKeys<Model extends Schema> = keyof {
18
- [Key in keyof Model as IsOptionalProperty<Model[Key]> extends true ? Key : never]: never;
19
- };
20
- type InferPropertyType<Value> = Value extends (infer Item)[] ? InferPropertyValue<Item> : InferPropertyValue<Value>;
21
- type InferPropertyValue<Value> = Value extends Constructor<infer Instance> ? Instance : Value extends Schematic<infer Model> ? Model : Value extends ValueName ? Values[Value & ValueName] : Value extends Schema ? Infer<Value> : never;
22
- type InferRequiredKeys<Model extends Schema> = keyof {
23
- [Key in keyof Model as IsOptionalProperty<Model[Key]> extends true ? never : Key]: never;
24
- };
25
- type InferSchemaEntry<Value> = Value extends (infer Item)[] ? InferSchemaEntryValue<Item> : InferSchemaEntryValue<Value>;
26
- type InferSchemaEntryValue<Value> = Value extends Constructor<infer Instance> ? Instance : Value extends Schematic<infer Model> ? Model : Value extends SchemaProperty ? InferPropertyType<Value['$type']> : Value extends NestedSchema ? Infer<Omit<Value, '$required'>> : Value extends ValueName ? Values[Value & ValueName] : Value extends Schema ? Infer<Value> : never;
27
- type IsOptionalProperty<Value> = Value extends SchemaProperty ? Value['$required'] extends false ? true : false : Value extends {
28
- $required?: boolean;
29
- } ? Value extends {
30
- $required: false;
31
- } ? true : false : false;
32
- type LastOfUnion<Value> = UnionToIntersection<Value extends unknown ? () => Value : never> extends () => infer Item ? Item : never;
33
- type MapToValueTypes<Value extends unknown[]> = Value extends [infer Head, ...infer Tail] ? [ToValueType<Head>, ...MapToValueTypes<Tail>] : [];
34
- type MapToSchemaPropertyTypes<Value extends unknown[]> = Value extends [infer Head, ...infer Tail] ? [ToSchemaPropertyTypeEach<Head>, ...MapToSchemaPropertyTypes<Tail>] : [];
35
- /**
36
- * A nested schema with optional requirement flag
37
- */
38
- export type NestedSchema = {
39
- $required?: boolean;
40
- [key: string]: any;
41
- };
42
- type OptionalKeys<Value> = {
43
- [Key in keyof Value]-?: {} extends Pick<Value, Key> ? Key : never;
44
- }[keyof Value];
45
- type PlainSchema = {
46
- [key: string]: NestedSchema | SchemaEntry | SchemaEntry[];
47
- };
48
- type PropertyValidators<Value> = {
49
- [Key in ExtractValueNames<Value>]?: ((value: Values[Key]) => boolean) | Array<(value: Values[Key]) => boolean>;
50
- };
51
- type RequiredKeys<Value> = Exclude<keyof Value, OptionalKeys<Value>>;
52
- /**
53
- * A schema for validating objects
54
- */
55
- export type Schema = SchemaIndex;
56
- type SchemaEntry = Constructor | Schema | SchemaProperty | Schematic<unknown> | ValueName | ((value: unknown) => boolean);
57
- interface SchemaIndex {
58
- [key: string]: NestedSchema | SchemaEntry | SchemaEntry[];
59
- }
60
- /**
61
- * A property definition with explicit type(s), optional requirement flag, and optional validators
62
- */
63
- export type SchemaProperty = {
64
- $required?: boolean;
65
- $type: SchemaPropertyType | SchemaPropertyType[];
66
- $validators?: PropertyValidators<SchemaPropertyType | SchemaPropertyType[]>;
67
- };
68
- type SchemaPropertyType = Constructor | PlainSchema | Schematic<unknown> | ValueName | ((value: unknown) => boolean);
69
- export declare class SchematicError extends Error {
70
- constructor(message: string);
71
- }
72
- type ToSchemaPropertyType<Value> = UnwrapSingle<DeduplicateTuple<MapToSchemaPropertyTypes<UnionToTuple<Value>>>>;
73
- type ToSchemaPropertyTypeEach<Value> = Value extends NestedSchema ? Omit<Value, '$required'> : Value extends PlainObject ? TypedSchema<Value> : ToValueType<Value>;
74
- type ToSchemaType<Value> = UnwrapSingle<DeduplicateTuple<MapToValueTypes<UnionToTuple<Value>>>>;
75
- type ToValueType<Value> = Value extends unknown[] ? 'array' : Value extends bigint ? 'bigint' : Value extends boolean ? 'boolean' : Value extends Date ? 'date' : Value extends Schematic<any> ? Value : Value extends Function ? 'function' : Value extends null ? 'null' : Value extends number ? 'number' : Value extends object ? 'object' | ((value: unknown) => value is Value) : Value extends string ? 'string' : Value extends symbol ? 'symbol' : Value extends undefined ? 'undefined' : (value: unknown) => value is Value;
76
- type TuplePermutations<Tuple extends unknown[], Elput extends unknown[] = []> = Tuple['length'] extends 0 ? Elput : {
77
- [Key in keyof Tuple]: TuplePermutations<TupleRemoveAt<Tuple, Key & `${number}`>, [
78
- ...Elput,
79
- Tuple[Key]
80
- ]>;
81
- }[keyof Tuple & `${number}`];
82
- type TupleRemoveAt<Items extends unknown[], Item extends string, Prefix extends unknown[] = []> = Items extends [infer Head, ...infer Tail] ? `${Prefix['length']}` extends Item ? [...Prefix, ...Tail] : TupleRemoveAt<Tail, Item, [...Prefix, Head]> : Prefix;
83
- export type TypedPropertyOptional<Value> = {
84
- /**
85
- * The property is not required
86
- */
87
- $required: false;
88
- /**
89
- * The type(s) of the property
90
- */
91
- $type: ToSchemaPropertyType<Exclude<Value, undefined>>;
92
- /**
93
- * Custom validators for the property and its types
94
- */
95
- $validators?: PropertyValidators<ToSchemaPropertyType<Exclude<Value, undefined>>>;
96
- };
97
- export type TypedPropertyRequired<Value> = {
98
- /**
99
- * The property is required _(defaults to `true`)_
100
- */
101
- $required?: true;
102
- /**
103
- * The type(s) of the property
104
- */
105
- $type: ToSchemaPropertyType<Value>;
106
- /**
107
- * Custom validators for the property and its types
108
- */
109
- $validators?: PropertyValidators<ToSchemaPropertyType<Value>>;
110
- };
111
- /**
112
- * Create a schema type constrained to match a TypeScript type
113
- */
114
- export type TypedSchema<Model extends PlainObject> = Simplify<{
115
- [Key in RequiredKeys<Model>]: Model[Key] extends PlainObject ? TypedSchemaRequired<Model[Key]> | Schematic<Model[Key]> : ToSchemaType<Model[Key]> | TypedPropertyRequired<Model[Key]>;
116
- } & {
117
- [Key in OptionalKeys<Model>]: Exclude<Model[Key], undefined> extends PlainObject ? TypedSchemaOptional<Exclude<Model[Key], undefined>> | Schematic<Exclude<Model[Key], undefined>> : TypedPropertyOptional<Model[Key]>;
118
- }>;
119
- type TypedSchemaOptional<Model extends PlainObject> = {
120
- $required: false;
121
- } & TypedSchema<Model>;
122
- type TypedSchemaRequired<Model extends PlainObject> = {
123
- $required?: true;
124
- } & TypedSchema<Model>;
125
- type UnionToIntersection<Value> = (Value extends unknown ? (value: Value) => void : never) extends (value: infer Item) => void ? Item : never;
126
- type UnionToTuple<Value, Items extends unknown[] = []> = [Value] extends [never] ? Items : UnionToTuple<Exclude<Value, LastOfUnion<Value>>, [LastOfUnion<Value>, ...Items]>;
127
- type UnwrapSingle<Value extends unknown[]> = Value extends [infer Only] ? Only : Value['length'] extends 1 | 2 | 3 | 4 | 5 ? TuplePermutations<Value> : Value;
128
- export type ValidatedProperty = {
129
- key: string;
130
- required: boolean;
131
- types: ValidatedPropertyType[];
132
- validators: ValidatedPropertyValidators;
133
- };
134
- export type ValidatedPropertyType = GenericCallback | Schematic<unknown> | ValidatedProperty | ValueName;
135
- export type ValidatedPropertyValidators = {
136
- [Key in ValueName]?: Array<(value: unknown) => boolean>;
137
- };
138
- /**
139
- * Valid type name strings
140
- */
141
- export type ValueName = keyof Values;
142
- /**
143
- * Map of type names to their TypeScript/validatable equivalents
144
- */
145
- export type Values = {
146
- array: unknown[];
147
- bigint: bigint;
148
- boolean: boolean;
149
- date: Date;
150
- function: Function;
151
- null: null;
152
- number: number;
153
- object: object;
154
- string: string;
155
- symbol: symbol;
156
- undefined: undefined;
157
- };
158
- export {};
@@ -1,22 +0,0 @@
1
- import type { PlainObject } from '@oscarpalmer/atoms/models';
2
- import { type Infer, type Schema, type TypedSchema, type ValidatedProperty } from './models';
3
- /**
4
- * A schematic for validating objects
5
- */
6
- export declare class Schematic<Model> {
7
- #private;
8
- private readonly $schematic;
9
- constructor(properties: ValidatedProperty[]);
10
- /**
11
- * Does the value match the schema?
12
- */
13
- is(value: unknown): value is Model;
14
- }
15
- /**
16
- * Create a schematic from a schema
17
- */
18
- export declare function schematic<Model extends Schema>(schema: Model): Schematic<Infer<Model>>;
19
- /**
20
- * Create a schematic from a typed schema
21
- */
22
- export declare function schematic<Model extends PlainObject>(schema: TypedSchema<Model>): Schematic<Model>;
@@ -1,3 +0,0 @@
1
- import type { PlainObject } from '@oscarpalmer/atoms/models';
2
- import { type ValidatedProperty } from '../models';
3
- export declare function getProperties(original: PlainObject, prefix?: string, fromType?: boolean): ValidatedProperty[];
@@ -1,2 +0,0 @@
1
- import type { ValidatedProperty } from '../models';
2
- export declare function validateObject(obj: unknown, properties: ValidatedProperty[]): boolean;