Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6886079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:42:52+00:00 2026-05-27T05:42:52+00:00

Scenario: We receive a couple of input parameters regarding an object, for ex Course

  • 0

Scenario: We receive a couple of input parameters regarding an object, for ex Course

Course has instructor name, number of students, course time, room no, etc.

We need to validate (number of students > 0, 9am < time < 9pm, etc) the input and create the Object. We need to return the source of error for invalid input.

I could think of two approaches

1) Create a separate Validator class with static methods,

  • validate input, (validate method returns true, or some enum like VALID, INVALID_TIME, INVALID_STUDENT_NUMBER).
  • instantiate the Bean if input is valid.

cons:

  • If we have to process input values before validating them, the above pattern leads to doing it twice if input is valid, once in Validator class, and once before bean setters. We can’t return Bean object since validate method needs to return source of error in case of invalidity.

2) Have the validate method inside Bean class itself, return exception for invalid input. Trace the source of error through different types of exceptions.

cons:

  • need to create multiple custom exceptions.
  • Is it a right way to include validate method inside holder object itself??

I explored a couple of design patterns, but they were not related.

Please help me understanding pros and cons of above approaches, and a better one to follow.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:42:53+00:00Added an answer on May 27, 2026 at 5:42 am

    One of the commentators points out: “You’ll want to end up with a collection of validation errors.” That is important.

    In my experience, validation methods inside of their model objects do not work because the validation logic changes frequently and because something that is not valid under one set of circumstances is valid under another set of circumstances. For example, if the validation method considers anything later than 9 PM to be invalid, the school administer could change change the rules so that courses during summer quarter can run until 11 PM. You don’t want to be stuck with your validation method inside your class when that kind of thing happens, as it inevitably does.

    Also, it is often impossible to validate an object without knowing about the state of other objects in the system. For example, if you have a Loan object, loan values over $100,000 might be invalid, expect when the Customer object is of type Institution. You need to know about both the Loan and Customer objects to correctly validate the Loan.

    The best practices I’ve seen for complex validation are:

    1. Valid simple things before accepting the object. This includes checking for invalid characters or non-digits in number fields.

    2. Create a validation framework that validates a group of dependent objects at once. Do not stop your validation routine at the first error you find. Instead, create a class named ValidationError and have your framework create a collection of these objects. This approach let’s you account for intra-object and inter-object dependencies. Also, you can present all the errors to the user at once instead of forcing the user to correct errors one-at-a-time.

    3. Don’t hard code values like minimumNumberOfStudents or latestClassTime. Instead, put these values in a relational database table or a configuration file. Make the validation framework look up the correct values (and then cache them). When the values change (as they often will) you only need to update a file or a database; there is no need to rebuild and redeploy the entire application. When I did this work, I was coding in Smalltalk. If you are using a dynamic language like Smalltalk or Ruby, it is easy to actually put source code into your database as a “validation block” and execute it at run-time.

    A validation framework might use exceptions, but I recommend against this. Exceptions should be reserved for more drastic errors that are harder to recover from. Also, exceptions can incur a big penalty performance, which is important if the validation is done on a server and not on the client. Worse still, a few platforms have shaky exception handling.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the scenario: I receive a JSON object comprising 140 events. Each event is
I have a scenario in which i receive files from a source and i
In LiveId Web Auth scenario, when client application receive clearcookie request, it is responsible
In an embedded python scenario we are using PyArg_ParseTupleAndKeywords to receive data from Python
Consider a scenario where a Mirth channel has one or more Destinations. In this
I have the following scenario (note that activity A has launchMode=singleTop ): Activity A
i need discover a type. scenario: I receive from DB a unkown type and
Feature Scenario: As a user that has not signed in I want to be
For a remoting scenario, the result would be very good to receive as an
Current scenario: I'm using the gmail oauth api to receive emails on a page.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.