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

Question - .NET Schema server side validation #8639

Closed
stephenstroud opened this issue Aug 1, 2024 · 1 comment
Closed

Question - .NET Schema server side validation #8639

stephenstroud opened this issue Aug 1, 2024 · 1 comment
Labels

Comments

@stephenstroud
Copy link

Hi, I wanted to validate the survey schema once received through my .NET API I've copied the reference code below. Is there a .NET flavour for this, if not I will create one.

import { Model } from "survey-core";

const surveyJson = { ... };
const survey = new Model(surveyJson);

survey.data = initialSurveyResultJson;
survey.clearIncorrectValues(true);

const correctSurveyResultJson = survey.data;
@andrewtelnov
Copy link
Member

andrewtelnov commented Aug 1, 2024

@stephenstroud It would take several lines of code to do it in NodeJS and would require some coding for .Net (C#).
I would do the following.
Together with saving JSON definition that you get from Creator, I would store the data validation JSON with the following format:
{ questionName/questionValueName : { type: "string"/"number"/"date"/"Array"/..., isRequired: boolean }
So you will need to run through all your questions:

function getQuestionCodeType(q) {
 if(q.getType() === "text") {
   if(q.inputType === "number") return "number";
   if(q.inputType === "date") return "date";
  }
//Put more logic here
 return "string";
}

const dataValidationJSON = {}
creator.survey.getAllQuestions().forEach( q => {
 dataValidationJSON[q.getValueName()] = { type: getQuestionCodeType(q), isRequired: q.isRequired }
});

Having this JSON on the server, you will be able to validate user responses .

You need it if you create a service where non authorized client can send the data to your service or from any client.

Thank you,
Andrew

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

No branches or pull requests

3 participants