<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: CAP NodeJS: @mandatory does not work for Function Imports / Actions in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/cap-nodejs-mandatory-does-not-work-for-function-imports-actions/qaa-p/12226171#M4576539</link>
    <description>&lt;P&gt;Hi Rafa, thanks for the reply!&lt;BR /&gt;&lt;BR /&gt;Something that we did in our side is the following, we created an Argument validator which checks if we have any property with mandatory and it's value. We call this under before("*") event for each Service that we expose. The only thing tha, in case you want to reproduce this in your side, it's that we are not validating trimmed strings (e.g.: "  ").&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;class ArgumentValidator {


  /**
   * Validates arguments with @mandatory annotation for actions and functions on .cds files
   */
  static validateArguments(data, event, serviceName) {
    services[serviceName] = services[serviceName] || cds.connect.to(serviceName);
    const eventDefinitions = services[serviceName].model.definitions[event];


    // Skip validation for events with no definitions (e.g. "READ")
    if (!eventDefinitions) {
      return;
    }


    for (const parameterName in eventDefinitions.params) {
      validateArgument(data[parameterName], eventDefinitions.params[parameterName], parameterName);
    }
  }


}


function validateArgument(providedArgument, parameterDefinition, parameterName) {
  if (parameterDefinition["@mandatory"]) {
    const isValid = providedArgument !== null &amp;amp;&amp;amp; providedArgument !== undefined &amp;amp;&amp;amp; providedArgument !== "";
    ErrorHandler.validate(isValid, ServiceError.ARGUMENT_MISSING, parameterName);
  }
}


module.exports = ArgumentValidator;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 26 Nov 2020 17:08:59 GMT</pubDate>
    <dc:creator>former_member207903</dc:creator>
    <dc:date>2020-11-26T17:08:59Z</dc:date>
    <item>
      <title>CAP NodeJS: @mandatory does not work for Function Imports / Actions</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-nodejs-mandatory-does-not-work-for-function-imports-actions/qaq-p/12226167</link>
      <description>&lt;P&gt;Hello colleagues!&lt;/P&gt;
  &lt;P&gt;&lt;BR /&gt;We are running a project on top NodeJS using CAP. We noticed a few weeks ago that the annotation @mandatory in the .cds file does not work for Function Modules.&lt;/P&gt;
  &lt;P&gt;I tested with the following annotations:&lt;BR /&gt;&lt;BR /&gt;- @mandatory: &lt;EM&gt;&lt;A href="https://cap.cloud.sap/docs/guides/generic#mandatory" target="test_blank"&gt;https://cap.cloud.sap/docs/guides/generic#mandatory&lt;/A&gt;&lt;/EM&gt;&lt;BR /&gt;- @FieldControl.Mandatory: &lt;EM&gt;&lt;A href="https://cap.cloud.sap/docs/guides/generic#mandatory"&gt;https://cap.cloud.sap/docs/guides/generic#mandatory&lt;/A&gt;&lt;/EM&gt;&lt;BR /&gt;- @Common.FieldControl.Mandatory: &lt;/P&gt;
  &lt;P&gt;None of them worked, so I'm not sure if they are supposed only to work with OData v2 or v4 API calls.&lt;/P&gt;
  &lt;P&gt;Here's a action in CDS as example:&lt;/P&gt;
  &lt;P&gt;&lt;EM&gt; action addGoal( &lt;BR /&gt; description : String(200) @mandatory, &lt;BR /&gt; targetCompletionDate : Timestamp @mandatory, &lt;BR /&gt; isPrivate : Boolean @mandatory );&lt;/EM&gt;&lt;/P&gt;
  &lt;P&gt;In the example, we can add not null after the word Timestamp, and it becomes required. But for Strings as in description, adding not null wouldn't be enough to detect a blank string.&lt;BR /&gt;&lt;BR /&gt;Do you know how to enable these annotations to work or if it's a known bug for CAP?&lt;/P&gt;
  &lt;P&gt;&lt;BR /&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 25 Nov 2020 17:39:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-nodejs-mandatory-does-not-work-for-function-imports-actions/qaq-p/12226167</guid>
      <dc:creator>former_member207903</dc:creator>
      <dc:date>2020-11-25T17:39:58Z</dc:date>
    </item>
    <item>
      <title>Re: CAP NodeJS: @mandatory does not work for Function Imports / Actions</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-nodejs-mandatory-does-not-work-for-function-imports-actions/qaa-p/12226168#M4576536</link>
      <description>&lt;P&gt;[Update] &lt;BR /&gt;&lt;BR /&gt;I also tried using Annotations with the type, but this also &lt;STRONG&gt;didn't work&lt;/STRONG&gt; for my case.&lt;/P&gt;&lt;P&gt;Example below&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;define type AdditionGoal {
  description          : String(200);
  targetCompletionDate : Timestamp;
  isPrivate            : Boolean;
}// Input validation
annotate AdditionGoal with {
  description             @mandatory;
  targetCompletionDate    @mandatory;
  isPrivate               @mandatory;
}
action addGoal(
    goal : AdditionGoal
  );&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I passed to the backend an entity goal with the description property empty and CAP allowed it.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Nov 2020 19:09:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-nodejs-mandatory-does-not-work-for-function-imports-actions/qaa-p/12226168#M4576536</guid>
      <dc:creator>former_member207903</dc:creator>
      <dc:date>2020-11-25T19:09:52Z</dc:date>
    </item>
    <item>
      <title>Re: CAP NodeJS: @mandatory does not work for Function Imports / Actions</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-nodejs-mandatory-does-not-work-for-function-imports-actions/qaa-p/12226169#M4576537</link>
      <description>&lt;P&gt;Hi Igor, have you tried?&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;annotate AdditionGoal with {
  description @Common.FieldControl: #Mandatory
}
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Nov 2020 16:14:05 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-nodejs-mandatory-does-not-work-for-function-imports-actions/qaa-p/12226169#M4576537</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2020-11-26T16:14:05Z</dc:date>
    </item>
    <item>
      <title>Re: CAP NodeJS: @mandatory does not work for Function Imports / Actions</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-nodejs-mandatory-does-not-work-for-function-imports-actions/qaa-p/12226170#M4576538</link>
      <description>&lt;P&gt;In fact, i  use something like&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;annotate DhcpAdminSvc.NetworkHosts with {
  device @( 
    Common: {
      Label:'{i18n&amp;gt;Device Name}',
      Text: device.name,
      TextArrangement : #TextOnly,
      FieldControl: #Mandatory
    },
    ValueList.entity: 'Devices'
  );
}
&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I hope it helps you.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Nov 2020 16:20:40 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-nodejs-mandatory-does-not-work-for-function-imports-actions/qaa-p/12226170#M4576538</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2020-11-26T16:20:40Z</dc:date>
    </item>
    <item>
      <title>Re: CAP NodeJS: @mandatory does not work for Function Imports / Actions</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-nodejs-mandatory-does-not-work-for-function-imports-actions/qaa-p/12226171#M4576539</link>
      <description>&lt;P&gt;Hi Rafa, thanks for the reply!&lt;BR /&gt;&lt;BR /&gt;Something that we did in our side is the following, we created an Argument validator which checks if we have any property with mandatory and it's value. We call this under before("*") event for each Service that we expose. The only thing tha, in case you want to reproduce this in your side, it's that we are not validating trimmed strings (e.g.: "  ").&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;class ArgumentValidator {


  /**
   * Validates arguments with @mandatory annotation for actions and functions on .cds files
   */
  static validateArguments(data, event, serviceName) {
    services[serviceName] = services[serviceName] || cds.connect.to(serviceName);
    const eventDefinitions = services[serviceName].model.definitions[event];


    // Skip validation for events with no definitions (e.g. "READ")
    if (!eventDefinitions) {
      return;
    }


    for (const parameterName in eventDefinitions.params) {
      validateArgument(data[parameterName], eventDefinitions.params[parameterName], parameterName);
    }
  }


}


function validateArgument(providedArgument, parameterDefinition, parameterName) {
  if (parameterDefinition["@mandatory"]) {
    const isValid = providedArgument !== null &amp;amp;&amp;amp; providedArgument !== undefined &amp;amp;&amp;amp; providedArgument !== "";
    ErrorHandler.validate(isValid, ServiceError.ARGUMENT_MISSING, parameterName);
  }
}


module.exports = ArgumentValidator;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Nov 2020 17:08:59 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-nodejs-mandatory-does-not-work-for-function-imports-actions/qaa-p/12226171#M4576539</guid>
      <dc:creator>former_member207903</dc:creator>
      <dc:date>2020-11-26T17:08:59Z</dc:date>
    </item>
    <item>
      <title>Re: CAP NodeJS: @mandatory does not work for Function Imports / Actions</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-nodejs-mandatory-does-not-work-for-function-imports-actions/qaa-p/12226172#M4576540</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;With the next CAP release the @mandatory check will be also applied for actions and functions having typed parameters. Also the String parameters annotated with @mandatory will be checked for being empty strings. You can follow CAP releases at &lt;A href="https://cap.cloud.sap/docs/releases/"&gt;https://cap.cloud.sap/docs/releases/&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;Vitaly&lt;/P&gt;</description>
      <pubDate>Tue, 22 Dec 2020 11:00:40 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-nodejs-mandatory-does-not-work-for-function-imports-actions/qaa-p/12226172#M4576540</guid>
      <dc:creator>vitaly_kozyura</dc:creator>
      <dc:date>2020-12-22T11:00:40Z</dc:date>
    </item>
  </channel>
</rss>

