tsbuffer-validator
Advanced tools
Comparing version 2.1.0-dev.2 to 2.1.0-dev.3
/*! | ||
* TSBuffer Validator v2.1.0-dev.2 | ||
* TSBuffer Validator v2.1.0-dev.3 | ||
* ----------------------------------------- | ||
@@ -33,2 +33,3 @@ * MIT LICENSE | ||
isInterface(schema: TSBufferSchema, excludeReference?: boolean): schema is InterfaceTypeSchema | InterfaceReference; | ||
isMappedType(schema: TSBufferSchema): schema is Exclude<InterfaceReference, TypeReference>; | ||
isTypeReference(schema: TSBufferSchema): schema is TypeReference; | ||
@@ -53,3 +54,3 @@ getUnionProperties(schema: UnionTypeSchema | IntersectionTypeSchema): string[]; | ||
*/ | ||
parseMappedType(schema: PickTypeSchema | OmitTypeSchema | PartialTypeSchema | OverwriteTypeSchema): InterfaceTypeSchema | UnionTypeSchema; | ||
parseMappedType(schema: PickTypeSchema | OmitTypeSchema | PartialTypeSchema | OverwriteTypeSchema): InterfaceTypeSchema | UnionTypeSchema | IntersectionTypeSchema; | ||
} | ||
@@ -56,0 +57,0 @@ |
41
index.js
/*! | ||
* TSBuffer Validator v2.1.0-dev.2 | ||
* TSBuffer Validator v2.1.0-dev.3 | ||
* ----------------------------------------- | ||
@@ -103,9 +103,11 @@ * MIT LICENSE | ||
else { | ||
return schema.type === tsbufferSchema.SchemaType.Interface || | ||
schema.type === tsbufferSchema.SchemaType.Pick || | ||
schema.type === tsbufferSchema.SchemaType.Partial || | ||
schema.type === tsbufferSchema.SchemaType.Omit || | ||
schema.type === tsbufferSchema.SchemaType.Overwrite; | ||
return schema.type === tsbufferSchema.SchemaType.Interface || this.isMappedType(schema) && this.parseMappedType(schema).type === tsbufferSchema.SchemaType.Interface; | ||
} | ||
}; | ||
ProtoHelper.prototype.isMappedType = function (schema) { | ||
return schema.type === tsbufferSchema.SchemaType.Pick || | ||
schema.type === tsbufferSchema.SchemaType.Partial || | ||
schema.type === tsbufferSchema.SchemaType.Omit || | ||
schema.type === tsbufferSchema.SchemaType.Overwrite; | ||
}; | ||
ProtoHelper.prototype.isTypeReference = function (schema) { | ||
@@ -149,2 +151,5 @@ return schema.type === tsbufferSchema.SchemaType.Reference || schema.type === tsbufferSchema.SchemaType.IndexedAccess; | ||
} | ||
else if (this.isMappedType(schema)) { | ||
this._addUnionProperties(unionProperties, [this.parseMappedType(schema)]); | ||
} | ||
} | ||
@@ -208,5 +213,9 @@ return unionProperties; | ||
} | ||
else { | ||
else if (this.isMappedType(schema)) { | ||
this._flatInterfaceSchemaCache[uuid] = this._flattenMappedType(schema); | ||
} | ||
else { | ||
// @ts-expect-error | ||
throw new Error('Invalid interface type: ' + schema.type); | ||
} | ||
return this._flatInterfaceSchemaCache[uuid]; | ||
@@ -316,5 +325,2 @@ }; | ||
} | ||
else { | ||
throw new Error("Cannot find pick key [".concat(key, "]")); | ||
} | ||
}; | ||
@@ -367,2 +373,3 @@ for (var _i = 0, _a = schema.keys; _i < _a.length; _i++) { | ||
ProtoHelper.prototype.parseMappedType = function (schema) { | ||
// 解嵌套,例如:Pick<Pick<Omit, XXX, 'a'|'b'>>> | ||
var parents = []; | ||
@@ -373,4 +380,4 @@ var child = schema; | ||
child = this.parseReference(child.target); | ||
} while (child.type === tsbufferSchema.SchemaType.Pick || child.type === tsbufferSchema.SchemaType.Omit || child.type === tsbufferSchema.SchemaType.Partial || child.type === tsbufferSchema.SchemaType.Overwrite); | ||
// Final | ||
} while (this.isMappedType(child)); | ||
// 最内层是 interface,直接返回(validator 会验证 key 匹配) | ||
if (child.type === tsbufferSchema.SchemaType.Interface) { | ||
@@ -380,5 +387,5 @@ return child; | ||
// PickOmit<A|B> === PickOmit<A> | PickOmit<B> | ||
else if (child.type === tsbufferSchema.SchemaType.Union) { | ||
else if (child.type === tsbufferSchema.SchemaType.Union || child.type === tsbufferSchema.SchemaType.Intersection) { | ||
var newSchema = { | ||
type: tsbufferSchema.SchemaType.Union, | ||
type: child.type, | ||
members: child.members.map(function (v) { | ||
@@ -851,3 +858,7 @@ // 从里面往外装 | ||
} | ||
throw new Error(); | ||
else if (parsed.type === tsbufferSchema.SchemaType.Intersection) { | ||
return this._validateIntersectionType(value, parsed, options); | ||
} | ||
// @ts-expect-error | ||
throw new Error("Invalid ".concat(schema.type, " target type: ").concat(parsed.type)); | ||
}; | ||
@@ -854,0 +865,0 @@ TSBufferValidator.prototype._validateFlatInterface = function (value, schema, options) { |
{ | ||
"name": "tsbuffer-validator", | ||
"version": "2.1.0-dev.2", | ||
"version": "2.1.0-dev.3", | ||
"description": "Validator for TSBuffer values", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
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
108950
2312