larksuite/cli · error

%s has invalid const: %w

Error message

%s has invalid const: %w

What it means

This wraps a json.Marshal failure while serializing a const-shape constraint's declared value. It means the shape definition itself contains a const value that cannot be JSON-encoded (e.g. a channel, func, or cyclic structure), not that the user's input is wrong. Thrown by validateJSONValueAgainstShape in the typedConstShape branch.

Source

Thrown at shortcuts/common/typed_binder.go:403

	switch constraint := shape.(type) {
	case anyJSONShape:
		return nil
	case typedOneOfShape:
		for _, variant := range constraint.Variants {
			if err := validateJSONValueAgainstShape(value, variant, path); err == nil {
				return nil
			}
		}
		return fmt.Errorf("%s does not match any allowed shape", path)
	case typedNullShape:
		if value != nil {
			return fmt.Errorf("%s must be null", path)
		}
		return nil
	case typedConstShape:
		expectedJSON, err := json.Marshal(constraint.Value)
		if err != nil {
			return fmt.Errorf("%s has invalid const: %w", path, err)
		}
		expected, err := decodeJSONValidationValue(expectedJSON)
		if err != nil {
			return fmt.Errorf("%s has invalid const: %w", path, err)
		}
		if !reflect.DeepEqual(value, expected) {
			return fmt.Errorf("%s must equal %v", path, constraint.Value)
		}
		return nil
	case typedStringShape:
		text, ok := value.(string)
		if !ok {
			return fmt.Errorf("%s must be a string", path)
		}
		length := len([]rune(text))
		if constraint.MinLength != nil && length < *constraint.MinLength {
			return fmt.Errorf("%s must contain at least %d characters", path, *constraint.MinLength)
		}

View on GitHub (pinned to 7fd6ef3c07)

Solutions

  1. Replace the const value with a JSON-encodable equivalent (string, number, bool, map, slice).
  2. Marshal the value to JSON at definition time and store the encoded form as the const.
  3. Check for accidentally captured closures or channels in programmatically generated constraints.

Example fix

// before
constraint.Value = func() bool { return true } // unmarshalable

// after
constraint.Value = "enabled"
Defensive patterns

Strategy: fallback

Validate before calling

if _, err := json.Marshal(constraint.Value); err != nil { return fmt.Errorf("invalid const definition: %w", err) }

Type guard

func jsonEncodable(v any) bool { _, err := json.Marshal(v); return err == nil }

Try / catch

if err := binder.Set("mode", input); err != nil {
    if strings.Contains(err.Error(), "has invalid const") {
        return fmt.Errorf("schema definition bug (const not JSON-encodable), not an input problem: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: A compiled shape declares constraint.Value as something json.Marshal cannot encode — func, channel, complex number, or a data structure containing them — so the const check fails before comparing input.

Common situations: A developer or generator builds shape constraints programmatically and puts a non-JSON-serializable Go value (e.g. a func or chan) into a const constraint.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of larksuite/cli@7fd6ef3c07 (2026-09-04). Data as JSON: /api/errors/bd0080821206aca1. Report an issue: GitHub.