@travetto/schema
Advanced tools
Comparing version 4.1.1 to 5.0.0-rc.0
{ | ||
"name": "@travetto/schema", | ||
"version": "4.1.1", | ||
"version": "5.0.0-rc.0", | ||
"description": "Data type registry for runtime validation, reflection and binding.", | ||
@@ -30,6 +30,6 @@ "keywords": [ | ||
"dependencies": { | ||
"@travetto/registry": "^4.1.1" | ||
"@travetto/registry": "^5.0.0-rc.0" | ||
}, | ||
"peerDependencies": { | ||
"@travetto/transformer": "^4.1.1" | ||
"@travetto/transformer": "^5.0.0-rc.0" | ||
}, | ||
@@ -36,0 +36,0 @@ "peerDependenciesMeta": { |
@@ -382,3 +382,3 @@ <!-- This file was generated by @travetto/doc and should not be modified directly --> | ||
## Data Utilities | ||
Data utilities for binding values, and type conversion. Currently [DataUtil](https://github.com/travetto/travetto/tree/main/module/schema/src/data.ts#L8) includes: | ||
Data utilities for binding values, and type conversion. Currently [DataUtil](https://github.com/travetto/travetto/tree/main/module/schema/src/data.ts#L10) includes: | ||
* `deepAssign(a, b, mode ?)` which allows for deep assignment of `b` onto `a`, the `mode` determines how aggressive the assignment is, and how flexible it is. `mode` can have any of the following values: | ||
@@ -385,0 +385,0 @@ * `loose`, which is the default is the most lenient. It will not error out, and overwrites will always happen |
@@ -1,2 +0,2 @@ | ||
import { Class, ConcreteClass, TypedObject, ObjectUtil } from '@travetto/base'; | ||
import { Class, ConcreteClass, TypedObject } from '@travetto/base'; | ||
@@ -53,3 +53,3 @@ import { DataUtil } from './data'; | ||
const objK = obj[k]; | ||
const val = ObjectUtil.isPlainObject(objK) ? this.expandPaths(objK) : objK; | ||
const val = DataUtil.isPlainObject(objK) ? this.expandPaths(objK) : objK; | ||
const parts = k.split('.'); | ||
@@ -81,3 +81,3 @@ const last = parts.pop()!; | ||
if (!arr) { | ||
if (sub[last] && ObjectUtil.isPlainObject(val)) { | ||
if (sub[last] && DataUtil.isPlainObject(val)) { | ||
sub[last] = DataUtil.deepAssign(sub[last], val, 'coerce'); | ||
@@ -100,3 +100,3 @@ } else { | ||
} | ||
if (arrSub[key] && ObjectUtil.isPlainObject(val) && ObjectUtil.isPlainObject(arrSub[key])) { | ||
if (arrSub[key] && DataUtil.isPlainObject(val) && DataUtil.isPlainObject(arrSub[key])) { | ||
arrSub[key] = DataUtil.deepAssign(arrSub[key], val, 'coerce'); | ||
@@ -120,3 +120,3 @@ } else { | ||
const pre = `${prefix}${key}`; | ||
if (ObjectUtil.isPlainObject(value)) { | ||
if (DataUtil.isPlainObject(value)) { | ||
Object.assign(out, this.flattenPaths(value, `${pre}.`) | ||
@@ -127,3 +127,3 @@ ); | ||
const v = value[i]; | ||
if (ObjectUtil.isPlainObject(v)) { | ||
if (DataUtil.isPlainObject(v)) { | ||
Object.assign(out, this.flattenPaths(v, `${pre}[${i}].`)); | ||
@@ -187,3 +187,3 @@ } else { | ||
if (!!data && !ObjectUtil.isPrimitive(data)) { | ||
if (!!data && !DataUtil.isPrimitive(data)) { | ||
const conf = SchemaRegistry.get(cons); | ||
@@ -190,0 +190,0 @@ |
@@ -1,3 +0,5 @@ | ||
import { Class, ClassInstance, TypedObject, ObjectUtil } from '@travetto/base'; | ||
import { isNumberObject as isNum, isBooleanObject as isBool, isStringObject as isStr } from 'node:util/types'; | ||
import { Class, ClassInstance, TypedObject } from '@travetto/base'; | ||
const REGEX_PAT = /[\/](.*)[\/](i|g|m|s)?/; | ||
@@ -10,2 +12,33 @@ | ||
/** | ||
* Is a value a plain JS object, created using {} | ||
* @param obj Object to check | ||
*/ | ||
static isPlainObject(obj: unknown): obj is Record<string, unknown> { | ||
return typeof obj === 'object' // separate from primitives | ||
&& obj !== undefined | ||
&& obj !== null // is obvious | ||
&& obj.constructor === Object // separate instances (Array, DOM, ...) | ||
&& Object.prototype.toString.call(obj) === '[object Object]'; // separate build-in like Math | ||
} | ||
/** | ||
* Is a value of primitive type | ||
* @param el Value to check | ||
*/ | ||
static isPrimitive(el: unknown): el is (string | boolean | number | RegExp) { | ||
switch (typeof el) { | ||
case 'string': case 'boolean': case 'number': case 'bigint': return true; | ||
case 'object': return !!el && (el instanceof RegExp || el instanceof Date || isStr(el) || isNum(el) || isBool(el)); | ||
default: return false; | ||
} | ||
} | ||
/** | ||
* Is simple, as a primitive, function or class | ||
*/ | ||
static isSimpleValue(a: unknown): a is Function | Class | string | number | RegExp | Date { | ||
return this.isPrimitive(a) || typeof a === 'function'; | ||
} | ||
static #deepAssignRaw(a: unknown, b: unknown, mode: 'replace' | 'loose' | 'strict' | 'coerce' = 'loose'): unknown { | ||
@@ -16,4 +49,4 @@ const isEmptyA = a === undefined || a === null; | ||
const isArrB = Array.isArray(b); | ||
const isSimpA = !isEmptyA && ObjectUtil.isSimple(a); | ||
const isSimpB = !isEmptyB && ObjectUtil.isSimple(b); | ||
const isSimpA = !isEmptyA && this.isSimpleValue(a); | ||
const isSimpB = !isEmptyB && this.isSimpleValue(b); | ||
@@ -159,3 +192,3 @@ let ret: unknown; | ||
case Object: { | ||
if (!strict || ObjectUtil.isPlainObject(input)) { | ||
if (!strict || this.isPlainObject(input)) { | ||
return input; | ||
@@ -169,3 +202,3 @@ } else { | ||
} | ||
if (!strict || ObjectUtil.isPlainObject(input)) { | ||
if (!strict || this.isPlainObject(input)) { | ||
return input; | ||
@@ -183,3 +216,3 @@ } else { | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
return (Array.isArray(a) ? a.slice(0) : (ObjectUtil.isSimple(a) ? a : { ...(a as {}) })) as T; | ||
return (Array.isArray(a) ? a.slice(0) : (this.isSimpleValue(a) ? a : { ...(a as {}) })) as T; | ||
} | ||
@@ -194,3 +227,3 @@ | ||
static deepAssign<T, U>(a: T, b: U, mode: | 'replace' | 'loose' | 'strict' | 'coerce' = 'loose'): T & U { | ||
if (!a || ObjectUtil.isSimple(a)) { | ||
if (!a || this.isSimpleValue(a)) { | ||
throw new Error(`Cannot merge onto a simple value, ${a}`); | ||
@@ -197,0 +230,0 @@ } |
@@ -414,2 +414,13 @@ import { RuntimeIndex } from '@travetto/manifest'; | ||
if (config.subTypeName && config.subTypeField in config.views[AllViewⲐ].schema) { | ||
const field = config.views[AllViewⲐ].schema[config.subTypeField]; | ||
config.views[AllViewⲐ].schema[config.subTypeField] = { | ||
...field, | ||
enum: { | ||
values: [config.subTypeName], | ||
message: `${config.subTypeField} can only be '${config.subTypeName}'`, | ||
} | ||
}; | ||
} | ||
return config; | ||
@@ -416,0 +427,0 @@ } |
@@ -1,2 +0,3 @@ | ||
import { Primitive, Class } from '@travetto/base'; | ||
import { Class } from '@travetto/base'; | ||
import { Primitive } from '@travetto/schema'; | ||
@@ -3,0 +4,0 @@ import { AllViewⲐ } from '../internal/types'; |
@@ -0,5 +1,7 @@ | ||
export type Primitive = number | bigint | boolean | string | Date; | ||
export type DeepPartial<T> = { | ||
[P in keyof T]?: (T[P] extends (number | string | Date | boolean | undefined) ? (T[P] | undefined) : | ||
[P in keyof T]?: (T[P] extends (Primitive | undefined) ? (T[P] | undefined) : | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(T[P] extends any[] ? (DeepPartial<T[P][number]> | null | undefined)[] : DeepPartial<T[P]>)); | ||
}; |
@@ -1,2 +0,2 @@ | ||
import { Class, ClassInstance, TypedObject, ObjectUtil } from '@travetto/base'; | ||
import { Class, ClassInstance, TypedObject } from '@travetto/base'; | ||
@@ -8,2 +8,3 @@ import { FieldConfig, SchemaConfig } from '../service/types'; | ||
import { isValidationError, TypeMismatchError, ValidationResultError } from './error'; | ||
import { DataUtil } from '../data'; | ||
@@ -272,3 +273,3 @@ /** | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
if (!ObjectUtil.isPlainObject(o) && !(o instanceof cls || cls.Ⲑid === (o as ClassInstance<T>).constructor.Ⲑid)) { | ||
if (!DataUtil.isPlainObject(o) && !(o instanceof cls || cls.Ⲑid === (o as ClassInstance<T>).constructor.Ⲑid)) { | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
@@ -275,0 +276,0 @@ throw new TypeMismatchError(cls.name, (o as ClassInstance).constructor.name); |
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
103996
2338
1
+ Added@travetto/manifest@5.0.9(transitive)
+ Added@travetto/registry@5.0.13(transitive)
+ Added@travetto/runtime@5.0.13(transitive)
+ Added@travetto/transformer@5.0.10(transitive)
+ Added@types/node@22.10.1(transitive)
+ Addedundici-types@6.20.0(transitive)
- Removed@travetto/base@4.1.2(transitive)
- Removed@travetto/manifest@4.1.0(transitive)
- Removed@travetto/registry@4.1.2(transitive)
- Removed@travetto/transformer@4.1.2(transitive)
- Removed@types/node@20.17.9(transitive)
- Removedundici-types@6.19.8(transitive)