Git Product home page Git Product logo

Comments (14)

jkav77 avatar jkav77 commented on August 15, 2024 1

@cdhowie Check out #110 and let me know what you think.

from dynogels.

clarkie avatar clarkie commented on August 15, 2024

Good idea.

I think I saw a workaround but I can't find it now. I'm sure there was a conversation somewhere, either here or on vogels.

from dynogels.

cdhowie avatar cdhowie commented on August 15, 2024

The main issue with this currently is that there doesn't seem to be any way to obtain the Joi schema object from a defined model. (Created here, there's no path to the schema object.) So to enable a workaround, I have to expose the schema object myself, for example:

const _ = require('lodash');
const dynogels = require('dynogels');

let define = dynogels.define.bind(dynogels);

dynogels.define = (name, def) =>
    _.set(define(name, def), 'schema', def.schema);

from dynogels.

cdhowie avatar cdhowie commented on August 15, 2024

One thing I forgot is that the table object is reachable from items, but not from models. So what I ask is actually already possible, if you create items instead of using model.save({ ... }) (code is untested but the basic idea works):

'use strict';                                                  

const _ = require('lodash');                                   
const Q = require('q');

function validateAndSaveGels(items) {                         
    return Q.all(                                              
        _.map(items, i =>                                     
            Q.ninvoke(i.table.schema, 'validate', i.get())     
        )                                                      
    )
    .then(() =>                                            
        Q.all(_.map(items, i => Q.ninvoke(i, 'save')))                            
    );                                                         
}                                                              

module.exports = validateAndSaveGels;                          

Called like:

validateAndSaveGels([
    new dynogels.models.Foo({ ... }),
    new dynogels.models.Bar({ ... })
])
.then(...);

from dynogels.

jkav77 avatar jkav77 commented on August 15, 2024

I was just searching for a way to do this. That is, I want to validate a model without trying to save it and seeing if it works or not. Is this still a valid enhancement? I am interested in sending a pull request if so.

from dynogels.

clarkie avatar clarkie commented on August 15, 2024

Yeah, definitely up for a PR. We keep all our dynogels interactions in separate files anyway and export the schemas from there anyway.

from dynogels.

jkav77 avatar jkav77 commented on August 15, 2024

Ok. I started looking at this briefly and didn't immediately understand where the schema was stored. It doesn't seem to be a property of model instances and I couldn't figure out how to grab it from the model definition either. Do you know offhand where to get started on this?

from dynogels.

clarkie avatar clarkie commented on August 15, 2024

Not really to be honest, I think this is where I'd start: https://github.com/clarkie/dynogels/blob/master/lib/table.js#L15

Thanks!

from dynogels.

cdhowie avatar cdhowie commented on August 15, 2024

@dangerginger If you have an item object m, the schema is at m.table.schema. So what you want is m.table.schema.validate(m.get()).

from dynogels.

jkav77 avatar jkav77 commented on August 15, 2024

Thanks @cdhowie. I will look into this and hopefully submit a PR over the weekend.

from dynogels.

cdhowie avatar cdhowie commented on August 15, 2024

@dangerginger Looks good to me

from dynogels.

jkav77 avatar jkav77 commented on August 15, 2024

Fixed by #110

from dynogels.

Dombo avatar Dombo commented on August 15, 2024

Just a heads up - I have pulled v9.0.0 and it does not include these changes.

dynogels@^9.0.0:
  version "9.0.0"
  resolved "https://registry.yarnpkg.com/dynogels/-/dynogels-9.0.0.tgz#cb863764647dfd027afeb9be20bc5082423d5125"
  integrity sha1-y4Y3ZGR9/QJ6/rm+ILxQgkI9USU=
  dependencies:
    async "2.5.0"
    aws-sdk "2.131.0"
    joi "11.3.4"
    lodash "4.17.4"
    uuid "3.1.0"

from dynogels.

cdhowie avatar cdhowie commented on August 15, 2024

@Dombo 9.0.0 was released in November 2017, and this change was committed in December 2017.

from dynogels.

Related Issues (20)

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.