@@ -30,6 +30,8 @@ THE SOFTWARE.
3030
3131import { Guard , GlobalsGuard } from '../../guard/index.ts'
3232import * as T from '../../type/index.ts'
33+ import { Check } from '../check/index.ts'
34+ import { Create } from '../create/index.ts'
3335
3436import { FromArray } from './from-array.ts'
3537import { FromBase } from './from-base.ts'
@@ -73,15 +75,35 @@ function AssertRepairableType(context: T.TProperties, type: T.TSchema, value: un
7375 }
7476}
7577// ------------------------------------------------------------------
78+ // FinalizeRepair
79+ //
80+ // When a type includes the ~refine modifier, a post-repair validation
81+ // check must be performed to ensure the repaired value satisfies the
82+ // refine constraint. This logic is implemented as part of FromType to
83+ // ensure the post-refine validation check is handled outside of
84+ // sub-schema constraint checking (i.e., at the top level).
85+ //
86+ // ------------------------------------------------------------------
87+ function FinalizeRepair ( context : T . TProperties , type : T . TSchema , repaired : unknown ) : unknown {
88+ return T . IsRefine ( type )
89+ ? Check ( context , type , repaired )
90+ ? repaired
91+ : Create ( context , type )
92+ : repaired
93+ }
94+ // ------------------------------------------------------------------
7695// FromType
7796// ------------------------------------------------------------------
7897export function FromType ( context : T . TProperties , type : T . TSchema , value : unknown ) : unknown {
79- // Intercept Base
80- if ( T . IsBase ( type ) ) return FromBase ( context , type , value )
81- // Standard Repair
98+ // Base Repair
99+ if ( T . IsBase ( type ) ) {
100+ const repaired = FromBase ( context , type , value )
101+ return FinalizeRepair ( context , type , repaired )
102+ }
103+ // Schema Repair
82104 AssertRepairableValue ( context , type , value )
83105 AssertRepairableType ( context , type , value )
84- return (
106+ const repaired = (
85107 T . IsArray ( type ) ? FromArray ( context , type , value ) :
86108 T . IsEnum ( type ) ? FromEnum ( context , type , value ) :
87109 T . IsIntersect ( type ) ? FromIntersect ( context , type , value ) :
@@ -93,4 +115,5 @@ export function FromType(context: T.TProperties, type: T.TSchema, value: unknown
93115 T . IsUnion ( type ) ? FromUnion ( context , type , value ) :
94116 FromUnknown ( context , type , value )
95117 )
118+ return FinalizeRepair ( context , type , repaired )
96119}
0 commit comments