+11
-65
@@ -216,3 +216,2 @@ // src/messages.ts | ||
| var strictName = "object:strict"; | ||
| var fieldsName = "object:fields"; | ||
| var testObject = (value, { path }) => { | ||
@@ -225,3 +224,2 @@ if (typeof value !== "object") { | ||
| var createTestObjectStrict = ({ | ||
| optional, | ||
| value: schemaValue | ||
@@ -236,3 +234,3 @@ }) => { | ||
| for (const key in currentValue) { | ||
| if (!(key in schemaValue) && !optional.includes(key)) { | ||
| if (!(key in schemaValue)) { | ||
| return messages.object.no_property({ path: key }); | ||
@@ -255,9 +253,2 @@ } | ||
| this.context.rules.push({ | ||
| name: "object:strict", | ||
| test: createTestObjectStrict({ | ||
| optional: [], | ||
| value: config.value | ||
| }) | ||
| }); | ||
| this.context.rules.push({ | ||
| name: "object:fields", | ||
@@ -278,59 +269,17 @@ test: (currentValue, { path }) => { | ||
| } | ||
| notStrict() { | ||
| strictObject() { | ||
| const strictIdx = this.context.rules.findIndex(({ name }) => name === strictName); | ||
| if (strictIdx !== undefined) { | ||
| this.context.rules.splice(strictIdx, 1); | ||
| if (strictIdx === -1) { | ||
| this.context.rules.splice(1, 0, { | ||
| name: strictName, | ||
| test: createTestObjectStrict({ value: this.value }) | ||
| }); | ||
| } | ||
| const fieldsIdx = this.context.rules.findIndex(({ name }) => name === fieldsName); | ||
| if (fieldsIdx !== undefined) { | ||
| this.context.rules[fieldsIdx] = { | ||
| name: fieldsName, | ||
| test: (currentValue, { path }) => { | ||
| for (const key in this.value) { | ||
| const schema = this.value[key]; | ||
| const error = schema.validate(currentValue[key], { | ||
| path: path === "" ? key : `${path}.${key}` | ||
| }); | ||
| if (error !== "") { | ||
| return error; | ||
| } | ||
| } | ||
| return ""; | ||
| } | ||
| }; | ||
| } | ||
| return this; | ||
| } | ||
| optionalFields(optionalFields) { | ||
| notStrict() { | ||
| const strictIdx = this.context.rules.findIndex(({ name }) => name === strictName); | ||
| if (strictIdx !== undefined) { | ||
| this.context.rules[strictIdx] = { | ||
| name: "object:strict", | ||
| test: createTestObjectStrict({ | ||
| optional: optionalFields, | ||
| value: this.value | ||
| }) | ||
| }; | ||
| if (strictIdx !== -1) { | ||
| this.context.rules.splice(strictIdx, 1); | ||
| } | ||
| const fieldsIdx = this.context.rules.findIndex(({ name }) => name === fieldsName); | ||
| if (fieldsIdx !== undefined) { | ||
| this.context.rules[fieldsIdx] = { | ||
| name: fieldsName, | ||
| test: (currentValue, { path }) => { | ||
| for (const key in this.value) { | ||
| const schema = this.value[key]; | ||
| if (!(key in currentValue) && optionalFields.includes(key)) { | ||
| continue; | ||
| } | ||
| const error = schema.validate(currentValue[key], { | ||
| path: path === "" ? key : `${path}.${key}` | ||
| }); | ||
| if (error !== "") { | ||
| return error; | ||
| } | ||
| } | ||
| return ""; | ||
| } | ||
| }; | ||
| } | ||
| return this; | ||
@@ -340,6 +289,3 @@ } | ||
| function object(value) { | ||
| return ObjectDesy.new({ | ||
| value, | ||
| context: Context.new() | ||
| }); | ||
| return ObjectDesy.new({ value, context: Context.new() }); | ||
| } | ||
@@ -346,0 +292,0 @@ |
@@ -14,3 +14,3 @@ import { Infer, Schema } from '../schema/schema.ts'; | ||
| number(): NumberDesy; | ||
| object<TValue extends ObjectDesyValue>(shape: TValue): ObjectDesy<TValue, { [K in keyof TValue as undefined extends Infer<TValue[K]> ? never : K]: Infer<TValue[K]>; } & { [K_1 in keyof TValue as undefined extends Infer<TValue[K_1]> ? K_1 : never]?: Infer<TValue[K_1]> | undefined; }>; | ||
| object<TValue extends ObjectDesyValue>(shape: TValue): ObjectDesy<TValue, { [K in keyof TValue as undefined extends Infer<TValue[K]> ? never : K]: Infer<TValue[K]>; } & { [K_1 in keyof TValue as undefined extends Infer<TValue[K_1]> ? K_1 : never]?: Infer<TValue[K_1]> | undefined; } extends infer T ? T extends { [K in keyof TValue as undefined extends Infer<TValue[K]> ? never : K]: Infer<TValue[K]>; } & { [K_1 in keyof TValue as undefined extends Infer<TValue[K_1]> ? K_1 : never]?: Infer<TValue[K_1]> | undefined; } ? T extends object ? { [K_2 in keyof T]: T[K_2]; } : T : never : never>; | ||
| boolean(): BooleanDesy<boolean>; | ||
@@ -17,0 +17,0 @@ string(): StringDesy<string>; |
| import { Schema, type Infer } from '../schema/schema.ts'; | ||
| import { type ConfigValue } from '../types.ts'; | ||
| export type ObjectDesyValue = Record<string, Schema<any>>; | ||
| type PreparedTypes<TValue extends ObjectDesyValue> = { | ||
| type Expand<T> = T extends object ? { | ||
| [K in keyof T]: T[K]; | ||
| } : T; | ||
| type PreparedTypes<TValue extends ObjectDesyValue> = Expand<{ | ||
| [K in keyof TValue as undefined extends Infer<TValue[K]> ? never : K]: Infer<TValue[K]>; | ||
| } & { | ||
| [K in keyof TValue as undefined extends Infer<TValue[K]> ? K : never]?: Infer<TValue[K]>; | ||
| }; | ||
| type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>; | ||
| }>; | ||
| export declare class ObjectDesy<TValue extends ObjectDesyValue, TValueTypes = PreparedTypes<TValue>> extends Schema<TValueTypes> { | ||
| static new<TValue extends ObjectDesyValue>(config: ConfigValue<TValue>): ObjectDesy<TValue, PreparedTypes<TValue>>; | ||
| static new<TValue extends ObjectDesyValue>(config: ConfigValue<TValue>): ObjectDesy<TValue, Expand<{ [K in keyof TValue as undefined extends Infer<TValue[K]> ? never : K]: Infer<TValue[K]>; } & { [K_1 in keyof TValue as undefined extends Infer<TValue[K_1]> ? K_1 : never]?: Infer<TValue[K_1]> | undefined; }>>; | ||
| value: TValue; | ||
| constructor(config: ConfigValue<TValue>); | ||
| strictObject(): this; | ||
| notStrict(): this; | ||
| optionalFields<TValueLocal extends keyof TValueTypes & string = any>(optionalFields: TValueLocal[]): ObjectDesy<TValue, PartialBy<this["types"], TValueLocal>>; | ||
| } | ||
| export declare function object<TValue extends ObjectDesyValue>(value: TValue): ObjectDesy<TValue, PreparedTypes<TValue>>; | ||
| export declare function object<TValue extends ObjectDesyValue>(value: TValue): ObjectDesy<TValue, Expand<{ [K in keyof TValue as undefined extends Infer<TValue[K]> ? never : K]: Infer<TValue[K]>; } & { [K_1 in keyof TValue as undefined extends Infer<TValue[K_1]> ? K_1 : never]?: Infer<TValue[K_1]> | undefined; }>>; | ||
| export {}; |
+1
-1
| { | ||
| "name": "desy", | ||
| "version": "0.0.20", | ||
| "version": "0.0.21", | ||
| "type": "module", | ||
@@ -5,0 +5,0 @@ "types": "./dist/types/desy.d.ts", |
33001
-3.09%858
-5.4%