-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(templates): add Validation Rule
- Loading branch information
1 parent
f7b348b
commit 0f60cbf
Showing
9 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/templates/validationRule/.template.config/dotnetcli.host.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/dotnetcli.host", | ||
"usageExamples": [], | ||
"symbolInfo": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/ide.host", | ||
"icon": "../../images/icon.png", | ||
"defaultItemExtension": "cs", | ||
"itemHierarchyPaths": ["Xperience by Kentico"] | ||
} |
86 changes: 86 additions & 0 deletions
86
src/templates/validationRule/.template.config/template.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/template", | ||
"author": "Xperience Community", | ||
"classifications": ["Web", "ASP.NET"], | ||
"name": "Xperience Validation Rule", | ||
"generatorVersions": "[1.0.0.0-*)", | ||
"description": "Xperience by Kentico Validation Rule with Properties, Client Properties, and Attribute", | ||
"tags": { | ||
"language": "C#", | ||
"type": "item" | ||
}, | ||
"groupIdentity": "XperinceCommunity.Admin.ValidationRule", | ||
"precedence": "10", | ||
"identity": "XperienceCommunity.Admin.ValidationRule.26.3.0.0", | ||
"shortName": "xpc-validation-rule", | ||
"sourceName": "NewValidationRule", | ||
"primaryOutputs": [ | ||
{ | ||
"path": "NewValidationRule.cs" | ||
} | ||
], | ||
"defaultName": "NewValidationRule", | ||
"preferNameDirectory": false, | ||
"preferDefaultName": true, | ||
"symbols": { | ||
"namespace": { | ||
"type": "generated", | ||
"generator": "coalesce", | ||
"replaces": "MySite.Admin.Validation", | ||
"parameters": { | ||
"sourceVariableName": "IDENamespace", | ||
"fallbackVariableName": "RootNamespace", | ||
"defaultValue": "MySite.Admin.Validation" | ||
} | ||
}, | ||
"IDENamespace": { | ||
"type": "bind", | ||
"binding": "host:namespace", | ||
"defaultValue": "" | ||
}, | ||
"RootNamespace": { | ||
"type": "bind", | ||
"binding": "msbuild:RootNamespace", | ||
"defaultValue": "" | ||
}, | ||
"HostIdentifier": { | ||
"type": "bind", | ||
"binding": "HostIdentifier" | ||
}, | ||
"DisplayName": { | ||
"type": "derived", | ||
"valueSource": "name", | ||
"replaces": "DisplayName", | ||
"valueTransform": "nameToWords" | ||
} | ||
}, | ||
"forms": { | ||
"extractName": { | ||
"identifier": "replace", | ||
"pattern": "^(\\w+)ValidationRule$", | ||
"replacement": "$1" | ||
}, | ||
"pascalToWords": { | ||
"identifier": "replace", | ||
"pattern": "(?<!^)(?=[A-Z])", | ||
"replacement": " " | ||
}, | ||
"nameToWords": { | ||
"identifier": "chain", | ||
"steps": ["extractName", "pascalToWords"] | ||
} | ||
}, | ||
"postActions": [ | ||
{ | ||
"id": "openInEditor", | ||
"condition": "(HostIdentifier != \"dotnetcli\" && HostIdentifier != \"dotnetcli-preview\")", | ||
"description": "Opens the created Validation Rule in the editor", | ||
"manualInstructions": [], | ||
"actionId": "84C0DA21-51C8-4541-9940-6CA19AF04EE6", | ||
"args": { | ||
"files": "0" | ||
}, | ||
"continueOnError": true | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using Kentico.Xperience.Admin.Base.Forms; | ||
using Kentico.Xperience.Admin.Base.FormAnnotations; | ||
using MySite.Admin.Validation; | ||
using System.Threading.Tasks; | ||
using CMS.Core; | ||
using System; | ||
|
||
[assembly: RegisterFormValidationRule( | ||
identifier: NewValidationRule.IDENTIFIER, | ||
ruleType: typeof(NewValidationRule), | ||
name: "DisplayName", | ||
description: "A DisplayName Validation Rule.")] | ||
|
||
namespace MySite.Admin.Validation; | ||
|
||
[ValidationRuleAttribute(typeof(NewValidationRuleAttribute))] | ||
public class NewValidationRule : ValidationRule<NewValidationRuleProperties, NewValidationRuleClientProperties, string> | ||
{ | ||
public const string IDENTIFIER = "MySite.Admin.Validation.NewValidationRule"; | ||
|
||
private readonly ILocalizationService localizer; | ||
|
||
public override string ClientRuleName => "@MySite.Admin/NewValidationRule"; | ||
|
||
public NewValidationRule(ILocalizationService localizer) | ||
{ | ||
this.localizer = localizer; | ||
} | ||
|
||
public override Task<ValidationResult> Validate(string value, IFormFieldValueProvider formFieldValueProvider) | ||
{ | ||
if (Random.Shared.Next() > 10) | ||
{ | ||
return ValidationResult.FailResult(localizer.LocalizeString("Please enter a valid value.")); | ||
} | ||
|
||
return ValidationResult.SuccessResult(); | ||
} | ||
|
||
protected override Task ConfigureClientProperties(NewValidationRuleClientProperties clientProperties) | ||
{ | ||
return base.ConfigureClientProperties(clientProperties); | ||
} | ||
} | ||
|
||
public class NewValidationRuleProperties : ValidationRuleProperties | ||
{ | ||
[TextInputComponent( | ||
Label = "Value", | ||
ExplanationText = "A value to validate against", | ||
Tooltip = "Enter a value", | ||
Order = 1)] | ||
public string Value { get; set; } | ||
|
||
public override string GetDescriptionText(ILocalizationService localizationService) | ||
{ | ||
return ""; | ||
} | ||
} | ||
|
||
public class NewValidationRuleClientProperties : ValidationRuleClientProperties | ||
{ | ||
public string Value { get; set; } | ||
} | ||
|
||
public class NewValidationRuleAttribute : ValidationRuleAttribute | ||
{ | ||
public NewValidationRuleAttribute(string value) | ||
{ | ||
Value = value; | ||
} | ||
|
||
public string Value { get; set; } | ||
} |
5 changes: 5 additions & 0 deletions
5
src/templates/validationRuleClient/.template.config/dotnetcli.host.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/dotnetcli.host", | ||
"usageExamples": [], | ||
"symbolInfo": {} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/templates/validationRuleClient/.template.config/ide.host.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/ide.host", | ||
"icon": "../../images/icon.png", | ||
"defaultItemExtension": "cs", | ||
"itemHierarchyPaths": ["Xperience by Kentico"] | ||
} |
60 changes: 60 additions & 0 deletions
60
src/templates/validationRuleClient/.template.config/template.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/template", | ||
"author": "Xperience Community", | ||
"classifications": ["Web", "TypeScript"], | ||
"name": "Xperience Validation Rule (TypeScript)", | ||
"generatorVersions": "[1.0.0.0-*)", | ||
"description": "Xperience by Kentico Validation Rule Front end validation function in TypeScript", | ||
"tags": { | ||
"language": "TypeScript", | ||
"type": "item" | ||
}, | ||
"groupIdentity": "XperinceCommunity.Admin.ValidationRule", | ||
"precedence": "10", | ||
"identity": "XperienceCommunity.Admin.ValidationRuleClient.26.3.0.0", | ||
"shortName": "xpc-validation-rule-client", | ||
"sourceName": "NewValidationRule", | ||
"primaryOutputs": [ | ||
{ | ||
"path": "NewValidationRule.ts" | ||
} | ||
], | ||
"defaultName": "NewValidationRule", | ||
"preferNameDirectory": false, | ||
"preferDefaultName": true, | ||
"symbols": { | ||
"HostIdentifier": { | ||
"type": "bind", | ||
"binding": "HostIdentifier" | ||
} | ||
}, | ||
"forms": { | ||
"extractName": { | ||
"identifier": "replace", | ||
"pattern": "^(\\w+)ValidationRule$", | ||
"replacement": "$1" | ||
}, | ||
"pascalToWords": { | ||
"identifier": "replace", | ||
"pattern": "(?<!^)(?=[A-Z])", | ||
"replacement": " " | ||
}, | ||
"nameToWords": { | ||
"identifier": "chain", | ||
"steps": ["extractName", "pascalToWords"] | ||
} | ||
}, | ||
"postActions": [ | ||
{ | ||
"id": "openInEditor", | ||
"condition": "(HostIdentifier != \"dotnetcli\" && HostIdentifier != \"dotnetcli-preview\")", | ||
"description": "Opens the created Validation Rule in the editor", | ||
"manualInstructions": [], | ||
"actionId": "84C0DA21-51C8-4541-9940-6CA19AF04EE6", | ||
"args": { | ||
"files": "0" | ||
}, | ||
"continueOnError": true | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { | ||
ValidationRule, | ||
ValidationRuleProps, | ||
} from "@kentico/xperience-admin-base"; | ||
|
||
interface NewValidationRuleProps extends ValidationRuleProps { | ||
readonly value: string; | ||
} | ||
|
||
export const NewValidationRule: ValidationRule< | ||
NewValidationRuleProps, | ||
string | ||
> = (props, value) => { | ||
return { isValid: false, errorMessage: props.errorMessage }; | ||
}; |