Code Monkey home page Code Monkey logo

xtypejs's Introduction

xtypejs Logo

Elegant, highly efficient data validation for JavaScript


Overview

  • Provides concise, performant, readable, data and type validation for JavaScript apps, using built-in and user-defined data-validating pseudo types.
  • Improves application efficiency and readability by unifying the most basic but common data and type validations in JavaScript apps, into single, concise, highly optimized operations.
  • Employs bitwise operations, data pre-processing, and memory-efficient memoization for fast, robust performance in small and large apps and libraries.
  • Ready for nodejs, requirejs, and regular script tag.
  • Website – xtype.js.org

Go from this:

function searchEmployees(value) {
    if (typeof value === 'string') {
         if (value.trim().length > 1) {
            return EmployeeDB.searchByName(value);
        } else if (value.trim().length === 1) {
            return EmployeeDB.searchByMiddleInitial(value);
        } else {
            return { error: 'Invalid search value supplied' };
        }
    } else if (typeof value === 'object' && value !== null) {
        if (Object.keys(value).length === 1) {
            return EmployeeDB.searchByFieldValuePair(value);
        } else if (Object.keys(value).length > 1) {
            return { error: 'Search by multiple fields not supported' };
        } else {
            return { error: 'Invalid search value supplied' };
        }
    } else if (typeof value === 'number') {
        if (!isNaN(value) && isFinite(value) && value > 0 && value % 1 === 0) {
            return EmployeeDB.searchByEmployeeNumber(value);
        } else {
            return { error: 'Invalid employee number supplied' };
        }
    } else if (typeof value === 'undefined' || value === null) {
        return { error: 'No search value supplied' };
    } else {
        return { error: 'Invalid search value supplied' };
    }
}

To concise, performant, readable, data validation:

function searchEmployees(value) {
    switch (xtype.which(value, 'str2+ str1 int+ obj1 obj2+ num nil')) {
        case 'str2+':
            return EmployeeDB.searchByName(value);
        case 'str1':
            return EmployeeDB.searchByMiddleInitial(value);
        case 'int+':
            return EmployeeDB.searchByEmployeeNumber(value);
        case 'obj1':
            return EmployeeDB.searchByFieldValuePair(value);
        case 'obj2+':
            return { error: 'Search by multiple fields not supported' };
        case 'num':
            return { error: 'Invalid employee number supplied' };
        case 'nil':
            return { error: 'No search value supplied' };
        default:
            return { error: 'Invalid search value supplied' };
    }
}

And even add custom validation types of your own:

xtype.ext.registerType('ssn', {
    validator: function(val) {
        return typeof val === 'string' && /^\d{3}-\d{2}-\d{4}$/.test(val);
    }
});

function searchEmployees(value) {
    switch (xtype.which(value, 'positive_integer, ssn, multi_char_string')) {
        case 'positive_integer':
            return EmployeeDB.searchByEmployeeNumber(value);
        case 'ssn':
            return EmployeeDB.searchBySSN(value);
        case 'multi_char_string':
            return EmployeeDB.searchByName(value);
        default:
            return { error: 'Invalid search value supplied' };
    }
}

 

Links

Installation

See here.

Dependencies

None.

Build / Test

See here.

License

MIT license.

Website

Visit the website for usage guide, examples, API docs, and installation.

xtype.js.org

xtypejs's People

Contributors

lucono avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xtypejs's Issues

Add not, any, all, some, none interface variants of xtype.is

Thanks for the 0.6 modularity update!

I think it would be helpful to have an isNot method for readability. Instead of something like this:

!xtype.is(data, 'obj')

I'd like to be able to write xtype.isNot(data, 'obj')

Also, xtype.not.is() throws an error (but I don't like that form anyway).

Introduce additional name schemes

The default name scheme is very readable and easily understood, but is verbose to type. On the other hand, the compact name scheme is short and easy to type but is not easily understandable to someone reading it in code without prior knowledge of the scheme's name convention.

For instance, default name scheme example:

xtype.which(value, 'multi_char_string, positive_integer, single_prop_object, number, nothing');

And compact name scheme equivalent:

xtype.which(value, 'str2+ int+ obj1 num nil');

It would be valuable to have available with the library, an additional set of alternative name schemes, which should be intuitive, have broad appeal, and provide a reasonable and practical balance of readability and conciseness falling somewhere between the two current default and compact name schemes.

How to validate Enums

We have a bunch of code that looks like the 'before' in your examples.
However we also need to validate enums, ensuring that the value is one of a list of strings or objects.
I am pretty sure I checked all the docs and didn't find this, any chance of getting some isEnum type functionality that basically does arr1 && indexOf type checks?

default to descriptive names tab on xtype.js.org

as someone who doesn't know too much about your library, but am interested, i believe it would be nice to see the 'descriptive names' tab by default in the code examples. don't worry this won't make me think the library is too verbose or inefficient, it will just help me understand your nice examples a bit faster :).

Break out non-core functionality into optional add-on modules

Extract the following functionality which will be needed only by a subset of library users, into separate, optional, add-on modules:

  • Custom types functionality - All the xtypejs built-in types will still be part of the core library.
  • The util API methods (xtype.util.*) - These can be very useful when needed, but likely only needed by a few users.
  • The bundled compact name scheme.

Extracting these into separate optional add-ons will reduce the size of the main library while still allowing the various extracted add-ons to be used when required, to re-introduce the various functionality only for library users who need them.

As much as possible, this change should maintain backward compatibility.

Checking values with compact representation does not work

I can't make xtype work when I compare values with the compact representation.

If I do this (from the docs):

switch (xtype.which(req.params.testid, 'str2+ int+ nil')) {
    case 'str2+':
        res.send('str2+');
        break;
    case 'int+':
        res.send('int+');
        break;
    case 'nil':
        res.send('nil');
        break;
    default:
        res.send('type not recognized');
        break;
}

This always sends type not recognized.

Maybe I have to set xtype to use the compact representation by default? I can't find how I do this, if that is the case.

Thanks

Support for arbitrary custom-validated types

Add support for arbitrary custom-validated types, based on the discussion started in issue #1. Should include ability to define and register custom types, with arbitrary custom validation logic.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.