fhirbuilder
Version:
Another FHIR Tool to build FHIR Resources
246 lines (245 loc) • 9.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrimitivesValidator = void 0;
const operation_outcome_exception_1 = require("../../../commons/exceptions/operation-outcome.exception");
const Base64BinaryValidator = (value, path, errors) => {
// regex for /^(\s*([0-9a-zA-Z\+\=]){4}\s*)+$/
const regex = /^(\s*([0-9a-zA-Z+=]){4}\s*)+$/;
if (!regex.test(value)) {
errors.push(new operation_outcome_exception_1.OperationOutcomeIssueException({
severity: 'error',
code: 'invalid',
diagnostics: `Invalid base64Binary.`,
details: {
text: `Path: ${path}. Value: ${value}`,
},
}));
}
};
const BooleanValidator = (value, path, errors) => {
// regex for ^true|false$
const regex = /^true|false$/;
if (!regex.test(value)) {
errors.push(new operation_outcome_exception_1.OperationOutcomeIssueException({
severity: 'error',
code: 'invalid',
diagnostics: `Invalid boolean`,
details: {
text: `Path: ${path}. Value: ${value}`,
},
}));
}
};
const CanonicalValidator = (value, path) => {
// regex for ^\\S*$
const regex = /^\S*$/;
if (!regex.test(value)) {
throw new Error(`Invalid canonical: ${value} at path: ${path}`);
}
};
const CodeValidator = (value, path) => {
// regex for ^[^\\s]+(\\s[^\\s]+)*$
const regex = /^[^\s]+(\s[^\s]+)*$/;
if (!regex.test(value)) {
throw new Error(`Invalid code: ${value} at path: ${path}`);
}
};
const DateValidator = (value, path) => {
// regex for ^([0-9]([0-9]([0-9][1-9]|[1-9]0)|[1-9]00)|[1-9]000)(-(0[1-9]|1[0-2])(-(0[1-9]|[1-2][0-9]|3[0-1]))?)?$
const regex = /^([0-9]([0-9]([0-9][1-9]|[1-9]0)|[1-9]00)|[1-9]000)(-(0[1-9]|1[0-2])(-(0[1-9]|[1-2][0-9]|3[0-1]))?)?$/;
if (!regex.test(value)) {
throw new Error(`Invalid date: ${value} at path: ${path}`);
}
};
const DateTimeValidator = (value, path, errors) => {
// regex for ^([0-9]([0-9]([0-9][1-9]|[1-9]0)|[1-9]00)|[1-9]000)(-(0[1-9]|1[0-2])(-(0[1-9]|[1-2][0-9]|3[0-1])(T([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\\.[0-9]+)?(Z|(\\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00)))?)?)?$
const regex = /^([0-9]([0-9]([0-9][1-9]|[1-9]0)|[1-9]00)|[1-9]000)(-(0[1-9]|1[0-2])(-(0[1-9]|[1-2][0-9]|3[0-1])(T([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\.[0-9]+)?(Z|(\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00)))?)?)?$/;
if (!regex.test(value)) {
errors.push(new operation_outcome_exception_1.OperationOutcomeIssueException({
severity: 'error',
code: 'invalid',
diagnostics: `Invalid dateTime.`,
details: {
text: `Path: ${path}. Value: ${value}`,
},
}));
// throw new Error(`Invalid dateTime: ${value} at path: ${path}`);
}
};
const DecimalValidator = (value, path, errors) => {
// regex for ^-?(0|[1-9][0-9]*)(\\.[0-9]+)?([eE][+-]?[0-9]+)?$
const regex = /^-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?$/;
if (!regex.test(value.toString())) {
errors.push(new operation_outcome_exception_1.OperationOutcomeIssueException({
severity: 'error',
code: 'invalid',
diagnostics: `Invalid decimal`,
details: {
text: `Path: ${path}. Value: ${value}`,
},
}));
}
};
const IdValidator = (value, path) => {
// regex for ^[A-Za-z0-9\\-\\.]{1,64}$
const regex = /^[A-Za-z0-9\-.]{1,64}$/;
if (!regex.test(value)) {
throw new Error(`Invalid id: ${value} at path: ${path}`);
}
};
const InstantValidator = (value, path, errors) => {
// regex for ^([0-9]([0-9]([0-9][1-9]|[1-9]0)|[1-9]00)|[1-9]000)-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])T([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\\.[0-9]+)?(Z|(\\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00))$
const regex = /^([0-9]([0-9]([0-9][1-9]|[1-9]0)|[1-9]00)|[1-9]000)-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])T([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\.[0-9]+)?(Z|(\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00))$/;
if (!regex.test(value)) {
errors.push(new operation_outcome_exception_1.OperationOutcomeIssueException({
severity: 'error',
code: 'invalid',
diagnostics: `Invalid instant.`,
details: {
text: `Path: ${path}. Value: ${value}`,
},
}));
//throw new Error(`Invalid instant: ${value} at path: ${path}`);
}
};
const IntegerValidator = (value, path) => {
// regex for ^-?([0]|([1-9][0-9]*))$
const regex = /^-?([0]|([1-9][0-9]*))$/;
if (!regex.test(value.toString())) {
throw new Error(`Invalid integer: ${value} at path: ${path}`);
}
const minValue = -2147483648;
const maxValue = 2147483647;
if (value < minValue || value > maxValue) {
throw new Error(`Invalid integer: ${value} at path: ${path}`);
}
};
const Integer64Validator = (value, path) => {
// regex for ^-?([0]|([1-9][0-9]*))$
const regex = /^-?(0|([1-9][0-9]*))$/;
if (!regex.test(value.toString())) {
throw new Error(`Invalid integer64: ${value} at path: ${path}`);
}
const minValue = -9223372036854775808n;
const maxValue = 9223372036854775807n;
if (value < minValue || value > maxValue) {
throw new Error(`Invalid integer64: ${value} at path: ${path}`);
}
};
const MarkdownValidator = (value, path) => {
// regex for ^[ \\r\\n\\t\\S]+$
const regex = /^[ \r\n\t\S]+$/;
if (!regex.test(value)) {
throw new Error(`Invalid markdown: ${value} at path: ${path}`);
}
};
const OidValidator = (value, path) => {
// regex for ^urn:oid:[0-2](\\.(0|[1-9][0-9]*))+$
const regex = /^urn:oid:[0-2](\.(0|[1-9][0-9]*))+$/;
if (!regex.test(value)) {
throw new Error(`Invalid oid: ${value} at path: ${path}`);
}
};
const StringValidator = (value, path) => {
// regex for ^[ \\r\\n\\t\\S]+$
const regex = /^[ \r\n\t\S]+$/;
if (!regex.test(value)) {
throw new Error(`Invalid string: ${value} at path: ${path}`);
}
};
const PositiveIntValidator = (value, path) => {
// regex for ^[1-9][0-9]*$
const regex = /^[1-9][0-9]*$/;
if (!regex.test(value.toString())) {
throw new Error(`Invalid positiveInt: ${value} at path: ${path}`);
}
const minValue = 1;
const maxValue = 2147483647;
if (value < minValue || value > maxValue) {
throw new Error(`Invalid positiveInt: ${value} at path: ${path}`);
}
};
const TimeValidator = (value, path, errors) => {
// regex for ^([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\\.[0-9]+)?$
const regex = /^([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\.[0-9]+)?$/;
if (!regex.test(value)) {
errors.push(new operation_outcome_exception_1.OperationOutcomeIssueException({
severity: 'error',
code: 'invalid',
diagnostics: `Invalid time`,
details: {
text: `Path: ${path}. Value: ${value}`,
},
}));
}
};
const UnsignedIntValidator = (value, path, errors) => {
// regex for ^[0]|([1-9][0-9]*)$
const regex = /^[0]|([1-9][0-9]*)$/;
if (!regex.test(value.toString())) {
errors.push(new operation_outcome_exception_1.OperationOutcomeIssueException({
severity: 'error',
code: 'invalid',
diagnostics: `Invalid unsignedInt`,
details: {
text: `Path: ${path}. Value: ${value}`,
},
}));
}
const minValue = 0;
const maxValue = 2147483647;
if (value < minValue || value > maxValue) {
errors.push(new operation_outcome_exception_1.OperationOutcomeIssueException({
severity: 'error',
code: 'invalid',
diagnostics: `Invalid unsignedInt`,
details: {
text: `Path: ${path}. Value: ${value}`,
},
}));
}
};
const UriValidator = (value, path) => {
// regex for ^\\S*$
const regex = /^\S*$/;
if (!regex.test(value)) {
throw new Error(`Invalid uri: ${value} at path: ${path}`);
}
};
const UrlValidator = (value, path) => {
// regex for ^\\S*$
const regex = /^\S*$/;
if (!regex.test(value)) {
throw new Error(`Invalid url: ${value} at path: ${path}`);
}
};
const UuidValidator = (value, path) => {
// regex for ^urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$
const regex = /^urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
if (!regex.test(value)) {
throw new Error(`Invalid uuid: ${value} at path: ${path}`);
}
};
exports.PrimitivesValidator = {
base64Binary: Base64BinaryValidator,
boolean: BooleanValidator,
canonical: CanonicalValidator,
code: CodeValidator,
date: DateValidator,
dateTime: DateTimeValidator,
decimal: DecimalValidator,
id: IdValidator,
instant: InstantValidator,
integer: IntegerValidator,
integer64: Integer64Validator,
markdown: MarkdownValidator,
oid: OidValidator,
positiveInt: PositiveIntValidator,
string: StringValidator,
time: TimeValidator,
unsignedInt: UnsignedIntValidator,
uri: UriValidator,
url: UrlValidator,
uuid: UuidValidator,
};
// const XhtmlValidator = (value: string, path: string) => {};