Giter VIP home page Giter VIP logo

assert's Introduction

assert's People

Contributors

btford avatar vojtajina 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

assert's Issues

Assert structure with optional fields and disallow unknown fields

Is there an easy way to assert a structure that has some required fields and some optional fields. And have it fail if there are any other unknown fields.

This works but I'm just wondering if there is a better way:

export default class Directive {
    constructor(options: DirectiveOptions) {
        this.selector = options.selector;
        this.bind = options.bind;
    }
}

var DirectiveOptions = assert.define('DirectiveOptions', function(options) {
    //Required fields
    assert(options).is(assert.structure({
        selector: assert.string
    }));

    //Optional fields
    if(options.bind) {
        assert.type(options.bind, Object);
    }

    for(var key in options) {
        if (options.hasOwnProperty(key)) {
            if(key !== 'selector' && key !== 'bind') {
                assert.fail(`${key} is not a valid directive field`);
            }
        }
    }
});

On an unrelated note any idea what I need to use assert.string instead of just string. I'm transpiling with Traceur 0.0.74.

Thanks

allow null/non-null type checking

I should be able to require that a value is exactly the type I specify, and expect it to throw if given null.

var val = null;
assert.type(val, assert.string);

That assertion passes in the current implementation. It would be nice be able to indicate that a value is nullable: assert.type(val, assert.string, true);

Or if we wanted to extend the language to support this: assert.type(?val, assert.string); or non-nullable: assert.type(!val, assert.string);.

export to window.assert

There should be a dist file that exports the assert function on window. This is useful if you want to use this library for type assertions with traceur, but don't want to migrate your whole angular-project to use requirejs :)

separate exceptions in assert methods

E.g. the following code would report a type error:

  static assert(obj) {
    assert(obj).is(assert.structureOf({
      createRootView: Function
    }));
  }

The real problem was the typo: It needs to be assert.structure instead of assert.structureOf. However, the error that assert generated was misleading.

Type Assertions for Named/Optional Arguments should only assert when provided.

Trying to apply Type Assertions for a function that takes optional arguments. when the optional arguments are not provided, it throws errors. will it be nice if assert.argumentTypes check and only apply type checking if optional argument is not undefined?
in the example below, value argument is optional and type is number.
My suggestion is, validate type only if an optional argument is supplied.

export class EnumItem {
    // constructor(name: string, {value} = {}, value: number) { //TODO want to use this 
    constructor(name: string, {value = Infinity}, value: number) { //workaround
        this.name  = name;
        // this.value = (value) ? value: Symbol(name);  //want to use this 
        this.value = (value === Infinity) ? Symbol(name) : value; //workaround


        delete arguments[1].value;
        Object.assign(this, arguments[1]);

        Object.freeze(this);
    }

    toString() {
        return this.name;
    }

    valueOf() {
        return this.value;
    }
}
console.log('EnumItem1', new EnumItem('xname',{value:2}));
console.log('EnumItem2', new EnumItem('xname',{ description:'ssss'})); 

Maximum Call Stack Exceeded when checking Document type

When using assert.is, I'm running into a Maximum Call Stack Exceeded RangeError when the value is of type Document. All other types work fine. This is because prettyprint is seeing it as typeof "object" and is trying to recurse over its properties to print them. I can submit a fix for this later.

var doc = document.implementation.createDocument(null, 'doc');
assert(doc).is(DataView, Blob, Document);

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.