Comments (14)
@cdhowie Check out #110 and let me know what you think.
from dynogels.
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.
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.
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.
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.
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.
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.
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.
@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.
Thanks @cdhowie. I will look into this and hopefully submit a PR over the weekend.
from dynogels.
@dangerginger Looks good to me
from dynogels.
Fixed by #110
from dynogels.
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.
@Dombo 9.0.0 was released in November 2017, and this change was committed in December 2017.
from dynogels.
Related Issues (20)
- Does not enforce required() on nested property HOT 6
- What is the best way to convert a list of objects HOT 1
- Provide access to the AWS request object
- Model.validate not in latest release HOT 1
- Add transactions support HOT 1
- A method which returns the raw DynamoDB params for creating an item
- When I do an .updateAsync() with ReturnValues I recieve a type error
- Item.set doesn't work as advertised HOT 3
- Security question?
- while updating the model getting validationException HOT 1
- Project is dead? HOT 27
- using reserved word in schema causes silent error
- Enable On-Demand Billing Mode
- Cannot use "startKey" when using an index HOT 1
- 'updatedAt' field doesn't change once set.
- ValidationException on .destroy() method HOT 3
- Update joi dependency to use @hapi/joi HOT 6
- Keep getting Error: Invalid schema content when I use define HOT 3
- Stale library? HOT 1
- Is it possible to run dynogels with version 3 of AWS
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dynogels.