-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fixed collection validations cleanup and
not
helper
- Loading branch information
1 parent
8d2264f
commit ead7bde
Showing
15 changed files
with
219 additions
and
62 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
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
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
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
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
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
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
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
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,61 @@ | ||
import { | ||
FormRuleDeclaration, | ||
RegleRuleDefinition, | ||
RegleRuleDefinitionProcessor, | ||
createRule, | ||
} from '@regle/core'; | ||
import { ruleHelpers } from './rulesHelpers'; | ||
|
||
export function not<TValue, TParams extends any[] = any[]>( | ||
rule: FormRuleDeclaration<TValue, TParams>, | ||
message: string | RegleRuleDefinitionProcessor<TValue, TParams, string> | ||
): RegleRuleDefinition<TValue> { | ||
let _type = 'not'; | ||
let _active: boolean | RegleRuleDefinitionProcessor<any, [], boolean> | undefined; | ||
let _params: any[] | undefined = []; | ||
let _async = false; | ||
|
||
if (typeof rule === 'function') { | ||
_async = rule.constructor.name === 'AsyncFunction'; | ||
} else { | ||
({ _type, _params, _async } = rule); | ||
} | ||
|
||
const validator = (() => { | ||
if (_async) { | ||
return async (value: any, ...params: any[]) => { | ||
if (ruleHelpers.isFilled(value)) { | ||
if (typeof rule === 'function') { | ||
const result = await rule(value); | ||
return !result; | ||
} else { | ||
const result = await rule._validator(value, ...(params as any)); | ||
return !result; | ||
} | ||
} | ||
return true; | ||
}; | ||
} else { | ||
return (value: any, ...params: any[]) => { | ||
if (ruleHelpers.isFilled(value)) { | ||
if (typeof rule === 'function') { | ||
return !rule(value); | ||
} else { | ||
return !rule._validator(value, ...(params as any)); | ||
} | ||
} | ||
return true; | ||
}; | ||
} | ||
})(); | ||
|
||
const newRule = createRule({ | ||
type: 'not', | ||
validator: validator, | ||
message, | ||
}); | ||
|
||
newRule._params = _params as any; | ||
|
||
return newRule as RegleRuleDefinition; | ||
} |
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,66 @@ | ||
import { | ||
createRule, | ||
FormRuleDeclaration, | ||
RegleRuleDefinition, | ||
RegleRuleDefinitionProcessor, | ||
} from '@regle/core'; | ||
import { minValue } from 'validators/minValue'; | ||
import { ExtractValueFormRules } from '../types'; | ||
import { maxLength } from '../validators'; | ||
|
||
export function or<TRules extends FormRuleDeclaration<any, any>[]>( | ||
...rules: [...TRules] | ||
): RegleRuleDefinition<ExtractValueFormRules<TRules>[number]> { | ||
const isAnyRuleAsync = rules.some((rule) => { | ||
if (typeof rule === 'function') { | ||
return rule.constructor.name === 'AsyncFunction'; | ||
} else { | ||
return rule._async; | ||
} | ||
}); | ||
|
||
const params = rules | ||
.map((rule) => { | ||
if (typeof rule === 'function') { | ||
return null; | ||
} else { | ||
return rule._params; | ||
} | ||
}) | ||
.filter((param): param is any => !!param); | ||
|
||
const validator = isAnyRuleAsync | ||
? async (value: any | null | undefined, ...params: any[]) => { | ||
const results = await Promise.all( | ||
rules.map((rule) => { | ||
if (typeof rule === 'function') { | ||
return rule(value); | ||
} else { | ||
return rule.validator(value, ...params); | ||
} | ||
}) | ||
); | ||
return results.some((result) => !!result); | ||
} | ||
: (value: any | null | undefined, ...params: any[]) => { | ||
return rules | ||
.map((rule) => { | ||
if (typeof rule === 'function') { | ||
return rule(value); | ||
} else { | ||
return rule.validator(value, ...params); | ||
} | ||
}) | ||
.some((result) => !!result); | ||
}; | ||
|
||
const newRule = createRule({ | ||
type: 'or', | ||
validator: validator, | ||
message: '', | ||
}); | ||
|
||
newRule._params = params; | ||
|
||
return newRule as RegleRuleDefinition; | ||
} |
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
Oops, something went wrong.