feat(v2): add strict schema validation for AI classification JSON
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
7dccb85efa
commit
c445e00fc5
@@ -0,0 +1,44 @@
|
|||||||
|
const VALID_CLASSIFICATIONS = [
|
||||||
|
'ACTIONABLE_NOW', 'UNASSIGNED_ACTIONABLE', 'WAITING_REQUESTER', 'WAITING_VENDOR',
|
||||||
|
'WAITING_APPROVAL', 'SCHEDULED', 'DEPENDENCY_BLOCKED', 'NEEDS_COORDINATION',
|
||||||
|
'PROJECT_LONG_TERM', 'DUPLICATE_OR_OBSOLETE', 'INSUFFICIENT_CONTEXT',
|
||||||
|
];
|
||||||
|
|
||||||
|
function validateAiClassification(raw) {
|
||||||
|
const errors = [];
|
||||||
|
if (!raw || typeof raw !== 'object') {
|
||||||
|
return { valid: false, data: null, errors: ['resposta nao e um objeto JSON'] };
|
||||||
|
}
|
||||||
|
if (!VALID_CLASSIFICATIONS.includes(raw.classification)) {
|
||||||
|
errors.push(`classification invalida: ${raw.classification}`);
|
||||||
|
}
|
||||||
|
if (typeof raw.actionable_today !== 'boolean') errors.push('actionable_today deve ser boolean');
|
||||||
|
const confidence = Number(raw.confidence);
|
||||||
|
if (!Number.isFinite(confidence) || confidence < 0 || confidence > 1) errors.push('confidence deve estar entre 0 e 1');
|
||||||
|
if (typeof raw.next_action !== 'string' || raw.next_action.length > 200) errors.push('next_action invalido');
|
||||||
|
if (raw.evidence != null && !Array.isArray(raw.evidence)) errors.push('evidence deve ser array');
|
||||||
|
if (typeof raw.reason !== 'string') errors.push('reason deve ser string');
|
||||||
|
|
||||||
|
if (errors.length) return { valid: false, data: null, errors };
|
||||||
|
|
||||||
|
return {
|
||||||
|
valid: true,
|
||||||
|
errors: [],
|
||||||
|
data: {
|
||||||
|
classification: raw.classification,
|
||||||
|
actionable_today: raw.actionable_today,
|
||||||
|
confidence,
|
||||||
|
blocker_type: raw.blocker_type ?? null,
|
||||||
|
waiting_for: raw.waiting_for ?? null,
|
||||||
|
next_action: raw.next_action,
|
||||||
|
suggested_action_type: raw.suggested_action_type ?? 'technical_action',
|
||||||
|
business_risk: raw.business_risk ?? 'medium',
|
||||||
|
reason: raw.reason,
|
||||||
|
evidence: Array.isArray(raw.evidence) ? raw.evidence.slice(0, 5) : [],
|
||||||
|
is_fallback: false,
|
||||||
|
fallback_reason: null,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { VALID_CLASSIFICATIONS, validateAiClassification };
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
const { test } = require('node:test');
|
||||||
|
const assert = require('node:assert/strict');
|
||||||
|
const { validateAiClassification, VALID_CLASSIFICATIONS } = require('./classification');
|
||||||
|
|
||||||
|
function validRaw(overrides) {
|
||||||
|
return Object.assign({
|
||||||
|
classification: 'ACTIONABLE_NOW',
|
||||||
|
actionable_today: true,
|
||||||
|
confidence: 0.91,
|
||||||
|
blocker_type: null,
|
||||||
|
waiting_for: null,
|
||||||
|
next_action: 'Validar equipamento e registrar resultado.',
|
||||||
|
suggested_action_type: 'technical_action',
|
||||||
|
business_risk: 'medium',
|
||||||
|
reason: 'Existe acao tecnica pendente.',
|
||||||
|
evidence: ['ultimo apontamento indica necessidade de validacao'],
|
||||||
|
}, overrides);
|
||||||
|
}
|
||||||
|
|
||||||
|
test('objeto valido passa e normaliza campos', () => {
|
||||||
|
const result = validateAiClassification(validRaw({}));
|
||||||
|
assert.equal(result.valid, true);
|
||||||
|
assert.equal(result.data.classification, 'ACTIONABLE_NOW');
|
||||||
|
assert.equal(result.data.is_fallback, false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('classification invalida falha com erro claro', () => {
|
||||||
|
const result = validateAiClassification(validRaw({ classification: 'ALGO_INVENTADO' }));
|
||||||
|
assert.equal(result.valid, false);
|
||||||
|
assert.ok(result.errors.some((e) => e.includes('classification')));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('JSON invalido/null nao lanca excecao', () => {
|
||||||
|
assert.doesNotThrow(() => validateAiClassification(null));
|
||||||
|
assert.doesNotThrow(() => validateAiClassification('nao e objeto'));
|
||||||
|
assert.equal(validateAiClassification(null).valid, false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('confidence fora de 0..1 falha', () => {
|
||||||
|
const result = validateAiClassification(validRaw({ confidence: 1.5 }));
|
||||||
|
assert.equal(result.valid, false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('evidence e limitado a 5 itens', () => {
|
||||||
|
const result = validateAiClassification(validRaw({ evidence: ['a', 'b', 'c', 'd', 'e', 'f', 'g'] }));
|
||||||
|
assert.equal(result.data.evidence.length, 5);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('todas as classes do dominio sao aceitas', () => {
|
||||||
|
for (const classification of VALID_CLASSIFICATIONS) {
|
||||||
|
const result = validateAiClassification(validRaw({ classification }));
|
||||||
|
assert.equal(result.valid, true, `classe ${classification} deveria ser valida`);
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user