index.d.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*!
  2. * TSBuffer Validator v2.1.2
  3. * -----------------------------------------
  4. * MIT LICENSE
  5. * KingWorks (C) Copyright 2023
  6. * https://github.com/k8w/tsbuffer-validator
  7. */
  8. import { InterfaceReference } from 'tsbuffer-schema';
  9. import { InterfaceTypeSchema } from 'tsbuffer-schema';
  10. import { IntersectionTypeSchema } from 'tsbuffer-schema';
  11. import { OmitTypeSchema } from 'tsbuffer-schema';
  12. import { OverwriteTypeSchema } from 'tsbuffer-schema';
  13. import { PartialTypeSchema } from 'tsbuffer-schema';
  14. import { PickTypeSchema } from 'tsbuffer-schema';
  15. import { TSBufferProto } from 'tsbuffer-schema';
  16. import { TSBufferSchema } from 'tsbuffer-schema';
  17. import { TypeReference } from 'tsbuffer-schema';
  18. import { UnionTypeSchema } from 'tsbuffer-schema';
  19. /** @public */
  20. export declare interface FlatInterfaceTypeSchema {
  21. type: InterfaceTypeSchema['type'];
  22. properties: NonNullable<InterfaceTypeSchema['properties']>;
  23. indexSignature?: InterfaceTypeSchema['indexSignature'];
  24. }
  25. export declare class ProtoHelper {
  26. readonly proto: TSBufferProto;
  27. constructor(proto: TSBufferProto);
  28. /** 将ReferenceTypeSchema层层转换为它最终实际引用的类型 */
  29. parseReference(schema: TSBufferSchema): Exclude<TSBufferSchema, TypeReference>;
  30. isInterface(schema: TSBufferSchema, excludeReference?: boolean): schema is InterfaceTypeSchema | InterfaceReference;
  31. isMappedType(schema: TSBufferSchema): schema is Exclude<InterfaceReference, TypeReference>;
  32. isTypeReference(schema: TSBufferSchema): schema is TypeReference;
  33. getUnionProperties(schema: UnionTypeSchema | IntersectionTypeSchema): string[];
  34. /**
  35. * unionProperties: 在Union或Intersection类型中,出现在任意member中的字段
  36. */
  37. /**
  38. * 将unionProperties 扩展到 InterfaceTypeSchema中(optional的any类型)
  39. * 以此来跳过对它们的检查(用于Intersection/Union)
  40. */
  41. applyUnionProperties(schema: FlatInterfaceTypeSchema, unionProperties: string[]): FlatInterfaceTypeSchema;
  42. /**
  43. * 将interface及其引用转换为展平的schema
  44. */
  45. getFlatInterfaceSchema(schema: InterfaceTypeSchema | InterfaceReference): FlatInterfaceTypeSchema;
  46. /**
  47. * 展平interface
  48. */
  49. /** 将MappedTypeSchema转换为展平的Interface
  50. */
  51. parseMappedType(schema: PickTypeSchema | OmitTypeSchema | PartialTypeSchema | OverwriteTypeSchema): InterfaceTypeSchema | UnionTypeSchema | IntersectionTypeSchema;
  52. }
  53. /** @public */
  54. export declare type PruneOutput<T> = {
  55. isSucc: true;
  56. pruneOutput: T;
  57. errMsg?: undefined;
  58. } | {
  59. isSucc: false;
  60. errMsg: string;
  61. pruneOutput?: undefined;
  62. };
  63. /**
  64. * TSBuffer Schema Validator
  65. * @public
  66. */
  67. export declare class TSBufferValidator<Proto extends TSBufferProto = TSBufferProto> {
  68. /**
  69. * Default options
  70. */
  71. options: TSBufferValidatorOptions;
  72. proto: Proto;
  73. readonly protoHelper: ProtoHelper;
  74. constructor(proto: Proto, options?: Partial<TSBufferValidatorOptions>);
  75. /**
  76. * Validate whether the value is valid to the schema
  77. * @param value - Value to be validated.
  78. * @param schemaId - Schema or schema ID.
  79. * For example, the schema ID for type `Test` in `a/b.ts` may be `a/b/Test`.
  80. */
  81. validate(value: any, schemaOrId: keyof Proto | TSBufferSchema, options?: Partial<ValidateOptions>): ValidateOutput;
  82. /**
  83. * 修剪 Object,移除 Schema 中未定义的 Key
  84. * 需要确保 value 类型合法
  85. * @param value - value to be validated
  86. * @param schemaOrId -Schema or schema ID.
  87. * @returns Validate result and pruned value. if validate failed, `pruneOutput` would be undefined.
  88. */
  89. prune<T>(value: T, schemaOrId: string | TSBufferSchema, options?: Partial<ValidateOptions>): PruneOutput<T>;
  90. }
  91. /** @public */
  92. export declare interface TSBufferValidatorOptions {
  93. /**
  94. * 检查interface中是否包含Schema之外的字段
  95. *
  96. * 例1:
  97. * ```
  98. * type AB = { a: string, b: string };
  99. * let ab: AB = { a: 'x', b: 'x', c: 'x' }
  100. * ```
  101. * 字段 `c` 为 excess property,当 `excessPropertyChecks` 启用时将会报错。
  102. *
  103. * 例2:
  104. * ```
  105. * type AB = { a: string} | { b: string };
  106. * let ab: AB = { a: 'x', b: 'x', c: 'x' }
  107. * ```
  108. * 字段 `c` 为 excess property,当 `excessPropertyChecks` 启用时将会报错。
  109. *
  110. * 默认:`true`
  111. */
  112. excessPropertyChecks: boolean;
  113. /**
  114. * 同 `tsconfig.json` 中的 `strictNullChecks`
  115. * 是否使用严格等于去判定 `undefined` 和 `null`
  116. * @defaultValue false
  117. */
  118. strictNullChecks: boolean;
  119. /**
  120. * Clone the proto, don't change this if you don't know what it is.
  121. * @defaultValue true
  122. */
  123. cloneProto?: boolean;
  124. }
  125. /**
  126. * 单次validate的选项,会向下透传
  127. * @public
  128. */
  129. export declare interface ValidateOptions extends TSBufferValidatorOptions {
  130. /**
  131. * Common properties from Union/Intersection type
  132. * ( In case of they are treated as excess property and lead to validation error )
  133. */
  134. unionProperties?: string[];
  135. }
  136. /** @public */
  137. export declare type ValidateOutput = {
  138. isSucc: true;
  139. errMsg?: undefined;
  140. } | {
  141. isSucc: false;
  142. errMsg: string;
  143. };
  144. export { }