Giter Site home page Giter Site logo

dmodel's People

Contributors

brandonpayton avatar erotavlas avatar kriszyp 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

Watchers

 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

dmodel's Issues

No validation on function setData()?

I noticed in the unit tests

dmodel-master/tests/extenstions/validating-jsonSchema.js

that you create the model and directly insert the data using setData() function. WIth no validation. I see no mentionof validation on this method available in the documentation. Is it possible to validate the data against the schema when setData(0 is called? I mean we can't always guarantee that the data will match the schema exactly if it comes from an external source. So why put it in the Model like that without checking?

var validatingMemory = (declare([Memory, Validating]))({
    Model: jsonSchema({
        properties: {
            prime: {
                type: 'boolean'
            },
            number: {
                type: 'number',
                minimum: 1,
                maximum: 10
            },
            name: {
                type: 'string',
                required: true
            }
        }
    })
});
validatingMemory.setData([
    {id: 1, name: 'one', number: 1, prime: false, mappedTo: 'E'},
    {id: 2, name: 'two', number: 2, prime: true, mappedTo: 'D'},
    {id: 3, name: 'three', number: 3, prime: true, mappedTo: 'C'},
    {id: 4, name: 'four', number: 4, even: true, prime: false, mappedTo: null},
    {id: 5, name: 'five', number: 5, prime: true, mappedTo: 'A'}
]);

extenstions/jsonSchema - consider returning just the schema

it's hard to use extensions/jsonSchema with a custom base Model. e.g. i have a base Model for all my models which is something like this:

define(function (require) {
    var Model = require('dmodel/Model');

    return Model.createSubclass({
        rpc: function (method) {
            var params = Array.prototype.slice.call(arguments, 1);

            return this._store._rpc(this._store.target + this._store.getIdentity(this), method, params);
        }
    });
});

then each of my models just depend on that module and define their own schemas.

BaseModel.createSubcalss({
  schema: ...
});

trying to use extensions/jsonSchema causes a lot of friction because it takes a schema and returns a Model. i would have to do something like using AMD map config to map my own model as dmodel/Model into dmodel/extensions/jsonSchema or use model.prototype.schema as the schema to pass to another Model.

it would be easier if the extension took a json-schema and returned the dmodel schema so that it is used like so:

Model.createSubclass({
  schema: jsonSchema(...)
});

building with json-schema

the build automatically discovers the json-schema package.json and sets the location for json-schema to be json-schema/lib based on the "directories" directive in the package.json. this causes the json-schema/lib/validate dependency to cause an error for the build since the build resolves that as json-schema/lib/lib/validate.

not sure what to do about this...

  1. use json-schema/validate as the dependency but configure the loader to set the location for the json-schema package to be json-schema/lib. this is not ideal since the default loader config won't work.
  2. change json-schema's package.json so that the build discovers json-schema/lib/validate as expected. this is not ideal since other node packages probably already rely on the "directories" directive.
  3. instruct people to include the following in their build profile - which is also not ideal since the default build profile config won't work.
{
    name: 'json-schema',
    location: 'json-schema',
    resourceTags: {
        amd: function (filename) {
            return /\/lib\/(?:links|validate)\.js$/.test(filename);
        }
    }
},

i guess this issue could probably belong in the json-schema repo but i'm reporting it here in the hope that adopters of dmodel will find this issue before complaining about how it isn't working.

Getting 404 error on validate.js

We are trying to use extensions/jsonSchema.js module but it is unable to locate 'json-schema/lib/validate'
What is this? This javascript file does not appear anywhere in the latest release (dmodel 0.1.0) or on the latest commit on github.

Please advise how to resolve this, Thanks

Listener has no knowledge of what property it is registered for

The listener that is passed to observe(listener, options) is a function that is provided two values, 'newvalue' and 'oldvalue when the property it was registered for in the model was changed. However once inside the listener function, there is no way to know what property these values correspond to.

For example say you are trying to set up all your observers dynamically like this in a for loop

                    widget.observeHandles[property] = widget.propertyObjects[property].observe(
         
                        function (newValue, oldValue) {
                            
                            console.log(property + " old value: " + oldValue);
                            console.log(property + " new value: " + newValue);

                            that.properties[property] = newValue;
                            that.propertyFunctions[property](newValue);
                        },
                        {
                            onlyFutureUpdates: onlyFutureUpdatesFlag
                        }

                    );

Once inside the function there is no knowledge of the property change which activated this function call, therefore you cannot run any code that depends on the name of that property.

Can this be added as a third parameter to the listener - like function(newValue, oldValue, property)?

publish instructions for how to run tests

a node idiom (and hence a de facto package.json idiom) is that npm install should install all dependencies and then npm test would run the tests. currently when i try to run npm install it fails because dstore is not published to npm.

i'm really not concerned about what the exact method is to install the dependencies and run the tests but having a simple way of doing this helps with continuous integration and gives contributors a simple way to test their contributions.

if i can know how to run the tests i'll open a PR with a .travis.yml to get CI working.

Set is not a function?

Trying to set a value on a property but there is no such set function.

For example

            var MyModel = declare(Model, {
                schema: {
                    firstName: 'string', // simple definition
                    lastName: {
                        type: 'string',
                        required: true
                    }
                }
            });
            MyModel.set('firstName', 'John');

Produces error

Uncaught TypeError: MyModel.set is not a function

Json schema validation not behaving as expected

I created a Memory store (model) based on a simple Json Schema like this and tried to put an an invalid value, however the object was still created inside the store.

<script>

    require(
        [
        'dojo/_base/declare',
        'dstore/Memory',
        'dmodel/extensions/jsonSchema'
        ], 
    function (declare, Memory, jsonSchema) {
        var myStore = new Memory({

            model: jsonSchema({
                properties: {
                    someProperty: {
                        type: "number",
                        minimum: 0,
                        maximum: 10
                    },
                }
            })
        });

        myStore.put({ id: 1, someProperty: -21 });

    });
</script>

Next I tried to use the String Validator like this

<script>

    require(
        [
        'dojo/_base/declare',
        'dstore/Memory',
        'dmodel/extensions/jsonSchema',
        'dmodel/validators/StringValidator'
        ], 
    function (declare, Memory, jsonSchema, StringValidator) {
        var myStore = new Memory({

            model: jsonSchema({
                properties: {
                    someProperty: new StringValidator({
                        // must be at least 4 characters
                        minimumLength: 4,
                        // and max of 20 characters
                        maximumLength: 20,
                        // and only letters or numbers
                        pattern: /^\w+$/
                    }),
                }
            })
        });

        myStore.put({ id: 1, someProperty: '#' });

    });
</script>

Again the property was created in the store. There were no validation errors.
Am I doing something wrong?

My end goal is to build a model from json schema, then when data is entered into the model, to validate based on the constraints defined in the schema. Data that does not match the constraints should not be put into the store. Is this even possible?

observe callback function second parameter 'oldValue' is always undefined

The listener that observes for changes to property objects, contains the newValue but the oldValue is always undefined.

This documentation states that "The listener will be called with the the new value as the first argument, and the old value as the second argument."

So in the example below i expect when put(7) is called that in the callback function the newValue is 7 and the oldValue is 5 - however oldValue is undefined.

      var validatingMemory = (declare([Memory, Validating]))({
            Model: jsonSchema({
                "type": "object",
                "required": ["name"],
                "properties": {
                    "prime": {
                        "type": 'boolean'
                    },
                    "number": {
                        "type": 'number',
                        "minimum": 1,
                        "maximum": 10
                    },
                    "name": {
                        "type": 'string'
                    },
                    "mappedTo": {
                        "type": 'string'
                    }
                },
                "additionalProperties": false
            }),

            idProperty: "name"
        });  

      var data = [
            { name: 'one', number: 1, prime: false, mappedTo: 'E' },
            { name: 'two', number: 2, prime: true, mappedTo: 'D' },
            { name: 'three', number: 3, prime: true, mappedTo: 'C' },
            { name: 'four', number: 4, even: true, prime: false, mappedTo: 'B' },
            { name: 'five', number: 5, prime: true, mappedTo: 'A' }
        ];
        //convert data to json string
        var jsonString = JSON.stringify(data);

        //convert string back to json object and insert back to Model
        var obj = JSON.parse(jsonString);
        validatingMemory.setData(obj);

        var testingProperty = validatingMemory.getSync("five");
        var testingPropertyObject = testingProperty.property('number');

        testingPropertyObject.observe(
            function (newValue, oldValue) {
                console.log("old value: " + oldValue);
                console.log("changed to new value: " + newValue);
            },
            {
                onlyFutureUpdates: false
            }
        );
        testingPropertyObject.put(7);

sending _scenario

Right now if an object is created using dmodel the _scenario property is added. Would it be possible to delete it before it is sent to the server in the put method? My server rejects the call because it can't find an object with _scenario to bind to. Rather than having to add it to all the objects being received it would make sense to delete the property first.
I added this at line 87 of Rest.js and it worked perfectly.
delete object._scenario;

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.