Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Required fields and default values #5835

Merged
merged 13 commits into from
Jul 26, 2019

Conversation

davimacedo
Copy link
Member

This PR adds the capability for defining required fields and default values to the schema. For example:

         {
            className: 'NewClass',
            fields: {
              ACL: { type: 'ACL' },
              createdAt: { type: 'Date' },
              updatedAt: { type: 'Date' },
              objectId: { type: 'String' },
              newField: {
                type: 'String',
                required: true,
                defaultValue: 'some value',
              },
            },
         }

How it works:

  • When creating a new object, if the value of a field is not set, its default value is applied (in the case it has a default value). If the field turns out being null, undefined or empty string, and it is a required field, an error is thrown.
  • When updating an object, it a field is set to null, empty string or unset, and it is a required field, an error is thrown.

close #3587

@codecov
Copy link

codecov bot commented Jul 22, 2019

Codecov Report

Merging #5835 into master will decrease coverage by 0.6%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5835      +/-   ##
==========================================
- Coverage   93.75%   93.14%   -0.61%     
==========================================
  Files         148      150       +2     
  Lines       10304    10616     +312     
==========================================
+ Hits         9660     9888     +228     
- Misses        644      728      +84
Impacted Files Coverage Δ
src/Adapters/Storage/Mongo/MongoStorageAdapter.js 92.56% <100%> (+0.1%) ⬆️
src/Controllers/SchemaController.js 96.37% <100%> (-0.53%) ⬇️
...rc/Adapters/Storage/Mongo/MongoSchemaCollection.js 97.8% <100%> (+0.12%) ⬆️
src/RestWrite.js 93.72% <100%> (+0.04%) ⬆️
...dapters/Cache/RedisCacheAdapter/KeyPromiseQueue.js 0% <0%> (-95.46%) ⬇️
src/Adapters/Cache/RedisCacheAdapter/index.js 15.9% <0%> (-84.1%) ⬇️
src/Adapters/Auth/apple.js 89.65% <0%> (-10.35%) ⬇️
src/Adapters/Auth/httpsRequest.js 95.23% <0%> (-4.77%) ⬇️
src/Controllers/LoggerController.js 90.52% <0%> (-1.06%) ⬇️
... and 15 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4fe0ff6...dd1d9a5. Read the comment docs.

@davimacedo davimacedo requested a review from dplewis July 22, 2019 08:36
Copy link
Member

@dplewis dplewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for getting started on this! I have a quick comment.

type,
targetClass,
});
if (fieldOptions && Object.keys(fieldOptions).length > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test for setting required fields and defaults when you create a new class / new class schema?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dplewis
Copy link
Member

dplewis commented Jul 25, 2019

Are the default values validated? Can I set a number to a type String

@davimacedo davimacedo requested a review from dplewis July 25, 2019 01:23
@davimacedo
Copy link
Member Author

@dplewis It is a good question. They aren't validated. I will work on this.

@dplewis
Copy link
Member

dplewis commented Jul 25, 2019

The StorageAdapters should handle the validation for you no? Can you write a test for this?

@davimacedo
Copy link
Member Author

No. I've written a test and it is not validating. I'm fixing it and should push new commits by the end of the day.

@davimacedo
Copy link
Member Author

@dplewis it is now validating. Can you please take a look again? Note that I also fixed a bug. Currently, when the addClassIfNotExists is called, any validation error (for example, invalid class name) are returned in a wrong format (not a Parse.Error). As a result, the API is returning Internal Server Error instead of the right codes and messages.

@dplewis
Copy link
Member

dplewis commented Jul 25, 2019

How would this work with beforeSave trigger? I think you should be good as it looks like changes made in a beforeSave would override default values.

@davimacedo
Copy link
Member Author

I think that the beforeSave trigger run before the validations and default assignments. So, if you set something in beforeSave, it will stay. On the other hand, if you set a required field to null in the beforeSave trigger, it will set the default value or return an error in the case it is a required field. This is the behavior I planned and I think it's consistent. Do you agree? I will add some tests to check.

@davimacedo
Copy link
Member Author

@dplewis Added some tests for beforeSave trigger and fixed a bug. The default value was not applied if you unset in beforeSave.

@davimacedo davimacedo merged commit fd637ff into parse-community:master Jul 26, 2019
@joaovqm1
Copy link

That will help me a lot. Thanks @davimacedo.

A little suggestion for later: the strings fields should have a uppercase or lowercase option to automatically converts it. What do you think?

@davimacedo
Copy link
Member Author

@joaovq there is a lot of things that we can still do here, as regular expression validation, formatted output (I think the lowercase/uppercase scenario goes here), etc. I will probably try to address some of these features in the future. But each of these features requires a lot of work not only in the Parse Server side, but also in docs, Parse Dashboard, and SDK. So, any help here would be very appreciated. Would you be able to tackle some of these tasks?

@joaovqm1
Copy link

joaovqm1 commented Jul 26, 2019 via email

@davimacedo
Copy link
Member Author

That's nice @joaovq . There is no short deadline. Any effort that you can put to help on this is very welcome!

@joaovqm1
Copy link

joaovqm1 commented Jul 27, 2019 via email

@davimacedo
Copy link
Member Author

@joaovq

Here it goes few things that I see as next steps:

  • Add the required and defaultValue options in the dashboard when creating a new column - I think @alencarlucas is already working on this
  • Improve GraphQL server to use required and defaultValue options when generating the schema - I will probably tackle this soon
  • Modify the docs explaining about these new options
  • Include in Parse Server the possibility of changing these settings (currently it is not possible to modify a schema field)
  • Include in Parse Dashboard the option to modify a schema field

Then we should do similar steps for each new option we want to include in a schema field.

What do you think? If you agree with this plan I can open a project with these tasks so we can keep track of what we have already done.

@joaovqm1
Copy link

joaovqm1 commented Jul 30, 2019 via email

@davimacedo
Copy link
Member Author

davimacedo commented Jul 30, 2019

Here is the project: https://github.com/parse-community/parse-server/projects/7

Feel free to just do whatever task you want and send the PR, then some of the maintainers will review. But, you might want to move the task you are working on, so it will avoid other developer working in the same issue.

Thanks for your help!

@joaovqm1
Copy link

joaovqm1 commented Jul 31, 2019 via email

@davimacedo
Copy link
Member Author

Sure. That's a good way to get started. Also let me know if you have any question.

@joaovqm1
Copy link

joaovqm1 commented Jul 31, 2019 via email

@JeromeDeLeon
Copy link
Contributor

Just wanted to ask if this change will be included in the parse-dashboard?

@alencarlucas
Copy link
Contributor

@JeromeDeLeon It's already included, take a look in this PR.

@JeromeDeLeon
Copy link
Contributor

How about parse-js-sdk?

@davimacedo
Copy link
Member Author

What do you have in mind for the JS SDK?

@JeromeDeLeon
Copy link
Contributor

Nah, nvm. I just thought of it but then I realized that using parse-dashboard is fine.

@santiagosemhan
Copy link

Is this feature documented yet?

dplewis added a commit to parse-community/Parse-SDK-JS that referenced this pull request Oct 16, 2019
Closes: #930

Requires Parse Server 3.7.0+

Feature: parse-community/parse-server#5835

I added a FieldOption to addField and its counter parts. This will allow for future options like uppercase / lowercase for example.

I keep getting a schema mismatch when I use dates and pointers. @davimacedo Maybe you know why?
dplewis added a commit to parse-community/Parse-SDK-JS that referenced this pull request Oct 16, 2019
* Parse.Schema required fields and defaultValues

Closes: #930

Requires Parse Server 3.7.0+

Feature: parse-community/parse-server#5835

I added a FieldOption to addField and its counter parts. This will allow for future options like uppercase / lowercase for example.

I keep getting a schema mismatch when I use dates and pointers. @davimacedo Maybe you know why?

* improve coverage

* fix date and pointer fields

* print tests

* fix utc date test

* Documentation

* nit test
UnderratedDev pushed a commit to UnderratedDev/parse-server that referenced this pull request Mar 21, 2020
* Add field options to mongo schema metadata

* Add/fix test with fields options

* Add required validation failing test

* Add more tests

* Only set default value if field is undefined

* Fix redis test

* Fix tests

* Test for creating a new class with field options

* Validate default value type

* fix lint (weird)

* Fix lint another way

* Add tests for beforeSave trigger and solve small issue regarding the use of unset in the beforeSave trigger
@rasool1994
Copy link

and who can I remove the required flag from the field without data loss?

@bahaa-kallas
Copy link

@rasool1994 You can remove it from the database without any data loss. Just connect to your mongoDB instance and go to SCHEMA collection see example below:

 "_id" : "PostCategory", 
    "objectId" : "string", 
    "updatedAt" : "date", 
    "createdAt" : "date", 
    "_metadata" : {
        "fields_options" : {
            "arName" : {
                "required" : true
            }, 
            "enName" : {
                "required" : true
            }, 
            "channelName" : {
                "required" : false
            }
        }, 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request : default values and required for object fields
8 participants