{"record":{"id":"d27e6ebc9036f474","repo":"larksuite/cli","slug":"s-field-s-s-optional-data-field-must-use-omi","errorCode":null,"errorMessage":"%s field %s (%s): optional Data field must use omitempty","messagePattern":"(.+?) field (.+?) \\((.+?)\\): optional Data field must use omitempty","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shortcuts/common/typed_compile_data.go","lineNumber":238,"sourceCode":"\t\t\t\treturn typedObjectShape{}, fmt.Errorf(\"%s field %s has unsupported json option %q\", path, field.Name, option)\n\t\t\t}\n\t\t}\n\t\tif previous, exists := seen[name]; exists {\n\t\t\treturn typedObjectShape{}, fmt.Errorf(\"%s field %s JSON name %q duplicates field %s\", path, field.Name, name, previous)\n\t\t}\n\t\tseen[name] = field.Name\n\t\tschema, err := parseSchemaTag(field.Tag.Get(\"schema\"), field.Type, input)\n\t\tif err != nil {\n\t\t\treturn typedObjectShape{}, fmt.Errorf(\"%s field %s (%s): %w\", path, field.Name, name, err)\n\t\t}\n\t\tif !input && schema.defaultValue.Set {\n\t\t\treturn typedObjectShape{}, fmt.Errorf(\"%s field %s (%s): Data field cannot declare default\", path, field.Name, name)\n\t\t}\n\t\tif schema.required && omitempty {\n\t\t\treturn typedObjectShape{}, fmt.Errorf(\"%s field %s (%s): required Data field cannot use omitempty\", path, field.Name, name)\n\t\t}\n\t\tif schema.optional && !omitempty {\n\t\t\treturn typedObjectShape{}, fmt.Errorf(\"%s field %s (%s): optional Data field must use omitempty\", path, field.Name, name)\n\t\t}\n\t\tif isNilCapable(field.Type) && schema.nullable == nil {\n\t\t\treturn typedObjectShape{}, fmt.Errorf(\"%s field %s (%s): nil-capable field must declare nullable or nonnullable\", path, field.Name, name)\n\t\t}\n\t\tdescription := strings.TrimSpace(field.Tag.Get(\"doc\"))\n\t\tif input && description == \"\" {\n\t\t\treturn typedObjectShape{}, fmt.Errorf(\"%s field %s (%s): description is required via doc\", path, field.Name, name)\n\t\t}\n\t\tfieldShape, err := shapeForType(field.Type, schema, input, active)\n\t\tif err != nil {\n\t\t\treturn typedObjectShape{}, fmt.Errorf(\"%s field %s (%s): %w\", path, field.Name, name, err)\n\t\t}\n\t\tshape.Fields = append(shape.Fields, typedValueField{Name: name, Description: description, Required: schema.required, Shape: fieldShape})\n\t}\n\treturn shape, nil\n}\n\nfunc validateShape(shape typedValueShape, path string) error {","sourceCodeStart":220,"sourceCodeEnd":256,"githubUrl":"https://github.com/larksuite/cli/blob/7fd6ef3c07182257ce776cdc5a614e122d5bd4b3/shortcuts/common/typed_compile_data.go#L220-L256","documentation":"A field marked `schema:\"optional\"` in a Data struct lacks `omitempty`. Optional output fields must be omitted from the payload when unset; without omitempty the encoder would always emit the zero value, so the compiler enforces the tag combination.","triggerScenarios":"Compiling a Data struct (input=false) where a field has `schema:\"optional\"` but its json tag has no omitempty, via compileStructShape.","commonSituations":"Adding the optional keyword to an existing always-serialized field; converting a required field to optional and forgetting the json tag; hand-writing both tags from memory.","solutions":["Add `omitempty` to the field's json tag","Or change the schema tag back to `required` if the field is actually mandatory"],"exampleFix":"// before\nNote string `json:\"note\" schema:\"optional\" doc:\"...\"`\n// after\nNote string `json:\"note,omitempty\" schema:\"optional\" doc:\"...\"`","handlingStrategy":"validation","validationCode":"if strings.Contains(f.Tag.Get(\"schema\"), \"optional\") && !strings.Contains(f.Tag.Get(\"json\"), \"omitempty\") {\n    return fmt.Errorf(\"field %s: optional must use omitempty\", f.Name)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always write the json tag and schema tag together for optional fields","Use a snippet/template that includes omitempty for optional fields"],"tags":["go","struct-tags","schema","optional-field"],"backgroundTag":"optional-field-missing-omitempty","analyzedSha":"7fd6ef3c07182257ce776cdc5a614e122d5bd4b3","analyzedAt":"2026-09-04T21:17:44.649Z","contentChangedAt":"2026-09-04T21:17:44.649Z","schemaVersion":2},"datasetVersion":"2026-09-12T02:17:10.037Z"}