2525 *
2626 */
2727
28- import { VALUE_SCHEMA_BUILDER_FLAG } from ' ../consts' ;
29- import { forEachObject , isFunction , isPlainObject } from ' ../utils' ;
30- import { markRaw } from ' ../utils/markRaw' ;
28+ import { VALUE_SCHEMA_BUILDER_FLAG } from " ../consts" ;
29+ import { forEachObject , isFunction , isPlainObject } from " ../utils" ;
30+ import { markRaw } from " ../utils/markRaw" ;
3131
32- import type {
33- SchemaBuilder ,
34- SchemaDescriptorBuilder ,
35- SchemaOptions ,
36- SchemaValidator ,
37- } from './types' ;
32+ import type { SchemaBuilder , SchemaDescriptorBuilder , SchemaOptions , SchemaValidator } from "./types" ;
3833
3934type SchemaArgs = {
40- value : any ;
41- options : SchemaOptions ;
42- validator : SchemaValidator ;
35+ value : any ;
36+ options : SchemaOptions ;
37+ validator : SchemaValidator ;
4338} ;
4439
4540/**
4641 * 将options里面的on和render开头的函数标识为raw
47- * @param options
42+ * @param options
4843 */
4944function markRawOptions ( options : SchemaOptions ) {
50- if ( isPlainObject ( options ) ) {
51- forEachObject ( options , ( { value, key, parent } ) => {
52- if (
53- ( isFunction ( value ) &&
54- ( key . startsWith ( 'on' ) || key . startsWith ( 'render' ) || key . startsWith ( 'to' ) ) )
55- ) {
56- // @ts -ignore
57- parent [ key ] = markRaw ( value ) ;
58- }
59- } ) ;
60- }
45+ if ( isPlainObject ( options ) ) {
46+ forEachObject ( options , ( { value, key, parent } ) => {
47+ if ( isFunction ( value ) && ( key . startsWith ( "on" ) || key . startsWith ( "render" ) || key . startsWith ( "to" ) ) ) {
48+ // @ts -expect-error
49+ parent [ key ] = markRaw ( value ) ;
50+ }
51+ } ) ;
52+ }
6153}
6254
6355/**
@@ -69,83 +61,82 @@ function markRawOptions(options: SchemaOptions) {
6961 * 参数解析规则:
7062 */
7163function parseSchemaOptions ( args : any [ ] ) : SchemaArgs {
72- const finalArgs : any = {
73- value : args [ 0 ] ,
74- options : Object . assign (
75- {
76- onFail : ' throw-pass' ,
77- } ,
78- args [ 1 ] ,
79- ) ,
80- } ;
81- // 自动推断widget类型,但是如果指定了toInput参数,则不需要自动推断
82- // 因为无法判断toInput对值进行如何转换,所以不需要自动推断widget类型
83- if ( ! finalArgs . options . widget && typeof finalArgs . options . toInput !== ' function' ) {
84- const datatype = typeof finalArgs . value ;
85- if ( datatype === ' boolean' ) {
86- finalArgs . options . widget = ' checkbox' ;
87- } else if ( datatype === ' number' ) {
88- finalArgs . options . widget = ' number' ;
89- }
90- if ( Array . isArray ( finalArgs . options . select ) ) {
91- finalArgs . options . widget = ' select' ;
92- }
93- }
94- if ( ' onValidate' in finalArgs . options ) {
95- markRaw ( finalArgs . options . onValidate ) ;
96- }
97- markRawOptions ( finalArgs . options ) ;
98- return finalArgs as SchemaArgs ;
64+ const finalArgs : any = {
65+ value : args [ 0 ] ,
66+ options : Object . assign (
67+ {
68+ onFail : " throw-pass" ,
69+ } ,
70+ args [ 1 ] ,
71+ ) ,
72+ } ;
73+ // 自动推断widget类型,但是如果指定了toInput参数,则不需要自动推断
74+ // 因为无法判断toInput对值进行如何转换,所以不需要自动推断widget类型
75+ if ( ! finalArgs . options . widget && typeof finalArgs . options . toInput !== " function" ) {
76+ const datatype = typeof finalArgs . value ;
77+ if ( datatype === " boolean" ) {
78+ finalArgs . options . widget = " checkbox" ;
79+ } else if ( datatype === " number" ) {
80+ finalArgs . options . widget = " number" ;
81+ }
82+ if ( Array . isArray ( finalArgs . options . select ) ) {
83+ finalArgs . options . widget = " select" ;
84+ }
85+ }
86+ if ( " onValidate" in finalArgs . options ) {
87+ markRaw ( finalArgs . options . onValidate ) ;
88+ }
89+ markRawOptions ( finalArgs . options ) ;
90+ return finalArgs as SchemaArgs ;
9991}
10092
10193export const schema = function ( ) {
102- const [ initial , options ] =
103- arguments . length === 1 && typeof arguments [ 0 ] === 'object'
104- ? [ undefined , arguments [ 0 ] ]
105- : [ ...arguments ] ;
106- const args = parseSchemaOptions ( [ initial , options ] ) ;
107- const value = initial ;
108- const datatype = Array . isArray ( value ) ? 'array' : typeof value ;
109- const builder = ( ) => ( {
110- value,
111- datatype,
112- options : args . options ,
113- } ) ;
114- builder [ VALUE_SCHEMA_BUILDER_FLAG ] = true ;
115- return builder as SchemaDescriptorBuilder ;
94+ // const [initial, options] =
95+ // arguments.length === 1 && typeof arguments[0] === "object" && !Array.isArray(arguments[0])
96+ // ? [undefined, arguments[0]]
97+ // : [...arguments];
98+ const initial = arguments [ 0 ] ;
99+ const options = arguments [ 1 ] ;
100+ const args = parseSchemaOptions ( [ initial , options ] ) ;
101+ const value = initial ;
102+ const datatype = Array . isArray ( value ) ? "array" : typeof value ;
103+ if ( typeof value === "object" ) {
104+ markRaw ( value ) ;
105+ }
106+ const builder = ( ) => ( {
107+ value,
108+ datatype,
109+ options : args . options ,
110+ } ) ;
111+ builder [ VALUE_SCHEMA_BUILDER_FLAG ] = true ;
112+ return builder as SchemaDescriptorBuilder ;
116113} as unknown as SchemaBuilder ;
117114
118115export const configurable = schema ;
119116export const c = schema ;
120117
121- export function createTypeSchemaBuilder < Value = any > (
122- isValid : ( val : any ) => boolean ,
123- defaultTips : string ,
124- ) {
125- const typeSchema = function ( ) {
126- const opts = Object . assign ( { } , arguments [ 1 ] ) ;
127- if ( typeof opts . onValidate !== 'function' ) {
128- opts . onValidate = isValid ;
129- }
130- if ( ! opts . invalidTips ) {
131- opts . invalidTips = defaultTips ;
132- }
133- return schema ( arguments [ 0 ] , opts ) ;
134- } ;
135- return typeSchema as SchemaBuilder < Value > ;
118+ export function createTypeSchemaBuilder < Value = any > ( isValid : ( val : any ) => boolean , defaultTips : string ) {
119+ const typeSchema = function ( ) {
120+ const opts = Object . assign ( { } , arguments [ 1 ] ) ;
121+ if ( typeof opts . onValidate !== "function" ) {
122+ opts . onValidate = isValid ;
123+ }
124+ if ( ! opts . invalidTips ) {
125+ opts . invalidTips = defaultTips ;
126+ }
127+ return schema ( arguments [ 0 ] , opts ) ;
128+ } ;
129+ return typeSchema as SchemaBuilder < Value > ;
136130}
137131
138132export const schemas = {
139- number : createTypeSchemaBuilder < number > ( ( val ) => typeof val === 'number' , 'must be a number' ) ,
140- string : createTypeSchemaBuilder < string > ( ( val ) => typeof val === 'string' , 'must be a string' ) ,
141- boolean : createTypeSchemaBuilder < boolean > (
142- ( val ) => typeof val === 'boolean' ,
143- 'must be a boolean' ,
144- ) ,
145- date : createTypeSchemaBuilder < Date > ( ( val ) => val instanceof Date , 'must be a date' ) ,
146- bigint : createTypeSchemaBuilder < bigint > ( ( val ) => typeof val === 'bigint' , 'must be a bigint' ) ,
147- array : createTypeSchemaBuilder < any [ ] > ( ( val ) => Array . isArray ( val ) , 'must be an array' ) ,
148- object : createTypeSchemaBuilder < object > ( ( val ) => typeof val === 'object' , 'must be an object' ) ,
133+ number : createTypeSchemaBuilder < number > ( ( val ) => typeof val === "number" , "must be a number" ) ,
134+ string : createTypeSchemaBuilder < string > ( ( val ) => typeof val === "string" , "must be a string" ) ,
135+ boolean : createTypeSchemaBuilder < boolean > ( ( val ) => typeof val === "boolean" , "must be a boolean" ) ,
136+ date : createTypeSchemaBuilder < Date > ( ( val ) => val instanceof Date , "must be a date" ) ,
137+ bigint : createTypeSchemaBuilder < bigint > ( ( val ) => typeof val === "bigint" , "must be a bigint" ) ,
138+ array : createTypeSchemaBuilder < any [ ] > ( ( val ) => Array . isArray ( val ) , "must be an array" ) ,
139+ object : createTypeSchemaBuilder < object > ( ( val ) => typeof val === "object" , "must be an object" ) ,
149140} ;
150141
151142export const s = schemas ;
0 commit comments