@tstdl/base 0.91.0-beta2 → 0.91.0-beta3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.91.0-beta2",
3
+ "version": "0.91.0-beta3",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -1,6 +1,8 @@
1
1
  import type { EmptyObject, Merge } from 'type-fest';
2
2
  import type { JsonPath } from '../../json-path/json-path.js';
3
3
  import type { AbstractConstructor, OneOrMany, PartialProperty, Record, SimplifyObject, Type, TypedOmit } from '../../types.js';
4
+ import { type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
5
+ import type { SchemaPropertyDecorator } from '../decorators/types.js';
4
6
  import { Schema, type SchemaTestable, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
5
7
  export type ObjectSchemaFactoryFunction<T> = (data: T) => T;
6
8
  export type ObjectSchemaFactory<T> = {
@@ -65,11 +67,13 @@ export declare function assign<T1 extends Record, T2 extends Record, T3 extends
65
67
  export declare function assign<T1 extends Record, T2 extends Record, T3 extends Record, T4 extends Record>(a: ObjectSchemaOrType<T1>, b: ObjectSchemaOrType<T2>, c: ObjectSchemaOrType<T3>, d: ObjectSchemaOrType<T4>): ObjectSchema<Merge<Merge<Merge<T1, T2>, T3>, T4>>;
66
68
  export declare function assign<T1 extends Record, T2 extends Record, T3 extends Record, T4 extends Record, T5 extends Record>(a: ObjectSchemaOrType<T1>, b: ObjectSchemaOrType<T2>, c: ObjectSchemaOrType<T3>, d: ObjectSchemaOrType<T4>, e: ObjectSchemaOrType<T5>): ObjectSchema<Merge<Merge<Merge<Merge<T1, T2>, T3>, T4>, T5>>;
67
69
  export declare function assign<T1 extends Record, T2 extends Record, T3 extends Record, T4 extends Record, T5 extends Record, T6 extends Record>(a: ObjectSchemaOrType<T1>, b: ObjectSchemaOrType<T2>, c: ObjectSchemaOrType<T3>, d: ObjectSchemaOrType<T4>, e: ObjectSchemaOrType<T5>, f: ObjectSchemaOrType<T6>): ObjectSchema<Merge<Merge<Merge<Merge<Merge<T1, T2>, T3>, T4>, T5>, T6>>;
68
- export declare function partial<T extends Record>(schema: ObjectSchema<T>): ObjectSchema<Partial<T>>;
69
- export declare function partial<T extends Record, K extends keyof T>(schema: ObjectSchema<T>, keys: OneOrMany<K>): ObjectSchema<PartialProperty<T, K>>;
70
- export declare function pick<T extends Record, K extends keyof T>(schema: ObjectSchema<T>, keys: OneOrMany<K>): ObjectSchema<SimplifyObject<Omit<T, K>>>;
71
- export declare function omit<T extends Record, K extends keyof T>(schema: ObjectSchema<T>, keys: OneOrMany<K>): ObjectSchema<SimplifyObject<Omit<T, K>>>;
70
+ export declare function partial<T extends Record>(schema: ObjectSchemaOrType<T>): ObjectSchema<Partial<T>>;
71
+ export declare function partial<T extends Record, K extends keyof T>(schema: ObjectSchemaOrType<T>, keys: OneOrMany<K>): ObjectSchema<PartialProperty<T, K>>;
72
+ export declare function pick<T extends Record, K extends keyof T>(schemaOrType: ObjectSchemaOrType<T>, keys: OneOrMany<K>): ObjectSchema<SimplifyObject<Omit<T, K>>>;
73
+ export declare function omit<T extends Record, K extends keyof T>(schemaOrType: ObjectSchemaOrType<T>, keys: OneOrMany<K>): ObjectSchema<SimplifyObject<Omit<T, K>>>;
72
74
  export declare function getSchemaFromReflection<T extends Record>(type: AbstractConstructor<T>): Schema<T>;
73
75
  declare function _tryGetSchemaFromReflection<T extends Record>(type: AbstractConstructor<T>): Schema<T> | null;
76
+ export declare function getObjectSchema<T extends Record>(schemaOrType: SchemaTestable<T>): ObjectSchema<T>;
77
+ export declare function Record<K extends PropertyKey, V>(key: Schema<K>, value: Schema<V>, options?: TypedOmit<ObjectSchemaOptions<Record<K, V>>, 'unknownProperties' | 'unknownPropertiesKey'> & SchemaPropertyDecoratorOptions): SchemaPropertyDecorator;
74
78
  export declare const emptyObjectSchema: ObjectSchema<EmptyObject>;
75
79
  export {};
@@ -6,6 +6,7 @@ import { memoizeSingle } from '../../utils/function/memoize.js';
6
6
  import { filterObject, mapObjectValues, objectKeys } from '../../utils/object/object.js';
7
7
  import { assert, isDefined, isFunction, isLiteralObject, isNotNull, isNotNullOrUndefined, isNull, isObject, isUndefined } from '../../utils/type-guards.js';
8
8
  import { typeOf } from '../../utils/type-of.js';
9
+ import { Property } from '../decorators/index.js';
9
10
  import { Schema } from '../schema.js';
10
11
  import { schemaTestableToSchema } from '../testable.js';
11
12
  import { array } from './array.js';
@@ -79,21 +80,15 @@ export function record(key, value, options) {
79
80
  return object({}, { ...options, unknownPropertiesKey: key, unknownProperties: value });
80
81
  }
81
82
  export function assign(...schemasOrTypes) {
82
- const schemas = schemasOrTypes.map((schemaOrType) => {
83
- if (schemaOrType instanceof ObjectSchema) {
84
- return schemaOrType;
85
- }
86
- const typeSchema = getSchemaFromReflection(schemaOrType);
87
- assert(typeSchema instanceof ObjectSchema, 'assign() only works with object schemas.');
88
- return typeSchema;
89
- });
83
+ const schemas = schemasOrTypes.map(getObjectSchema);
90
84
  return object(schemas.reduce((result, schema) => ({ ...result, ...schema.properties }), {}), {
91
85
  mask: schemas.findLast((schema) => isNotNull(schema.mask))?.mask,
92
86
  unknownProperties: schemas.findLast((schema) => isNotNull(schema.unknownProperties))?.unknownProperties,
93
87
  unknownPropertiesKey: schemas.findLast((schema) => isNotNull(schema.unknownPropertiesKey))?.unknownPropertiesKey
94
88
  });
95
89
  }
96
- export function partial(schema, keyOrKeys) {
90
+ export function partial(schemaOrType, keyOrKeys) {
91
+ const schema = getObjectSchema(schemaOrType);
97
92
  const keys = isUndefined(keyOrKeys) ? undefined : toArray(keyOrKeys);
98
93
  const mapper = isUndefined(keys)
99
94
  ? (propertySchema) => optional(propertySchema)
@@ -104,7 +99,8 @@ export function partial(schema, keyOrKeys) {
104
99
  unknownPropertiesKey: schema.unknownPropertiesKey
105
100
  });
106
101
  }
107
- export function pick(schema, keys) {
102
+ export function pick(schemaOrType, keys) {
103
+ const schema = getObjectSchema(schemaOrType);
108
104
  const keyArray = toArray(keys);
109
105
  return object(filterObject(schema.properties, (_, key) => keyArray.includes(key)), {
110
106
  mask: schema.mask,
@@ -112,7 +108,8 @@ export function pick(schema, keys) {
112
108
  unknownPropertiesKey: schema.unknownPropertiesKey
113
109
  });
114
110
  }
115
- export function omit(schema, keys) {
111
+ export function omit(schemaOrType, keys) {
112
+ const schema = getObjectSchema(schemaOrType);
116
113
  const keysArray = toArray(keys);
117
114
  return object(filterObject(schema.properties, (_, key) => !keysArray.includes(key)), {
118
115
  mask: schema.mask,
@@ -174,4 +171,16 @@ function getObjectSchemaPropertiesFromReflection(metadata, type) {
174
171
  }
175
172
  return properties;
176
173
  }
174
+ export function getObjectSchema(schemaOrType) {
175
+ if (schemaOrType instanceof ObjectSchema) {
176
+ return schemaOrType;
177
+ }
178
+ if (isFunction(schemaOrType)) {
179
+ return getObjectSchema(getSchemaFromReflection(schemaOrType));
180
+ }
181
+ throw new Error('Could not infer ObjectSchema.');
182
+ }
183
+ export function Record(key, value, options) {
184
+ return Property(record(key, value, options), options);
185
+ }
177
186
  export const emptyObjectSchema = explicitObject({});