validate duplicate item in content type

5 posts, 0 answers
  1. Trevor
    Trevor avatar
    4 posts
    Member since:
    Oct 2014

    Posted 26 Sep 2015 Link to this post

    Hi,

     I was wondering if there is any example of validating duplicate items in a content type on the backend services?  I'm a beginner so I was looking for something like the validation of durplicate user in the authentication examples.  Unfortunately it's not it's not so clear to me how this is achieved.

    Thanks,
    Trev

  2. Anton Dobrev
    Admin
    Anton Dobrev avatar
    539 posts

    Posted 30 Sep 2015 Link to this post

    Hello Trev,

    Thanks for posting to the Telerik Developer Forums.

    The server validation will not accept a new user account with the same username at the time of making a new user account registration. Same applies to the "Email" field if it is specified. 

    If you'd like to show the user that the username chosen by the user is already picked up by another account, you may need to make a call to the Users type / or to a Cloud function (preferably the latter) that can verify if the entered username is not in use by another user (basically check if querying for user with this username will return any results).

    In regard to any other item in a content type - uniqueness cannot be automatically enforced and you need to implement your own logic in the Business Logic layer that checks for this.

    Let me know if this helps and if you have questions.

    Regards,
    Anton Dobrev
    Telerik
     
    Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
     
  3. Trevor
    Trevor avatar
    4 posts
    Member since:
    Oct 2014

    Posted 01 Oct 2015 in reply to Anton Dobrev Link to this post

    Thanks Anton,

    You've answered my question elsewhere.

    Best,
    Trevor

  4. Anton Dobrev
    Admin
    Anton Dobrev avatar
    539 posts

    Posted 10 Dec 2015 Link to this post

    Just a quick follow up with some information on the subject.

    There is a feature request in our feedback portal to add support for this use case on the server by allowing the developer to define unique fields in a type’s structure. Thus the uniqueness of this field will be preserved with an out-of-the-box and more efficient solution. If you have any comments or suggestions or just would like to vote for the request you can do this here.

    As discussed in this thread before, for now the solution may be to implement your own validation logic. Here is a simple example that can be used to implement such with Cloud Code for Data. In the beforeCreate event of the Cloud Code for the given content type you can add a query based on the specified field value. In the example to follow we check the count of items in "MyType" that have a field "TitleField" with the specified value. If there are no items that match the query, the request is allowed to continue with the create operation. Otherwise it is rejected.

    For instance:

    Everlive.Events.beforeCreate(function(request, context, done) {
     
        var checkedFieldName = "TitleField";
        var checkedTypeName = "MyType";
     
        if (request.data && request.data[checkedFieldName] && !Array.isArray(request.data)) {
            var incomingData = request.data;
            var titleValue = incomingData[checkedFieldName];
     
            var data = Everlive.Sdk.withMasterKey().data(checkedTypeName); // we use master key (administrator) access to read the MyType data type
     
            var query = new Everlive.Sdk.Query();
            query.where().eq(checkedFieldName, titleValue);
     
            data.count(query).then(
                function(count) {
                    if (count.result > 0) {
                        // return the error to the client app with custom message, custom error code and HTTP status code
                        Everlive.Response.setErrorResult('An item with the same title field already exists.', 100310, 400);
                        done();
                    } else {
                        // do nothing - continue with the create operation
                        done();
                    }
                },
                function(err) {
                    // cannot retrieve count for the items
                    console.log(JSON.stringify(err));
                     
                    // perhaps deny the request
                    Everlive.Response.setErrorResult('We cannot proceed with your request.', 100311, 400);
                    done();
                }
            );
        } else {
            // the request is trying to create multiple items or the requested field is not specified
            // return the error to the client app with custom message, custom error code and HTTP status code
            Everlive.Response.setErrorResult('Please revise the sent parameters', 100312, 400);
            done();
        }
    });


    Regards,
    Anton Dobrev
    Telerik
     
    Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
     
  5. Anton Dobrev
    Admin
    Anton Dobrev avatar
    539 posts

    Posted 14 Jul Link to this post


    I am happy to announce that we just introduced server support for defining unique indexes on database level. This ability allows developers to enforce uniqueness on the given field values when inserting them. You can read more in the respective Data section of the documentation - http://docs.telerik.com/platform/backend-services/

    See Getting Started with Data and Creating Data Items.


    Regards,
    Anton Dobrev
    Telerik by Progress
     
    Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
     
Back to Top