teyit 1.0.1 → 1.0.2

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/README.md CHANGED
@@ -24,6 +24,7 @@
24
24
  <!---->
25
25
 
26
26
  [AnyObject]: ./src/types/AnyObject.type.ts
27
+ [InferSchema]: ./src/types/InferSchema.type.ts
27
28
  [JSONSchema]: ./src/types/JSONSchema.type.ts
28
29
  [Schema]: ./src/types/Schema.type.ts
29
30
  [TeyitOptions]: ./src/types/TeyitOptions.type.ts
@@ -119,6 +120,7 @@ teyit
119
120
  │ └── Username
120
121
 
121
122
  ├── type AnyObject
123
+ ├── type InferSchema
122
124
  ├── type JSONSchema
123
125
  ├── type Schema
124
126
  ├── type TeyitOptions
@@ -161,12 +163,12 @@ Validate the properties with your Teyit schema.
161
163
  > | `schema` | [Schema] | | Teyit schema. |
162
164
  > | `properties` | [AnyObject] | | Properties to be validate. |
163
165
  >
164
- > returns [Promise]<[AnyObject]>
166
+ > returns [Promise]<[InferSchema<Schema>]>
165
167
  >
166
168
  > Example:
167
169
  >
168
170
  > ```typescript
169
- > const schema: Schema = {
171
+ > const schema = {
170
172
  > display_name: {
171
173
  > type: 'string',
172
174
  > max: 32,
@@ -210,7 +212,7 @@ Validate the properties with your Teyit schema.
210
212
  > required: true
211
213
  > }
212
214
  > ]
213
- > };
215
+ > } as const satisfies Schema;
214
216
  >
215
217
  > const properties = {
216
218
  > display_name: 'Fırat',
@@ -350,6 +352,7 @@ Convert your Teyit schema into [JSON Schema](https://json-schema.org).
350
352
  | Type |
351
353
  | ----------------- |
352
354
  | [AnyObject] |
355
+ | [InferSchema] |
353
356
  | [JSONSchema] |
354
357
  | [Schema] |
355
358
  | [TeyitOptions] |
package/dist/main.d.mts CHANGED
@@ -35,7 +35,7 @@ type Boolean = {
35
35
  nullable: boolean;
36
36
  required: boolean;
37
37
  };
38
- type Date = {
38
+ type Date$1 = {
39
39
  type: 'date';
40
40
  min?: string;
41
41
  max?: string;
@@ -59,7 +59,7 @@ type Array = {
59
59
  nullable: boolean;
60
60
  required: boolean;
61
61
  };
62
- type TypeSingle = String | Number | Boolean | Date | Object$1 | Array;
62
+ type TypeSingle = String | Number | Boolean | Date$1 | Object$1 | Array;
63
63
  type TypeUnion = [TypeSingle, TypeSingle, ...TypeSingle[]];
64
64
  type Type = TypeSingle | TypeUnion;
65
65
  type SchemaSingle = Record<string, Type>;
@@ -117,7 +117,7 @@ declare class Teyit {
117
117
  private readonly options;
118
118
  constructor(options?: TeyitOptions);
119
119
  private cleanupTypesDir;
120
- validate(schema: Schema, properties: AnyObject): Promise<AnyObject>;
120
+ validate<const _Schema extends Schema>(schema: _Schema, properties: AnyObject): Promise<InferSchema<_Schema>>;
121
121
  declare(schema: Schema, name: string): Promise<void>;
122
122
  convertToJSONSchema(schema: Schema): JSONSchema;
123
123
  }
@@ -144,6 +144,47 @@ declare namespace Patterns_export {
144
144
  export { Patterns_export_Domain as Domain, Patterns_export_Email as Email, Patterns_export_HTTP as HTTP, Patterns_export_PhoneNumber as PhoneNumber, Patterns_export_URI as URI, Patterns_export_Username as Username };
145
145
  }
146
146
 
147
+ type Prettify<Type> = {
148
+ [Key in keyof Type]: Type[Key];
149
+ } & {};
150
+ type InferBaseType<Type> = Type extends {
151
+ type: 'string';
152
+ } ? string : Type extends {
153
+ type: 'number';
154
+ } ? number : Type extends {
155
+ type: 'boolean';
156
+ } ? boolean : Type extends {
157
+ type: 'date';
158
+ } ? Date : Type extends {
159
+ type: 'object';
160
+ properties: infer Properties;
161
+ } ? InferSchema<Properties> : Type extends {
162
+ type: 'array';
163
+ items: infer Items;
164
+ } ? InferType<Items>[] : never;
165
+ type ApplyNullable<Type, Definition> = Definition extends {
166
+ nullable: true;
167
+ } ? Type | null : Type;
168
+ type InferTypeSingle<Type> = Type extends unknown ? ApplyNullable<InferBaseType<Type>, Type> : never;
169
+ type InferType<Type> = Type extends readonly unknown[] ? InferTypeSingle<Type[number]> : InferTypeSingle<Type>;
170
+ type IsOptional<Type> = Type extends {
171
+ required: false;
172
+ } ? true : Type extends readonly (infer Union)[] ? (Union extends {
173
+ required: false;
174
+ } ? true : false) : false;
175
+ type RequiredKeys<Schema> = {
176
+ [Key in keyof Schema]: IsOptional<Schema[Key]> extends true ? never : Key;
177
+ }[keyof Schema];
178
+ type OptionalKeys<Schema> = {
179
+ [Key in keyof Schema]: IsOptional<Schema[Key]> extends true ? Key : never;
180
+ }[keyof Schema];
181
+ type InferSchemaSingle<Schema> = Prettify<{
182
+ [Key in RequiredKeys<Schema>]: InferType<Schema[Key]>;
183
+ } & {
184
+ [Key in OptionalKeys<Schema>]?: InferType<Schema[Key]>;
185
+ }>;
186
+ type InferSchema<Schema> = Schema extends readonly unknown[] ? InferSchemaSingle<Schema[number]> : InferSchemaSingle<Schema>;
187
+
147
188
  type UnknownObject = Record<string, unknown>;
148
189
 
149
190
  declare class ValidationError extends Error {
@@ -160,4 +201,4 @@ declare class ValidationError extends Error {
160
201
  });
161
202
  }
162
203
 
163
- export { type AnyObject, type JSONSchema, Patterns_export as Patterns, type Schema, Teyit, type TeyitOptions, type UnknownObject, ValidationError };
204
+ export { type AnyObject, type InferSchema, type JSONSchema, Patterns_export as Patterns, type Schema, Teyit, type TeyitOptions, type UnknownObject, ValidationError };
package/dist/main.d.ts CHANGED
@@ -35,7 +35,7 @@ type Boolean = {
35
35
  nullable: boolean;
36
36
  required: boolean;
37
37
  };
38
- type Date = {
38
+ type Date$1 = {
39
39
  type: 'date';
40
40
  min?: string;
41
41
  max?: string;
@@ -59,7 +59,7 @@ type Array = {
59
59
  nullable: boolean;
60
60
  required: boolean;
61
61
  };
62
- type TypeSingle = String | Number | Boolean | Date | Object$1 | Array;
62
+ type TypeSingle = String | Number | Boolean | Date$1 | Object$1 | Array;
63
63
  type TypeUnion = [TypeSingle, TypeSingle, ...TypeSingle[]];
64
64
  type Type = TypeSingle | TypeUnion;
65
65
  type SchemaSingle = Record<string, Type>;
@@ -117,7 +117,7 @@ declare class Teyit {
117
117
  private readonly options;
118
118
  constructor(options?: TeyitOptions);
119
119
  private cleanupTypesDir;
120
- validate(schema: Schema, properties: AnyObject): Promise<AnyObject>;
120
+ validate<const _Schema extends Schema>(schema: _Schema, properties: AnyObject): Promise<InferSchema<_Schema>>;
121
121
  declare(schema: Schema, name: string): Promise<void>;
122
122
  convertToJSONSchema(schema: Schema): JSONSchema;
123
123
  }
@@ -144,6 +144,47 @@ declare namespace Patterns_export {
144
144
  export { Patterns_export_Domain as Domain, Patterns_export_Email as Email, Patterns_export_HTTP as HTTP, Patterns_export_PhoneNumber as PhoneNumber, Patterns_export_URI as URI, Patterns_export_Username as Username };
145
145
  }
146
146
 
147
+ type Prettify<Type> = {
148
+ [Key in keyof Type]: Type[Key];
149
+ } & {};
150
+ type InferBaseType<Type> = Type extends {
151
+ type: 'string';
152
+ } ? string : Type extends {
153
+ type: 'number';
154
+ } ? number : Type extends {
155
+ type: 'boolean';
156
+ } ? boolean : Type extends {
157
+ type: 'date';
158
+ } ? Date : Type extends {
159
+ type: 'object';
160
+ properties: infer Properties;
161
+ } ? InferSchema<Properties> : Type extends {
162
+ type: 'array';
163
+ items: infer Items;
164
+ } ? InferType<Items>[] : never;
165
+ type ApplyNullable<Type, Definition> = Definition extends {
166
+ nullable: true;
167
+ } ? Type | null : Type;
168
+ type InferTypeSingle<Type> = Type extends unknown ? ApplyNullable<InferBaseType<Type>, Type> : never;
169
+ type InferType<Type> = Type extends readonly unknown[] ? InferTypeSingle<Type[number]> : InferTypeSingle<Type>;
170
+ type IsOptional<Type> = Type extends {
171
+ required: false;
172
+ } ? true : Type extends readonly (infer Union)[] ? (Union extends {
173
+ required: false;
174
+ } ? true : false) : false;
175
+ type RequiredKeys<Schema> = {
176
+ [Key in keyof Schema]: IsOptional<Schema[Key]> extends true ? never : Key;
177
+ }[keyof Schema];
178
+ type OptionalKeys<Schema> = {
179
+ [Key in keyof Schema]: IsOptional<Schema[Key]> extends true ? Key : never;
180
+ }[keyof Schema];
181
+ type InferSchemaSingle<Schema> = Prettify<{
182
+ [Key in RequiredKeys<Schema>]: InferType<Schema[Key]>;
183
+ } & {
184
+ [Key in OptionalKeys<Schema>]?: InferType<Schema[Key]>;
185
+ }>;
186
+ type InferSchema<Schema> = Schema extends readonly unknown[] ? InferSchemaSingle<Schema[number]> : InferSchemaSingle<Schema>;
187
+
147
188
  type UnknownObject = Record<string, unknown>;
148
189
 
149
190
  declare class ValidationError extends Error {
@@ -160,4 +201,4 @@ declare class ValidationError extends Error {
160
201
  });
161
202
  }
162
203
 
163
- export { type AnyObject, type JSONSchema, Patterns_export as Patterns, type Schema, Teyit, type TeyitOptions, type UnknownObject, ValidationError };
204
+ export { type AnyObject, type InferSchema, type JSONSchema, Patterns_export as Patterns, type Schema, Teyit, type TeyitOptions, type UnknownObject, ValidationError };
package/dist/main.js CHANGED
@@ -252,11 +252,10 @@ var validate = (schema, properties, options) => {
252
252
  if (equivalent.lowercase === true) this.update(property.toLowerCase());
253
253
  if (equivalent.uppercase === true) this.update(property.toUpperCase());
254
254
  if (equivalent.enum !== void 0 && !equivalent.enum.map((item) => {
255
- let mappedItem = item;
256
- if (equivalent.trim !== false) mappedItem = mappedItem.trim();
257
- if (equivalent.lowercase === true) mappedItem = mappedItem.toLowerCase();
258
- if (equivalent.uppercase === true) mappedItem = mappedItem.toUpperCase();
259
- return mappedItem;
255
+ if (equivalent.trim !== false) item = item.trim();
256
+ if (equivalent.lowercase === true) item = item.toLowerCase();
257
+ if (equivalent.uppercase === true) item = item.toUpperCase();
258
+ return item;
260
259
  }).includes(property))
261
260
  reportError({ message: (options.error_messages?.string?.enum ?? "").replaceAll("{path}", path2), parts: { path: path2 } });
262
261
  if (equivalent.pattern !== void 0 && !new RegExp(equivalent.pattern).test(property)) {
package/dist/main.mjs CHANGED
@@ -220,11 +220,10 @@ var validate = (schema, properties, options) => {
220
220
  if (equivalent.lowercase === true) this.update(property.toLowerCase());
221
221
  if (equivalent.uppercase === true) this.update(property.toUpperCase());
222
222
  if (equivalent.enum !== void 0 && !equivalent.enum.map((item) => {
223
- let mappedItem = item;
224
- if (equivalent.trim !== false) mappedItem = mappedItem.trim();
225
- if (equivalent.lowercase === true) mappedItem = mappedItem.toLowerCase();
226
- if (equivalent.uppercase === true) mappedItem = mappedItem.toUpperCase();
227
- return mappedItem;
223
+ if (equivalent.trim !== false) item = item.trim();
224
+ if (equivalent.lowercase === true) item = item.toLowerCase();
225
+ if (equivalent.uppercase === true) item = item.toUpperCase();
226
+ return item;
228
227
  }).includes(property))
229
228
  reportError({ message: (options.error_messages?.string?.enum ?? "").replaceAll("{path}", path2), parts: { path: path2 } });
230
229
  if (equivalent.pattern !== void 0 && !new RegExp(equivalent.pattern).test(property)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "teyit",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Portable and simple schemas for property validation.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/keift/teyit",