Skip to content

Commit

Permalink
feat(ts/use-validation): Greatly improve the 'rules' prop type (fix: #…
Browse files Browse the repository at this point in the history
…11043) (#11597)

* feat(ts&api): Allow tsType for Array type

* feat(ts/use-validation): Greatly improve the 'rules' prop type
  • Loading branch information
yusufkandemir authored Dec 8, 2021
1 parent 107190e commit 9579815
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ui/build/build.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const objectTypes = {
},

Array: {
props: [ 'tsInjectionPoint', 'desc', 'required', 'reactive', 'sync', 'link', 'values', 'default', 'definition', 'examples', 'category', 'addedIn', 'internal' ],
props: [ 'tsInjectionPoint', 'tsType', 'desc', 'required', 'reactive', 'sync', 'link', 'values', 'default', 'definition', 'examples', 'category', 'addedIn', 'internal' ],
required: [ 'desc', 'examples' ],
isBoolean: [ 'tsInjectionPoint', 'required', 'reactive', 'sync', 'internal' ],
isObject: [ 'definition' ],
Expand Down
2 changes: 1 addition & 1 deletion ui/build/build.types.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const dontNarrowValues = [

function convertTypeVal (type, def) {
if (def.tsType !== void 0) {
return def.tsType
return `${ def.tsType }${ def.type === 'Array' ? '[]' : '' }`
}

if (def.values && type === 'String') {
Expand Down
1 change: 1 addition & 0 deletions ui/src/composables/private/use-validate.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

"rules": {
"type": "Array",
"tsType": "ValidationRule",
"desc": "Array of Functions/Strings; If String, then it must be a name of one of the embedded validation rules",
"examples": [
":rules=\"[ val => val.length <= 3 || 'Please use maximum 3 characters' ]\"",
Expand Down
1 change: 1 addition & 0 deletions ui/src/utils/patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const
rgb = /^rgb\(((0|[1-9][\d]?|1[\d]{0,2}|2[\d]?|2[0-4][\d]|25[0-5]),){2}(0|[1-9][\d]?|1[\d]{0,2}|2[\d]?|2[0-4][\d]|25[0-5])\)$/,
rgba = /^rgba\(((0|[1-9][\d]?|1[\d]{0,2}|2[\d]?|2[0-4][\d]|25[0-5]),){2}(0|[1-9][\d]?|1[\d]{0,2}|2[\d]?|2[0-4][\d]|25[0-5]),(0|0\.[0-9]+[1-9]|0\.[1-9]+|1)\)$/

// Keep in sync with ui/types/api/validation.d.ts
export const testPattern = {
date: v => /^-?[\d]+\/[0-1]\d\/[0-3]\d$/.test(v),
time: v => /^([0-1]?\d|2[0-3]):[0-5]\d$/.test(v),
Expand Down
1 change: 1 addition & 0 deletions ui/types/api.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./api/cookies";
export * from "./api/web-storage";
export * from "./api/validation";
4 changes: 4 additions & 0 deletions ui/types/api/validation.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Keep in sync with ui/src/utils/patterns.js
export type EmbeddedValidationRule = "date" | "time" | "fulltime" | "timeOrFulltime" | "hexColor" | "hexaColor" | "hexOrHexaColor" | "rgbColor" | "rgbaColor" | "rgbOrRgbaColor" | "hexOrRgbColor" | "hexaOrRgbaColor" | "anyColor";

export type ValidationRule<T = any> = EmbeddedValidationRule | ((value: T) => boolean | string);

0 comments on commit 9579815

Please sign in to comment.