Skip to content

Validation Results

Every identifier class (e.g. PeselIdentifier) exposes the same two methods: isValid() and validate(). Neither ever throws.

validate() returns a ValidationResult instance.

PropertyTypeDescription
isValidbooleantrue when validation passed.
failuresreadonly ValidationFailure[]Empty array on success; one or more failures on failure.
MethodReturn typeDescription
isFailed()booleanInverse of isValid.
getFailures()readonly ValidationFailure[]Returns the failures array.
getFirstFailure()ValidationFailure | nullFirst failure, or null if valid.
hasFailureReason(reason: ValidationFailureReason)booleantrue if any failure matches the given reason.
import { Numerik, ValidationFailureReason } from '@slashlab/numerik-js'
// Passing result
const result = Numerik.pesel().validate('92060512186')
result.isValid // true
result.isFailed() // false
result.failures // []
result.getFirstFailure() // null
// Failing result
const failed = Numerik.nip().validate('0000000000')
failed.isValid // false
failed.isFailed() // true
// Inspect the first (and usually only) failure
const failure = failed.getFirstFailure()
failure?.reason // ValidationFailureReason.InvalidFormat
failure?.message // 'NIP tax office code cannot be 000.'
// Check for a specific reason
failed.hasFailureReason(ValidationFailureReason.InvalidChecksum) // false
failed.hasFailureReason(ValidationFailureReason.InvalidFormat) // true

Each item in failures is a ValidationFailure instance.

PropertyTypeDescription
reasonValidationFailureReasonEnum value identifying the failure category.
messagestringHuman-readable description in English.
ValueRaw valueDescription
InvalidLengthinvalid_lengthInput has the wrong number of digits.
InvalidCharactersinvalid_charactersUnexpected characters are present after stripping allowed separators.
InvalidFormatinvalid_formatCorrect length and characters, but a structural rule is violated (e.g. NIP tax office code 000).
ValueRaw valueDescription
InvalidChecksuminvalid_checksumThe computed checksum does not match the checksum digit.
ValueRaw valueDescription
InvalidDateinvalid_dateThe date encoded inside the identifier is not a real calendar date.
FutureDatefuture_dateThe encoded birth date is in the future.
InvalidMonthinvalid_monthThe month encoding does not correspond to any known century range.
ValueRaw valueDescription
AllZerosall_zerosAll digits are zero — structurally plausible but semantically invalid.
AllSameDigitall_same_digitAll digits are the same non-zero value.

ValidationResult exposes three static constructors used internally and in tests:

import { ValidationResult, ValidationFailure, ValidationFailureReason } from '@slashlab/numerik-js'
// Success
ValidationResult.pass()
// Failure with a list of failures
ValidationResult.fail([
new ValidationFailure(ValidationFailureReason.InvalidChecksum, 'Checksum mismatch.'),
])
// Failure with a single reason — shorthand
ValidationResult.failWithReason(
ValidationFailureReason.InvalidLength,
'Expected 11 digits, got 10.',
)