@@ -17,11 +17,8 @@ type coercionExtension struct {
1717}
1818
1919var (
20- // Pre-compiled regex for boolean coercion
2120 booleanRegex = regexp .MustCompile (`^(true|false)$` )
22- // Pre-compiled regex for number validation
23- numberRegex = regexp .MustCompile (`^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$` )
24- // Pre-compiled regex for integer validation
21+ numberRegex = regexp .MustCompile (`^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$` )
2522 integerRegex = regexp .MustCompile (`^-?(?:0|[1-9]\d*)$` )
2623)
2724
@@ -35,7 +32,7 @@ func (c *coercionExtension) Validate(ctx *jsonschema.ValidatorContext, v any) {
3532 return // Not a string - let normal validation handle it
3633 }
3734
38- // Check if we should coerce and validate the string format
35+ // check if we should coerce and validate the string format
3936 if c .shouldCoerceToBoolean () {
4037 if ! c .isValidBooleanString (str ) {
4138 ctx .AddError (& CoercionError {
@@ -71,8 +68,6 @@ func (c *coercionExtension) Validate(ctx *jsonschema.ValidatorContext, v any) {
7168 }
7269 return
7370 }
74-
75- // No coercion applies - let normal validation handle it
7671}
7772
7873func (c * coercionExtension ) shouldCoerceToBoolean () bool {
@@ -123,7 +118,7 @@ func (c *coercionExtension) isValidIntegerString(s string) bool {
123118 return err == nil
124119}
125120
126- // compileCoercion compiles coercion behavior based on schema type
121+ // CompileCoercion compiles the coercion extension if coercion is allowed and applicable
127122func CompileCoercion (ctx * jsonschema.CompilerContext , obj map [string ]any , allowCoercion bool ) (jsonschema.SchemaExt , error ) {
128123 if ! allowCoercion {
129124 return nil , nil // Coercion disabled
@@ -146,13 +141,13 @@ func CompileCoercion(ctx *jsonschema.CompilerContext, obj map[string]any, allowC
146141 }, nil
147142}
148143
149- // isCoercibleType checks if the type supports scalar coercion
144+ // IsCoercibleType checks if the schema type is one that supports coercion
150145func IsCoercibleType (schemaType any ) bool {
151146 switch t := schemaType .(type ) {
152147 case string :
153148 return t == "boolean" || t == "number" || t == "integer"
154149 case []any :
155- // For type arrays, check if any coercible type is present
150+ // for type arrays, check if any coercible type is present
156151 for _ , item := range t {
157152 if str , ok := item .(string ); ok {
158153 if str == "boolean" || str == "number" || str == "integer" {
0 commit comments