DEV Community

freelancing
freelancing

Posted on

Building a Vehicle Maintenance Data Validator with JavaScript

When building a vehicle maintenance application, accepting user input is easy. Making sure that input is valid, consistent, and useful is the harder part.

Consider an application that records tyre pressure, tread depth, and vehicle registration details. Incorrect values can make reports unreliable, so validation should happen before data is stored.

In this tutorial, we'll build a small JavaScript validator using reusable functions.

1. Define the data structure

A maintenance record might look like this:

const tyreRecord = {
  vehicleId: "CAR-102",
  tyrePressure: 34,
  treadDepth: 5.2,
  checkedAt: "2026-10-25"
};
Enter fullscreen mode Exit fullscreen mode

These are example values, not recommendations for every vehicle. Correct tyre pressure depends on the vehicle manufacturer's specifications and operating conditions.

2. Validate individual fields

Instead of putting every condition inside one large function, create small validators.

function isValidVehicleId(value) {
  return typeof value === "string" &&
    value.trim().length > 0;
}

function isValidPressure(value) {
  return typeof value === "number" &&
    Number.isFinite(value) &&
    value > 0;
}

function isValidTreadDepth(value) {
  return typeof value === "number" &&
    Number.isFinite(value) &&
    value >= 0;
}
Enter fullscreen mode Exit fullscreen mode

Notice that the numeric validators use Number.isFinite(). This prevents values such as NaN and infinity from passing validation.

These functions check data structure and basic validity. A real application would also need domain-specific limits and appropriate warnings.

3. Validate the complete record

function validateTyreRecord(record) {
  const errors = [];

  if (!isValidVehicleId(record.vehicleId)) {
    errors.push("Vehicle ID is required.");
  }

  if (!isValidPressure(record.tyrePressure)) {
    errors.push("Pressure must be a valid positive number.");
  }

  if (!isValidTreadDepth(record.treadDepth)) {
    errors.push("Tread depth must be a valid non-negative number.");
  }

  return {
    valid: errors.length === 0,
    errors
  };
}
Enter fullscreen mode Exit fullscreen mode

Test the validator:

const result = validateTyreRecord(tyreRecord);

if (result.valid) {
  console.log("Record is valid.");
} else {
  console.error(result.errors);
}
Enter fullscreen mode Exit fullscreen mode

For production code, also check that record is a non-null object before reading its properties.

4. Add unit tests

A validator should be tested with both valid and invalid values.

console.assert(isValidPressure(34) === true);
console.assert(isValidPressure(-5) === false);
console.assert(isValidPressure(NaN) === false);

console.assert(isValidTreadDepth(5.2) === true);
console.assert(isValidTreadDepth(-1) === false);
Enter fullscreen mode Exit fullscreen mode

These lightweight assertions are useful while learning. Larger applications should use a test runner such as Vitest or Jest.

5. What changes in a real application?

Client-side validation improves the user experience, but it is not a security boundary. Users can bypass browser validation and send requests directly to an API.

A production system should also:

  • Validate requests on the server.
  • Use a consistent schema for incoming records.
  • Store timestamps in a consistent format.
  • Keep an audit trail for important maintenance changes.
  • Separate invalid data from legitimate safety warnings.

Conclusion

Good data validation starts with small, reusable functions and clear error messages. The same approach works for vehicle inspections, equipment inventories, repair logs, and many other applications.

The important lesson is simple: validate data before trusting it, and test the edge cases before deploying it.

Top comments (0)