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

  • Home
  • SEARCH
  • 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 7827997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:07:33+00:00 2026-06-02T10:07:33+00:00

I would like to have a way to specify for certain Javascript method which

  • 0

I would like to have a way to specify for certain Javascript method which attributes are required, what pattern they should match, and how to respond if they are not.

This is because it results in a lot of repetitive code to check for required and optional parameters at the method level.

Take this example. Here I’d like to build a lightbox. If they send me a string, I’ll display a lightbox with just content. If they send me an options object, I look for a ‘title’ and ‘content’. Wouldn’t it be great to be able to specify this in some standardized way?

// Static method for generating a lightbox
// callerOptions = '' //if sent a string, the lightbox displays it with no title
// callerOptions = {
//      content: '' // required popup contents. can be HTML or text.
//  ,   title: '' // required title for the lightbox
//  ,   subtitle: '' // optional subtitle for lightbox
//  }
lightbox = function (callerOptions) {
    if (!callerOptions) {
        log.warn(_myName + ': calling me without a message to display or any options won\'t do anything');
        return;
    }

    // If they send us a string, assume it's the popup contents
    if (typeof(callerOptions) === 'string') {
        this.options = {};
        this.options.content = callerOptions;

    // Otherwise assume they sent us a good options object
    } else {
        this.options = callerOptions;
    }

    _build();
    _contentLoaded();
};

I’d love to be able to use some library I’ve never heard of to do something like this:

// Maybe this is what it looks like with a method signature enforcement library
lightbox = function (callerOptions) {
    TheEnforcer(
    ,   {   valid: [
                'string' // assumes that it is testing type against arguments by convention
            ,   'typeof([0].title) === "string" && typeof([0].content) === "string"'
            ]
        }
    });

    // If they send us a string, assume it's the popup contents
    if (typeof(callerOptions) === 'string') {
        this.options = { 'content': callerOptions };

    // Otherwise we know they sent us a good options object
    } else {
        this.options = callerOptions;
    }

    _build();
    _contentLoaded();
};

Has anyone ever seen a Javascript library like this? Maybe built into one of the 1000 JS MV* Frameworks?

Edit:
Seems like this is usually taken care of by the MV* frameworks. Backbone.js has both validation and default values on it’s model’s properties. I think these could be used to meet, or nearly meet, the use case I present here.

  • 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-06-02T10:07:35+00:00Added an answer on June 2, 2026 at 10:07 am

    I guess there are two parts to this, the philosophical/architectural and the implementation.

    On the philosophical side, I think there is nothing simpler (for users of my API, not for me, the API developer) than clear expectations and error messages that describe what is and isn’t required for each method.

    On the implementation side, which is what the question was originally about, I think I have to answer my own. Backbone.js’ Models, particularly the .validate portion, seem to fulfill this need pretty well. I have yet to use them but it’s the only thing I’ve found like it so far.

    http://documentcloud.github.com/backbone/#Model-validate

    EDIT:
    Another solution could be compile time. Google Closure Compiler seems to handle this quite nicely: https://developers.google.com/closure/compiler/docs/js-for-compiler

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

Sidebar

Related Questions

I would like to temporarily override the kill-new function. I have a way I
I have found the way to copy the record that I would like, but
I would like to know if there is a way in Android to have
Is there a way to specify regex that would match a string but from
Like most software, users are able to specify how they'd like to handle certain
I have some Javascript files I would like to minify and combine into one
So in my project i would like have a nice treeview that has images.
I would like to have a Windows 2003 server fire a script to fire
I would like to have a QMainWindow that can change it's look at runtime,
I would like to have Ajax form in Rails so i'm using form_remote_tag. The

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.