CodeSampleX

Beispiel

github.com/go-playground/validator/v10 v10.30.3: WithRequiredStructEnabled

Verifiziertes Beispiel für golang github.com/go-playground/validator/v10 v10.30.3: WithRequiredStructEnabled. Der Vertrag lief auf go 1.26 · linux alpine/x64…

sha256:441bda0d68694dfb0743734e67395fffd39d57b1b79b0092b30a21d4c1eef004

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
1
Signaturschlüssel, die es gebaut haben
1
Deklarierte Umgebung linux 24 · ubuntu · glibc 2.39 x64 go

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-08-27

Fall

HOW
Ziel
verify github.com/go-playground/validator/v10.WithRequiredStructEnabled in pkg:golang/github.com/go-playground/validator/v10@v10.30.3
Pakete
Symbole
  • github.com/go-playground/validator/v10.WithRequiredStructEnabled
Erstellt
2026-08-27T23:58:31Z

Contract

  1. validator.New(validator.WithRequiredStructEnabled()) initializes a validator with struct-level required validation enabled
  2. WithRequiredStructEnabled causes validation to fail with a required tag error on zero-value non-pointer struct fields
  3. Default validator without WithRequiredStructEnabled ignores required tags on zero-value non-pointer struct fields and returns nil
  4. Populating all required fields of the nested struct passes validation under WithRequiredStructEnabled
  5. Zero-value structs containing inner required fields fail on the outer struct when WithRequiredStructEnabled is active
  6. WithRequiredStructEnabled validates nil pointer struct fields as required violations

Dateien

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • sample.go
  • spec.json
  • test/contract.go

Quellartefakt herunterladen (tar.gz)

Quelltext

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify github.com/go-playground/validator/v10.WithRequiredStructEnabled in pkg:golang/github.com/go-playground/validator/v10@v10.30.3
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/go-playground/validator/v10@v10.30.3
Demonstrate these symbols/APIs:
  - github.com/go-playground/validator/v10.WithRequiredStructEnabled

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:39b924f4a875cbde5ec44d5f247cf7a336caa2db8338c7da4eedfd199a397c36","contract":["validator.New(validator.WithRequiredStructEnabled()) initializes a validator with struct-level required validation enabled","WithRequiredStructEnabled causes validation to fail with a required tag error on zero-value non-pointer struct fields","Default validator without WithRequiredStructEnabled ignores required tags on zero-value non-pointer struct fields and returns nil","Populating all required fields of the nested struct passes validation under WithRequiredStructEnabled","Zero-value structs containing inner required fields fail on the outer struct when WithRequiredStructEnabled is active","WithRequiredStructEnabled validates nil pointer struct fields as required violations"],"goal":"verify github.com/go-playground/validator/v10.WithRequiredStructEnabled in pkg:golang/github.com/go-playground/validator/v10@v10.30.3","kind":"HOW","packages":["pkg:golang/github.com/go-playground/validator/v10@v10.30.3"],"schemaVersion":1,"symbols":["github.com/go-playground/validator/v10.WithRequiredStructEnabled"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/go-playground/validator/v10@v10.30.3"],"schemaVersion":1,"subject":"pkg:golang/github.com/go-playground/validator/v10@v10.30.3","symbols":["github.com/go-playground/validator/v10.WithRequiredStructEnabled"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require github.com/go-playground/validator/v10 v10.30.3

require (
	github.com/gabriel-vasile/mimetype v1.4.13 // indirect
	github.com/go-playground/locales v0.14.1 // indirect
	github.com/go-playground/universal-translator v0.18.1 // indirect
	github.com/leodido/go-urn v1.4.0 // indirect
	golang.org/x/crypto v0.52.0 // indirect
	golang.org/x/sys v0.45.0 // indirect
	golang.org/x/text v0.37.0 // indirect
)
go.sum
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.13 h1:46nXokslUBsAJE/wMsp5gtO500a4F3Nkz9Ufpk2AcUM=
github.com/gabriel-vasile/mimetype v1.4.13/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.30.3 h1:4MU6YkEwx7GbcPJOZxrtbu+QfF3pJLJuaYTeAH0DYy8=
github.com/go-playground/validator/v10 v10.30.3/go.mod h1:4Axh7oCNGcoGkqLoE4YWt6n20mcEIsPRlB7vPk3lpyc=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.52.0 h1:RMs7fP2rXdep0CftQlK8Uf+kibLm7qkCcradZWYz988=
golang.org/x/crypto v0.52.0/go.mod h1:1QgfPxDqh0T2M/elOJtp9RvuR95kVjir0e6/BvEmGbc=
golang.org/x/sys v0.45.0 h1:dO4czNzziLiiXplLQgBCEpCvXQ3dnkn0SdaZSYdQ+FY=
golang.org/x/sys v0.45.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc=
golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
sample.go
package sample

import (
	"github.com/go-playground/validator/v10"
)

// DatabaseConfig holds database connection settings without inner validation tags.
type DatabaseConfig struct {
	Host string
	Port int
}

// AppConfig represents application configuration with a non-pointer nested struct.
type AppConfig struct {
	AppName  string         `validate:"required"`
	Database DatabaseConfig `validate:"required"`
}

// EndpointConfig holds endpoint settings with inner required tags.
type EndpointConfig struct {
	Host string `validate:"required"`
	Port int    `validate:"required"`
}

// ServiceConfig represents configuration with a nested struct having inner required tags.
type ServiceConfig struct {
	ServiceName string         `validate:"required"`
	Endpoint    EndpointConfig `validate:"required"`
}

// ServerConfig represents configuration with a pointer to a struct.
type ServerConfig struct {
	ServerName string          `validate:"required"`
	Database   *DatabaseConfig `validate:"required"`
}

// NewValidatorWithRequiredStruct initializes a validator with WithRequiredStructEnabled option.
func NewValidatorWithRequiredStruct() *validator.Validate {
	return validator.New(validator.WithRequiredStructEnabled())
}

// NewDefaultValidator initializes a standard validator without options.
func NewDefaultValidator() *validator.Validate {
	return validator.New()
}

// ValidateAppConfig validates an AppConfig struct.
func ValidateAppConfig(v *validator.Validate, cfg AppConfig) error {
	return v.Struct(cfg)
}

// ValidateServiceConfig validates a ServiceConfig struct.
func ValidateServiceConfig(v *validator.Validate, cfg ServiceConfig) error {
	return v.Struct(cfg)
}

// ValidateServerConfig validates a ServerConfig struct.
func ValidateServerConfig(v *validator.Validate, cfg ServerConfig) error {
	return v.Struct(cfg)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/go-playground/validator/v10.WithRequiredStructEnabled in pkg:golang/github.com/go-playground/validator/v10@v10.30.3",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/go-playground/validator/v10@v10.30.3"
  ],
  "symbols": [
    "github.com/go-playground/validator/v10.WithRequiredStructEnabled"
  ]
}
test/contract.go
package main

import (
	"errors"
	"fmt"
	"os"

	"github.com/go-playground/validator/v10"
	"sample"
)

func assert(condition bool, message string) {
	if !condition {
		fmt.Fprintf(os.Stderr, "Assertion failed: %s\n", message)
		os.Exit(1)
	}
}

func main() {
	// Assertion 1: validator.New(validator.WithRequiredStructEnabled()) initializes a validator with struct-level required validation enabled.
	configuredVal := sample.NewValidatorWithRequiredStruct()
	assert(configuredVal != nil, "sample.NewValidatorWithRequiredStruct should return a non-nil validator")

	defaultVal := sample.NewDefaultValidator()
	assert(defaultVal != nil, "sample.NewDefaultValidator should return a non-nil validator")

	// Assertion 2: WithRequiredStructEnabled causes validation to fail with a required tag error on zero-value non-pointer struct fields.
	emptyAppCfg := sample.AppConfig{
		AppName:  "myapp",
		Database: sample.DatabaseConfig{},
	}
	errConfigured := sample.ValidateAppConfig(configuredVal, emptyAppCfg)
	assert(errConfigured != nil, "configured validator should fail on zero-value non-pointer Database struct")

	var valErrs validator.ValidationErrors
	assert(errors.As(errConfigured, &valErrs), "errConfigured should be validator.ValidationErrors")
	assert(len(valErrs) == 1, "expected exactly 1 validation error for zero-value struct")
	assert(valErrs[0].Field() == "Database" && valErrs[0].Tag() == "required", "expected Database field to fail with required tag")

	// Assertion 3: Default validator without WithRequiredStructEnabled ignores required tags on zero-value non-pointer struct fields and returns nil.
	errDefault := sample.ValidateAppConfig(defaultVal, emptyAppCfg)
	assert(errDefault == nil, "default validator without WithRequiredStructEnabled should ignore required on zero-value non-pointer struct and return nil")

	// Assertion 4: Populating all required fields of the nested struct passes validation under WithRequiredStructEnabled.
	validAppCfg := sample.AppConfig{
		AppName: "myapp",
		Database: sample.DatabaseConfig{
			Host: "127.0.0.1",
			Port: 5432,
		},
	}
	errValid := sample.ValidateAppConfig(configuredVal, validAppCfg)
	assert(errValid == nil, "configured validator should succeed when nested struct fields are populated")

	// Assertion 5: Zero-value structs containing inner required fields fail on the outer struct when WithRequiredStructEnabled is active.
	emptyServiceCfg := sample.ServiceConfig{
		ServiceName: "auth-service",
		Endpoint:    sample.EndpointConfig{},
	}
	errServiceDef := sample.ValidateServiceConfig(defaultVal, emptyServiceCfg)
	assert(errServiceDef != nil, "default validator should fail on inner required fields")
	var valErrsDef validator.ValidationErrors
	assert(errors.As(errServiceDef, &valErrsDef), "expected ValidationErrors from default validator")
	assert(len(valErrsDef) == 2, "default validator should report 2 inner field errors")

	errServiceCfg := sample.ValidateServiceConfig(configuredVal, emptyServiceCfg)
	assert(errServiceCfg != nil, "configured validator should fail on outer struct")
	var valErrsCfg validator.ValidationErrors
	assert(errors.As(errServiceCfg, &valErrsCfg), "expected ValidationErrors from configured validator")
	assert(len(valErrsCfg) == 1, "configured validator should report exactly 1 outer struct error")
	assert(valErrsCfg[0].Field() == "Endpoint" && valErrsCfg[0].Tag() == "required", "expected Endpoint to fail with required tag")

	// Assertion 6: WithRequiredStructEnabled validates nil pointer struct fields as required violations.
	nilPointerServerCfg := sample.ServerConfig{
		ServerName: "web-server",
		Database:   nil,
	}
	errNilPointer := sample.ValidateServerConfig(configuredVal, nilPointerServerCfg)
	assert(errNilPointer != nil, "configured validator should fail on nil pointer struct field marked required")
	var valErrsNil validator.ValidationErrors
	assert(errors.As(errNilPointer, &valErrsNil), "errNilPointer should be validator.ValidationErrors")
	assert(len(valErrsNil) == 1, "expected exactly 1 validation error for nil pointer struct")
	assert(valErrsNil[0].Field() == "Database" && valErrsNil[0].Tag() == "required", "expected Database pointer field to fail with required tag")

	fmt.Println("Contract verification passed: all assertions satisfied.")
}

Ursprungs-Seeder

anonym