@plasius/schema 1.2.5 → 1.2.7

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/index.d.cts CHANGED
@@ -8,8 +8,24 @@ interface PII {
8
8
  purpose?: string;
9
9
  }
10
10
 
11
+ interface ValidationIssueInput {
12
+ code: string;
13
+ message: string;
14
+ path?: string;
15
+ }
16
+ interface ValidationIssue {
17
+ path: string;
18
+ code: string;
19
+ message: string;
20
+ }
21
+ type FieldValidatorResult = boolean | string | ValidationIssueInput | ValidationIssueInput[];
22
+ type FieldValidator<T = unknown> = {
23
+ bivarianceHack(value: T): FieldValidatorResult;
24
+ }["bivarianceHack"];
25
+
11
26
  type FieldType$1 = "string" | "number" | "boolean" | "object" | "array" | "ref";
12
27
 
28
+ type FieldExposure = "public" | "internal";
13
29
  declare class FieldBuilder<TExternal = unknown, TInternal = TExternal> {
14
30
  type: FieldType$1;
15
31
  _type: TExternal;
@@ -17,7 +33,7 @@ declare class FieldBuilder<TExternal = unknown, TInternal = TExternal> {
17
33
  isSystem: boolean;
18
34
  isImmutable: boolean;
19
35
  isRequired: boolean;
20
- _validator?: (value: any) => boolean;
36
+ _validator?: FieldValidator<TInternal>;
21
37
  _description: string;
22
38
  _version: string;
23
39
  _default?: TInternal | (() => TInternal);
@@ -35,6 +51,7 @@ declare class FieldBuilder<TExternal = unknown, TInternal = TExternal> {
35
51
  itemType?: FieldBuilder<any>;
36
52
  refType?: string;
37
53
  _pii: PII;
54
+ _exposure: FieldExposure;
38
55
  enumValues?: readonly TInternal[];
39
56
  constructor(type: FieldType$1, options?: {
40
57
  shape?: Record<string, FieldBuilder<any>>;
@@ -43,9 +60,12 @@ declare class FieldBuilder<TExternal = unknown, TInternal = TExternal> {
43
60
  });
44
61
  immutable(): FieldBuilder<TExternal, TInternal>;
45
62
  system(): FieldBuilder<TExternal, TInternal>;
63
+ exposure(exposure: FieldExposure): FieldBuilder<TExternal, TInternal>;
64
+ internal(): FieldBuilder<TExternal, TInternal>;
65
+ public(): FieldBuilder<TExternal, TInternal>;
46
66
  required(): FieldBuilder<TExternal, TInternal>;
47
67
  optional(): FieldBuilder<TExternal, TInternal>;
48
- validator(fn: (value: TInternal) => boolean): FieldBuilder<TExternal, TInternal>;
68
+ validator(fn: FieldValidator<TInternal>): FieldBuilder<TExternal, TInternal>;
49
69
  description(desc: string): FieldBuilder<TExternal, TInternal>;
50
70
  default(value: TInternal | (() => TInternal)): FieldBuilder<TExternal, TInternal>;
51
71
  /**
@@ -136,6 +156,9 @@ interface SchemaOptions {
136
156
  piiEnforcement?: PIIEnforcement;
137
157
  schemaUpgrade?: SchemaUpgradeStep[] | SchemaUpgradeFunction | undefined;
138
158
  }
159
+ interface SerializeOptions {
160
+ includeInternal?: boolean;
161
+ }
139
162
  type SchemaShape = Record<string, FieldBuilder<any>>;
140
163
  interface FieldDefinition<T = unknown> {
141
164
  type: FieldType;
@@ -153,7 +176,8 @@ interface FieldDefinition<T = unknown> {
153
176
  enum?: string[];
154
177
  _shape?: SchemaShape;
155
178
  pii?: PII;
156
- validator?: (value: any) => boolean;
179
+ exposure?: FieldExposure;
180
+ validator?: FieldValidator<T>;
157
181
  }
158
182
  interface ValidateCompositionOptions {
159
183
  resolveEntity: (type: string, id: string) => Promise<any | null>;
@@ -182,6 +206,7 @@ interface Schema<T extends SchemaShape> {
182
206
  };
183
207
  validate: (input: unknown, existing?: Record<string, any>) => ValidationResult<Infer<T>>;
184
208
  validateComposition: (entity: Infer<T>, options: ValidateCompositionOptions) => Promise<void>;
209
+ serialize: (input: Record<string, any>, options?: SerializeOptions) => Record<string, any>;
185
210
  tableName?: () => string | undefined;
186
211
  prepareForRead(stored: Record<string, any>, decryptFn: (value: string) => any | null): Record<string, any>;
187
212
  prepareForStorage(input: Record<string, any>, encryptFn: (value: any) => string, hashFn: (value: any) => string): Record<string, any>;
@@ -209,6 +234,7 @@ interface Schema<T extends SchemaShape> {
209
234
  enum: string[] | null;
210
235
  refType: string | null;
211
236
  pii: PII | null;
237
+ exposure: FieldExposure;
212
238
  }>;
213
239
  };
214
240
  }
@@ -219,6 +245,7 @@ interface ValidationResult<T> {
219
245
  valid: boolean;
220
246
  value?: DeepReadonly<T>;
221
247
  errors?: string[];
248
+ issues?: ValidationIssue[];
222
249
  }
223
250
  type RefEntityId<T extends string = string> = {
224
251
  type: T;
@@ -302,6 +329,7 @@ declare function renderSchemaDescription(schema: Schema<any>): {
302
329
  description: string;
303
330
  deprecated: boolean;
304
331
  pii?: string;
332
+ exposure: "public" | "internal";
305
333
  }>;
306
334
  };
307
335
 
@@ -624,4 +652,4 @@ declare function isPrivateUseSubtag(value: string): boolean;
624
652
  */
625
653
  declare function validateLanguage(value: unknown): boolean;
626
654
 
627
- export { type DeepReadonly, FieldBuilder, type FieldDefinition, type FieldType, type FieldTypeMap, type Infer, IsoLanguageCode, type PIIEnforcement, type RefEntityId, type Schema, type SchemaOptions, type SchemaShape, type SchemaUpgradeFunction, type SchemaUpgradeResult, type SchemaUpgradeSpec, type SchemaUpgradeStep, type ValidateCompositionOptions, type ValidationResult, createComponentSchema, createSchema, field, getAllComponentSchemas, getAllSchemas, getComponentSchema, getSchemaForType, isExtensionSingleton, isExtensionSubtag, isIsoLanguageCode, isPrivateUseSingleton, isPrivateUseSubtag, isRegionSubtag, isScriptSubtag, isVariantSubtag, isoCountryCodes, isoCurrencyCodes, registerComponentSchema, renderSchemaDescription, validateCountryCode, validateCurrencyCode, validateDateTimeISO, validateEmail, validateLanguage, validateName, validatePercentage, validatePhone, validateRichText, validateSafeText, validateSemVer, validateUUID, validateUrl, validateUserId, validateUserIdArray };
655
+ export { type DeepReadonly, FieldBuilder, type FieldDefinition, type FieldExposure, type FieldType, type FieldTypeMap, type FieldValidator, type FieldValidatorResult, type Infer, IsoLanguageCode, type PIIEnforcement, type RefEntityId, type Schema, type SchemaOptions, type SchemaShape, type SchemaUpgradeFunction, type SchemaUpgradeResult, type SchemaUpgradeSpec, type SchemaUpgradeStep, type SerializeOptions, type ValidateCompositionOptions, type ValidationIssue, type ValidationIssueInput, type ValidationResult, createComponentSchema, createSchema, field, getAllComponentSchemas, getAllSchemas, getComponentSchema, getSchemaForType, isExtensionSingleton, isExtensionSubtag, isIsoLanguageCode, isPrivateUseSingleton, isPrivateUseSubtag, isRegionSubtag, isScriptSubtag, isVariantSubtag, isoCountryCodes, isoCurrencyCodes, registerComponentSchema, renderSchemaDescription, validateCountryCode, validateCurrencyCode, validateDateTimeISO, validateEmail, validateLanguage, validateName, validatePercentage, validatePhone, validateRichText, validateSafeText, validateSemVer, validateUUID, validateUrl, validateUserId, validateUserIdArray };
package/dist/index.d.ts CHANGED
@@ -8,8 +8,24 @@ interface PII {
8
8
  purpose?: string;
9
9
  }
10
10
 
11
+ interface ValidationIssueInput {
12
+ code: string;
13
+ message: string;
14
+ path?: string;
15
+ }
16
+ interface ValidationIssue {
17
+ path: string;
18
+ code: string;
19
+ message: string;
20
+ }
21
+ type FieldValidatorResult = boolean | string | ValidationIssueInput | ValidationIssueInput[];
22
+ type FieldValidator<T = unknown> = {
23
+ bivarianceHack(value: T): FieldValidatorResult;
24
+ }["bivarianceHack"];
25
+
11
26
  type FieldType$1 = "string" | "number" | "boolean" | "object" | "array" | "ref";
12
27
 
28
+ type FieldExposure = "public" | "internal";
13
29
  declare class FieldBuilder<TExternal = unknown, TInternal = TExternal> {
14
30
  type: FieldType$1;
15
31
  _type: TExternal;
@@ -17,7 +33,7 @@ declare class FieldBuilder<TExternal = unknown, TInternal = TExternal> {
17
33
  isSystem: boolean;
18
34
  isImmutable: boolean;
19
35
  isRequired: boolean;
20
- _validator?: (value: any) => boolean;
36
+ _validator?: FieldValidator<TInternal>;
21
37
  _description: string;
22
38
  _version: string;
23
39
  _default?: TInternal | (() => TInternal);
@@ -35,6 +51,7 @@ declare class FieldBuilder<TExternal = unknown, TInternal = TExternal> {
35
51
  itemType?: FieldBuilder<any>;
36
52
  refType?: string;
37
53
  _pii: PII;
54
+ _exposure: FieldExposure;
38
55
  enumValues?: readonly TInternal[];
39
56
  constructor(type: FieldType$1, options?: {
40
57
  shape?: Record<string, FieldBuilder<any>>;
@@ -43,9 +60,12 @@ declare class FieldBuilder<TExternal = unknown, TInternal = TExternal> {
43
60
  });
44
61
  immutable(): FieldBuilder<TExternal, TInternal>;
45
62
  system(): FieldBuilder<TExternal, TInternal>;
63
+ exposure(exposure: FieldExposure): FieldBuilder<TExternal, TInternal>;
64
+ internal(): FieldBuilder<TExternal, TInternal>;
65
+ public(): FieldBuilder<TExternal, TInternal>;
46
66
  required(): FieldBuilder<TExternal, TInternal>;
47
67
  optional(): FieldBuilder<TExternal, TInternal>;
48
- validator(fn: (value: TInternal) => boolean): FieldBuilder<TExternal, TInternal>;
68
+ validator(fn: FieldValidator<TInternal>): FieldBuilder<TExternal, TInternal>;
49
69
  description(desc: string): FieldBuilder<TExternal, TInternal>;
50
70
  default(value: TInternal | (() => TInternal)): FieldBuilder<TExternal, TInternal>;
51
71
  /**
@@ -136,6 +156,9 @@ interface SchemaOptions {
136
156
  piiEnforcement?: PIIEnforcement;
137
157
  schemaUpgrade?: SchemaUpgradeStep[] | SchemaUpgradeFunction | undefined;
138
158
  }
159
+ interface SerializeOptions {
160
+ includeInternal?: boolean;
161
+ }
139
162
  type SchemaShape = Record<string, FieldBuilder<any>>;
140
163
  interface FieldDefinition<T = unknown> {
141
164
  type: FieldType;
@@ -153,7 +176,8 @@ interface FieldDefinition<T = unknown> {
153
176
  enum?: string[];
154
177
  _shape?: SchemaShape;
155
178
  pii?: PII;
156
- validator?: (value: any) => boolean;
179
+ exposure?: FieldExposure;
180
+ validator?: FieldValidator<T>;
157
181
  }
158
182
  interface ValidateCompositionOptions {
159
183
  resolveEntity: (type: string, id: string) => Promise<any | null>;
@@ -182,6 +206,7 @@ interface Schema<T extends SchemaShape> {
182
206
  };
183
207
  validate: (input: unknown, existing?: Record<string, any>) => ValidationResult<Infer<T>>;
184
208
  validateComposition: (entity: Infer<T>, options: ValidateCompositionOptions) => Promise<void>;
209
+ serialize: (input: Record<string, any>, options?: SerializeOptions) => Record<string, any>;
185
210
  tableName?: () => string | undefined;
186
211
  prepareForRead(stored: Record<string, any>, decryptFn: (value: string) => any | null): Record<string, any>;
187
212
  prepareForStorage(input: Record<string, any>, encryptFn: (value: any) => string, hashFn: (value: any) => string): Record<string, any>;
@@ -209,6 +234,7 @@ interface Schema<T extends SchemaShape> {
209
234
  enum: string[] | null;
210
235
  refType: string | null;
211
236
  pii: PII | null;
237
+ exposure: FieldExposure;
212
238
  }>;
213
239
  };
214
240
  }
@@ -219,6 +245,7 @@ interface ValidationResult<T> {
219
245
  valid: boolean;
220
246
  value?: DeepReadonly<T>;
221
247
  errors?: string[];
248
+ issues?: ValidationIssue[];
222
249
  }
223
250
  type RefEntityId<T extends string = string> = {
224
251
  type: T;
@@ -302,6 +329,7 @@ declare function renderSchemaDescription(schema: Schema<any>): {
302
329
  description: string;
303
330
  deprecated: boolean;
304
331
  pii?: string;
332
+ exposure: "public" | "internal";
305
333
  }>;
306
334
  };
307
335
 
@@ -624,4 +652,4 @@ declare function isPrivateUseSubtag(value: string): boolean;
624
652
  */
625
653
  declare function validateLanguage(value: unknown): boolean;
626
654
 
627
- export { type DeepReadonly, FieldBuilder, type FieldDefinition, type FieldType, type FieldTypeMap, type Infer, IsoLanguageCode, type PIIEnforcement, type RefEntityId, type Schema, type SchemaOptions, type SchemaShape, type SchemaUpgradeFunction, type SchemaUpgradeResult, type SchemaUpgradeSpec, type SchemaUpgradeStep, type ValidateCompositionOptions, type ValidationResult, createComponentSchema, createSchema, field, getAllComponentSchemas, getAllSchemas, getComponentSchema, getSchemaForType, isExtensionSingleton, isExtensionSubtag, isIsoLanguageCode, isPrivateUseSingleton, isPrivateUseSubtag, isRegionSubtag, isScriptSubtag, isVariantSubtag, isoCountryCodes, isoCurrencyCodes, registerComponentSchema, renderSchemaDescription, validateCountryCode, validateCurrencyCode, validateDateTimeISO, validateEmail, validateLanguage, validateName, validatePercentage, validatePhone, validateRichText, validateSafeText, validateSemVer, validateUUID, validateUrl, validateUserId, validateUserIdArray };
655
+ export { type DeepReadonly, FieldBuilder, type FieldDefinition, type FieldExposure, type FieldType, type FieldTypeMap, type FieldValidator, type FieldValidatorResult, type Infer, IsoLanguageCode, type PIIEnforcement, type RefEntityId, type Schema, type SchemaOptions, type SchemaShape, type SchemaUpgradeFunction, type SchemaUpgradeResult, type SchemaUpgradeSpec, type SchemaUpgradeStep, type SerializeOptions, type ValidateCompositionOptions, type ValidationIssue, type ValidationIssueInput, type ValidationResult, createComponentSchema, createSchema, field, getAllComponentSchemas, getAllSchemas, getComponentSchema, getSchemaForType, isExtensionSingleton, isExtensionSubtag, isIsoLanguageCode, isPrivateUseSingleton, isPrivateUseSubtag, isRegionSubtag, isScriptSubtag, isVariantSubtag, isoCountryCodes, isoCurrencyCodes, registerComponentSchema, renderSchemaDescription, validateCountryCode, validateCurrencyCode, validateDateTimeISO, validateEmail, validateLanguage, validateName, validatePercentage, validatePhone, validateRichText, validateSafeText, validateSemVer, validateUUID, validateUrl, validateUserId, validateUserIdArray };