{"record":{"id":"146defc71e8530d5","repo":"BoundaryML/baml","slug":"value-name-already-exists-type-builder","errorCode":null,"errorMessage":"Value ${name} already exists.","messagePattern":"Value (.+?) already exists\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"engine/language_client_typescript/type_builder.js","lineNumber":212,"sourceCode":"    value(name) {\n        if (!this.values.has(name)) {\n            throw new Error(`Value ${name} not found.`);\n        }\n        return new EnumValueViewer();\n    }\n}\nexports.EnumViewer = EnumViewer;\nclass EnumValueViewer {\n    constructor() { }\n}\nexports.EnumValueViewer = EnumValueViewer;\nclass EnumBuilder extends EnumAst {\n    constructor(tb, name, values = new Set()) {\n        super(tb, name, values);\n    }\n    addValue(name) {\n        if (this.values.has(name)) {\n            throw new Error(`Value ${name} already exists.`);\n        }\n        this.values.add(name);\n        return this.bldr.value(name);\n    }\n    listValues() {\n        return Array.from(this.values).map((name) => [name, this.bldr.value(name)]);\n    }\n    value(name) {\n        return this.bldr.value(name);\n    }\n}\nexports.EnumBuilder = EnumBuilder;\n","sourceCodeStart":194,"sourceCodeEnd":225,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_typescript/type_builder.js#L194-L225","documentation":"JavaScript type-builder validation: addValue was called with an enum value name already present in this EnumBuilder's set. Duplicate variants are rejected to keep the generated enum well-formed; the set membership check fires before the value is registered with the underlying builder.","triggerScenarios":"Calling EnumBuilder.addValue('STATUS') twice for the same enum, or re-running registration logic (e.g. a hot-reload or rebuild pass) without clearing the builder's value set.","commonSituations":"Dynamically generating enums from external schemas that contain duplicate keys; idempotency bugs where setup code runs more than once; merging enum values from two sources with overlapping names.","solutions":["Check this.values / listValues() for the name before calling addValue","Make registration idempotent: skip names that already exist instead of adding again","Reset or re-create the EnumBuilder if you need to rebuild the value set from scratch"],"exampleFix":"// before\nenumBuilder.addValue('ACTIVE'); // second call throws\n// after\nif (!enumBuilder.listValues().some(([n]) => n === 'ACTIVE')) {\n  enumBuilder.addValue('ACTIVE');\n}","handlingStrategy":"validation","validationCode":"const existing = new Set(enumBuilder.listValues().map(([n]) => n));\nfor (const v of newValues) if (!existing.has(v)) enumBuilder.addValue(v);","typeGuard":null,"tryCatchPattern":"try { enumBuilder.addValue(name); }\ncatch (e) { if (!String(e.message).includes('already exists')) throw e; }","preventionTips":["Make enum registration idempotent (check-before-add)","Deduplicate value lists sourced from external schemas","Avoid re-running builder setup without reset"],"tags":["enum","duplicate","builder"],"backgroundTag":"file-already-exists","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}