CodeSampleX

Exemplo

golang.org/x/mod v0.18.0: semver.IsValid

Amostra verificada para golang golang.org/x/mod v0.18.0: semver.IsValid. O contrato rodou em go 1.26 · linux debian/x64 · docker e passou.

sha256:a93c0bd55c06cb8a362b572a1fd15509f31bd841f6814147d9fb1289d81930d5

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
1
Chaves de assinatura que o compilaram
1
Ambiente declarado linux 24 · ubuntu · glibc 2.39 x64 go

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-01

Caso

HOW
Objetivo
verify golang.org/x/mod/semver.IsValid in pkg:golang/golang.org/x/mod@v0.18.0
Pacotes
Símbolos
  • golang.org/x/mod/semver.IsValid
Criado
2026-09-01T11:28:12Z

Contrato

  1. semver.IsValid reports true for valid semantic versions with a leading 'v', including full versions, short major/minor forms, and prerelease or build metadata
  2. semver.IsValid reports false for versions missing the leading 'v', versions with leading zeros, malformed prereleases, extra segments, or whitespace

Arquivos

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

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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 golang.org/x/mod/semver.IsValid in pkg:golang/golang.org/x/mod@v0.18.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/mod@v0.18.0
Demonstrate these symbols/APIs:
  - golang.org/x/mod/semver.IsValid

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:3a5565884aedfc78f3a5a398180f8c0ac51d7106c2d5d7ce8d709a17d6ef8345","contract":["semver.IsValid reports true for valid semantic versions with a leading 'v', including full versions, short major/minor forms, and prerelease or build metadata","semver.IsValid reports false for versions missing the leading 'v', versions with leading zeros, malformed prereleases, extra segments, or whitespace"],"goal":"verify golang.org/x/mod/semver.IsValid in pkg:golang/golang.org/x/mod@v0.18.0","kind":"HOW","packages":["pkg:golang/golang.org/x/mod@v0.18.0"],"schemaVersion":1,"symbols":["golang.org/x/mod/semver.IsValid"]},"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/golang.org/x/mod@v0.18.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/mod@v0.18.0","symbols":["golang.org/x/mod/semver.IsValid"],"verifierAdapter":"golang@1"}
go.mod
module example.com/semver-validator

go 1.25.0

require golang.org/x/mod v0.18.0
go.sum
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/mod/semver.IsValid in pkg:golang/golang.org/x/mod@v0.18.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/mod@v0.18.0"
  ],
  "symbols": [
    "golang.org/x/mod/semver.IsValid"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"

	semvervalidator "example.com/semver-validator"
	"golang.org/x/mod/semver"
)

func main() {
	// Assertion 1: Valid semantic versions with leading 'v' return true
	validCases := []string{
		"v0.0.0",
		"v1",
		"v1.2",
		"v1.0.0",
		"v1.2.3",
		"v2.10.34",
		"v1.0.0-alpha",
		"v1.0.0-alpha.1",
		"v1.0.0-0.3.7",
		"v1.0.0-x.7.z.92",
		"v1.0.0-beta+exp.sha.5114f85",
		"v1.0.0+20130313144700",
		"v1.2.3-rc.1+build.42",
		"v0.0.0-20190528180749-0d6a8550f398",
	}

	for _, v := range validCases {
		if !semver.IsValid(v) {
			fmt.Fprintf(os.Stderr, "FAIL: expected semver.IsValid(%q) to be true, got false\n", v)
			os.Exit(1)
		}
		if !semvervalidator.ValidateVersion(v) {
			fmt.Fprintf(os.Stderr, "FAIL: expected ValidateVersion(%q) to be true, got false\n", v)
			os.Exit(1)
		}
	}

	// Assertion 2: Invalid semantic versions return false
	invalidCases := []string{
		"",
		"1.2.3",                // Missing leading 'v'
		"V1.2.3",               // Uppercase 'V'
		"v01.2.3",              // Leading zero in major
		"v1.02.3",              // Leading zero in minor
		"v1.2.03",              // Leading zero in patch
		"v1.2.3.4",             // Extra dot-separated segment
		"v1.2.3-",              // Trailing hyphen with empty prerelease
		"v1.2.3+",              // Trailing plus with empty build metadata
		"v1.2.3-01",            // Leading zero in numeric prerelease
		"v1-alpha",             // Prerelease not allowed on short version
		"v1.2-alpha",           // Prerelease not allowed on short version
		"alpha",                // Plain non-version string
		"v+1.2.3",              // Invalid character after 'v'
		"v-1.2.3",              // Invalid character after 'v'
		"v1.2.3 ",              // Trailing space
		" v1.2.3",              // Leading space
		"v1.2.3/extra",         // Invalid path separator
	}

	for _, v := range invalidCases {
		if semver.IsValid(v) {
			fmt.Fprintf(os.Stderr, "FAIL: expected semver.IsValid(%q) to be false, got true\n", v)
			os.Exit(1)
		}
		if semvervalidator.ValidateVersion(v) {
			fmt.Fprintf(os.Stderr, "FAIL: expected ValidateVersion(%q) to be false, got true\n", v)
			os.Exit(1)
		}
	}

	// Assertion 3: FilterValidVersions filters slices to retain only valid versions
	mixedInput := []string{
		"v1.0.0",
		"2.0.0",
		"v2.1.0-beta.1",
		"invalid",
		"v0.0.1",
		"v1.2",
		"v1.2.3-01",
	}
	expectedFiltered := []string{
		"v1.0.0",
		"v2.1.0-beta.1",
		"v0.0.1",
		"v1.2",
	}

	filtered := semvervalidator.FilterValidVersions(mixedInput)
	if len(filtered) != len(expectedFiltered) {
		fmt.Fprintf(os.Stderr, "FAIL: FilterValidVersions length mismatch: got %d, expected %d\n", len(filtered), len(expectedFiltered))
		os.Exit(1)
	}
	for i := range filtered {
		if filtered[i] != expectedFiltered[i] {
			fmt.Fprintf(os.Stderr, "FAIL: FilterValidVersions mismatch at %d: got %q, expected %q\n", i, filtered[i], expectedFiltered[i])
			os.Exit(1)
		}
	}

	fmt.Println("PASS: golang.org/x/mod/semver.IsValid contract passed")
}
validator.go
package semvervalidator

import (
	"golang.org/x/mod/semver"
)

// ValidateVersion checks if the provided string is a valid Go semantic version.
// Valid Go semantic versions must begin with a leading "v" (e.g. "v1.2.3").
func ValidateVersion(version string) bool {
	return semver.IsValid(version)
}

// FilterValidVersions returns only the strings from the input slice that are valid semantic versions.
func FilterValidVersions(versions []string) []string {
	var valid []string
	for _, v := range versions {
		if semver.IsValid(v) {
			valid = append(valid, v)
		}
	}
	return valid
}

Seeder de origem

anônimo