schema-dsl 1.0.3 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- // Type definitions for schema-dsl v1.0.3
1
+ // Type definitions for schema-dsl v1.0.4
2
2
  // Project: https://github.com/vextjs/schema-dsl
3
3
  // Definitions by: schema-dsl Team
4
4
 
@@ -405,37 +405,663 @@ declare module 'schema-dsl' {
405
405
  * ```
406
406
  */
407
407
  phone(country?: 'cn' | 'us' | 'uk' | 'hk' | 'tw' | 'international'): this;
408
+
409
+ /**
410
+ * 设置格式
411
+ * @param format - 格式名称 (email, url, uuid, date, date-time, time, ipv4, ipv6等)
412
+ * @returns 当前实例(支持链式调用)
413
+ *
414
+ * @example
415
+ * ```typescript
416
+ * builder.format('email');
417
+ * builder.format('uuid');
418
+ * builder.format('date-time');
419
+ * ```
420
+ */
421
+ format(format: string): this;
422
+
423
+ /**
424
+ * 手机号别名(phoneNumber是phone的别名)
425
+ * @param country - 国家代码
426
+ * @returns 当前实例(支持链式调用)
427
+ *
428
+ * @example
429
+ * ```typescript
430
+ * builder.phoneNumber('cn'); // 等同于 phone('cn')
431
+ * ```
432
+ */
433
+ phoneNumber(country?: 'cn' | 'us' | 'uk' | 'hk' | 'tw' | 'international'): this;
434
+
435
+ /**
436
+ * 身份证验证
437
+ * @param country - 国家代码(目前仅支持 'cn')
438
+ * @returns 当前实例(支持链式调用)
439
+ *
440
+ * @example
441
+ * ```typescript
442
+ * builder.idCard('cn'); // 中国身份证18位
443
+ * ```
444
+ */
445
+ idCard(country?: 'cn'): this;
446
+
447
+ /**
448
+ * URL Slug 验证(只能包含小写字母、数字和连字符)
449
+ * @returns 当前实例(支持链式调用)
450
+ *
451
+ * @example
452
+ * ```typescript
453
+ * builder.slug(); // my-blog-post, hello-world-123
454
+ * ```
455
+ */
456
+ slug(): this;
457
+
458
+ /**
459
+ * 信用卡验证
460
+ * @param type - 卡类型
461
+ * @returns 当前实例(支持链式调用)
462
+ *
463
+ * @example
464
+ * ```typescript
465
+ * builder.creditCard('visa');
466
+ * builder.creditCard('mastercard');
467
+ * builder.creditCard('amex');
468
+ * ```
469
+ */
470
+ creditCard(type?: 'visa' | 'mastercard' | 'amex' | 'discover' | 'jcb' | 'unionpay'): this;
471
+
472
+ /**
473
+ * 车牌号验证
474
+ * @param country - 国家代码
475
+ * @returns 当前实例(支持链式调用)
476
+ *
477
+ * @example
478
+ * ```typescript
479
+ * builder.licensePlate('cn'); // 中国车牌号
480
+ * ```
481
+ */
482
+ licensePlate(country?: 'cn' | 'us' | 'uk'): this;
483
+
484
+ /**
485
+ * 邮政编码验证
486
+ * @param country - 国家代码
487
+ * @returns 当前实例(支持链式调用)
488
+ *
489
+ * @example
490
+ * ```typescript
491
+ * builder.postalCode('cn'); // 中国邮政编码6位
492
+ * builder.postalCode('us'); // 美国邮政编码
493
+ * ```
494
+ */
495
+ postalCode(country?: 'cn' | 'us' | 'uk'): this;
496
+
497
+ /**
498
+ * 护照号码验证
499
+ * @param country - 国家代码
500
+ * @returns 当前实例(支持链式调用)
501
+ *
502
+ * @example
503
+ * ```typescript
504
+ * builder.passport('cn'); // 中国护照号
505
+ * ```
506
+ */
507
+ passport(country?: 'cn' | 'us' | 'uk'): this;
508
+
509
+ /**
510
+ * String 最小长度(使用AJV原生minLength)
511
+ * @param n - 最小长度
512
+ * @returns 当前实例(支持链式调用)
513
+ *
514
+ * @example
515
+ * ```typescript
516
+ * dsl('string!').min(3); // 最少3个字符
517
+ * ```
518
+ */
519
+ min(n: number): this;
520
+
521
+ /**
522
+ * String 最大长度(使用AJV原生maxLength)
523
+ * @param n - 最大长度
524
+ * @returns 当前实例(支持链式调用)
525
+ *
526
+ * @example
527
+ * ```typescript
528
+ * dsl('string!').max(32); // 最多32个字符
529
+ * ```
530
+ */
531
+ max(n: number): this;
532
+
533
+ /**
534
+ * String 精确长度
535
+ * @param n - 精确长度
536
+ * @returns 当前实例(支持链式调用)
537
+ *
538
+ * @example
539
+ * ```typescript
540
+ * dsl('string!').length(11); // 必须是11个字符
541
+ * ```
542
+ */
543
+ length(n: number): this;
544
+
545
+ /**
546
+ * String 只能包含字母和数字
547
+ * @returns 当前实例(支持链式调用)
548
+ *
549
+ * @example
550
+ * ```typescript
551
+ * dsl('string!').alphanum(); // 只能是字母和数字
552
+ * ```
553
+ */
554
+ alphanum(): this;
555
+
556
+ /**
557
+ * String 不能包含前后空格
558
+ * @returns 当前实例(支持链式调用)
559
+ *
560
+ * @example
561
+ * ```typescript
562
+ * dsl('string!').trim(); // 不能有前后空格
563
+ * ```
564
+ */
565
+ trim(): this;
566
+
567
+ /**
568
+ * String 必须是小写
569
+ * @returns 当前实例(支持链式调用)
570
+ *
571
+ * @example
572
+ * ```typescript
573
+ * dsl('string!').lowercase(); // 必须全小写
574
+ * ```
575
+ */
576
+ lowercase(): this;
577
+
578
+ /**
579
+ * String 必须是大写
580
+ * @returns 当前实例(支持链式调用)
581
+ *
582
+ * @example
583
+ * ```typescript
584
+ * dsl('string!').uppercase(); // 必须全大写
585
+ * ```
586
+ */
587
+ uppercase(): this;
588
+
589
+ /**
590
+ * Number 小数位数限制
591
+ * @param n - 最大小数位数
592
+ * @returns 当前实例(支持链式调用)
593
+ *
594
+ * @example
595
+ * ```typescript
596
+ * dsl('number!').precision(2); // 最多2位小数
597
+ * ```
598
+ */
599
+ precision(n: number): this;
600
+
601
+ /**
602
+ * Number 倍数验证(使用AJV原生multipleOf)
603
+ * @param n - 必须是此数的倍数
604
+ * @returns 当前实例(支持链式调用)
605
+ *
606
+ * @example
607
+ * ```typescript
608
+ * dsl('number!').multiple(5); // 必须是5的倍数
609
+ * ```
610
+ */
611
+ multiple(n: number): this;
612
+
613
+ /**
614
+ * Number 端口号验证(1-65535)
615
+ * @returns 当前实例(支持链式调用)
616
+ *
617
+ * @example
618
+ * ```typescript
619
+ * dsl('integer!').port(); // 必须是有效端口号
620
+ * ```
621
+ */
622
+ port(): this;
623
+
624
+ /**
625
+ * Object 要求所有属性都必须存在
626
+ * @returns 当前实例(支持链式调用)
627
+ *
628
+ * @example
629
+ * ```typescript
630
+ * dsl({ name: 'string', age: 'number' }).requireAll();
631
+ * ```
632
+ */
633
+ requireAll(): this;
634
+
635
+ /**
636
+ * Object 严格模式,不允许额外属性
637
+ * @returns 当前实例(支持链式调用)
638
+ *
639
+ * @example
640
+ * ```typescript
641
+ * dsl({ name: 'string!' }).strict();
642
+ * ```
643
+ */
644
+ strict(): this;
645
+
646
+ /**
647
+ * Array 不允许稀疏数组
648
+ * @returns 当前实例(支持链式调用)
649
+ *
650
+ * @example
651
+ * ```typescript
652
+ * dsl('array<string>').noSparse();
653
+ * ```
654
+ */
655
+ noSparse(): this;
656
+
657
+ /**
658
+ * Array 必须包含指定元素
659
+ * @param items - 必须包含的元素数组
660
+ * @returns 当前实例(支持链式调用)
661
+ *
662
+ * @example
663
+ * ```typescript
664
+ * dsl('array<string>').includesRequired(['admin', 'user']);
665
+ * ```
666
+ */
667
+ includesRequired(items: any[]): this;
668
+
669
+ /**
670
+ * Date 自定义日期格式验证
671
+ * @param fmt - 日期格式(YYYY-MM-DD, YYYY/MM/DD, DD-MM-YYYY, DD/MM/YYYY, ISO8601)
672
+ * @returns 当前实例(支持链式调用)
673
+ *
674
+ * @example
675
+ * ```typescript
676
+ * dsl('string!').dateFormat('YYYY-MM-DD');
677
+ * ```
678
+ */
679
+ dateFormat(fmt: 'YYYY-MM-DD' | 'YYYY/MM/DD' | 'DD-MM-YYYY' | 'DD/MM/YYYY' | 'ISO8601' | string): this;
680
+
681
+ /**
682
+ * Date 必须晚于指定日期
683
+ * @param date - 比较日期
684
+ * @returns 当前实例(支持链式调用)
685
+ *
686
+ * @example
687
+ * ```typescript
688
+ * dsl('date!').after('2024-01-01');
689
+ * ```
690
+ */
691
+ after(date: string): this;
692
+
693
+ /**
694
+ * Date 必须早于指定日期
695
+ * @param date - 比较日期
696
+ * @returns 当前实例(支持链式调用)
697
+ *
698
+ * @example
699
+ * ```typescript
700
+ * dsl('date!').before('2025-12-31');
701
+ * ```
702
+ */
703
+ before(date: string): this;
704
+
705
+ /**
706
+ * Pattern 域名验证
707
+ * @returns 当前实例(支持链式调用)
708
+ *
709
+ * @example
710
+ * ```typescript
711
+ * dsl('string!').domain(); // example.com, sub.example.com
712
+ * ```
713
+ */
714
+ domain(): this;
715
+
716
+ /**
717
+ * Pattern IP地址验证(IPv4或IPv6)
718
+ * @returns 当前实例(支持链式调用)
719
+ *
720
+ * @example
721
+ * ```typescript
722
+ * dsl('string!').ip(); // 192.168.1.1 或 2001:0db8:85a3::8a2e:0370:7334
723
+ * ```
724
+ */
725
+ ip(): this;
726
+
727
+ /**
728
+ * Pattern Base64编码验证
729
+ * @returns 当前实例(支持链式调用)
730
+ *
731
+ * @example
732
+ * ```typescript
733
+ * dsl('string!').base64();
734
+ * ```
735
+ */
736
+ base64(): this;
737
+
738
+ /**
739
+ * Pattern JWT令牌验证
740
+ * @returns 当前实例(支持链式调用)
741
+ *
742
+ * @example
743
+ * ```typescript
744
+ * dsl('string!').jwt();
745
+ * ```
746
+ */
747
+ jwt(): this;
748
+
749
+ /**
750
+ * Pattern JSON字符串验证
751
+ * @returns 当前实例(支持链式调用)
752
+ *
753
+ * @example
754
+ * ```typescript
755
+ * dsl('string!').json();
756
+ * ```
757
+ */
758
+ json(): this;
759
+
760
+ /**
761
+ * 日期大于验证
762
+ * @param date - 对比日期
763
+ * @returns 当前实例(支持链式调用)
764
+ *
765
+ * @example
766
+ * ```typescript
767
+ * dsl('string!').dateGreater('2025-01-01');
768
+ * ```
769
+ */
770
+ dateGreater(date: string): this;
771
+
772
+ /**
773
+ * 日期小于验证
774
+ * @param date - 对比日期
775
+ * @returns 当前实例(支持链式调用)
776
+ *
777
+ * @example
778
+ * ```typescript
779
+ * dsl('string!').dateLess('2025-12-31');
780
+ * ```
781
+ */
782
+ dateLess(date: string): this;
408
783
  }
409
784
 
410
785
  // ========== String 扩展 ==========
411
786
 
412
787
  /**
413
788
  * String 扩展全局接口
414
- * 让字符串直接支持链式调用
789
+ *
790
+ * ⚠️ TypeScript 用户注意事项
791
+ *
792
+ * 由于 TypeScript 对全局扩展的类型推导限制,在 .ts 文件中使用链式调用时,
793
+ * 推荐使用 dsl() 函数包裹字符串以获得完整的类型提示:
415
794
  *
416
795
  * @example
417
796
  * ```typescript
797
+ * // ❌ 不推荐:可能缺少类型提示
798
+ * const schema = dsl({
799
+ * email: 'email!'.label('邮箱') // TypeScript 可能无法推导
800
+ * });
801
+ *
802
+ * // ✅ 推荐:使用 dsl() 包裹获得完整类型推导
418
803
  * const schema = dsl({
419
- * email: 'email!'.pattern(/custom/).label('邮箱')
804
+ * email: dsl('email!').label('邮箱').pattern(/custom/)
805
+ * });
806
+ *
807
+ * // ✅ 也可以:先定义再使用
808
+ * const emailField = dsl('email!').label('邮箱');
809
+ * const schema = dsl({ email: emailField });
810
+ *
811
+ * // 📝 JavaScript 用户不受影响,可以直接使用
812
+ * const schema = dsl({
813
+ * email: 'email!'.label('邮箱') // JavaScript 中完全正常
420
814
  * });
421
815
  * ```
422
816
  */
423
817
  global {
424
818
  interface String {
819
+ /**
820
+ * 添加正则验证
821
+ * @deprecated TypeScript 用户请使用 dsl(string).pattern()
822
+ */
425
823
  pattern(regex: RegExp | string, message?: string): DslBuilder;
824
+
825
+ /**
826
+ * 设置字段标签
827
+ * @deprecated TypeScript 用户请使用 dsl(string).label()
828
+ */
426
829
  label(text: string): DslBuilder;
830
+
831
+ /**
832
+ * 自定义错误消息
833
+ * @deprecated TypeScript 用户请使用 dsl(string).messages()
834
+ */
427
835
  messages(messages: ErrorMessages): DslBuilder;
836
+
837
+ /**
838
+ * 设置描述
839
+ * @deprecated TypeScript 用户请使用 dsl(string).description()
840
+ */
428
841
  description(text: string): DslBuilder;
842
+
843
+ /**
844
+ * 自定义验证器
845
+ * @deprecated TypeScript 用户请使用 dsl(string).custom()
846
+ */
429
847
  custom(validator: (value: any) => boolean | Promise<boolean> | { error: string; message: string }): DslBuilder;
848
+
849
+ /**
850
+ * 条件验证
851
+ * @deprecated TypeScript 用户请使用 dsl(string).when()
852
+ */
430
853
  when(refField: string, options: { is: any; then: DslBuilder | JSONSchema; otherwise?: DslBuilder | JSONSchema }): DslBuilder;
854
+
855
+ /**
856
+ * 设置默认值
857
+ * @deprecated TypeScript 用户请使用 dsl(string).default()
858
+ */
431
859
  default(value: any): DslBuilder;
860
+
861
+ /**
862
+ * 转为 JSON Schema
863
+ * @deprecated TypeScript 用户请使用 dsl(string).toSchema()
864
+ */
432
865
  toSchema(): JSONSchema;
433
- /** 用户名验证 */
866
+
867
+ /**
868
+ * 用户名验证
869
+ * @deprecated TypeScript 用户请使用 dsl(string).username()
870
+ */
434
871
  username(preset?: 'short' | 'medium' | 'long' | string): DslBuilder;
435
- /** 密码强度验证 */
872
+
873
+ /**
874
+ * 密码强度验证
875
+ * @deprecated TypeScript 用户请使用 dsl(string).password()
876
+ */
436
877
  password(strength?: 'weak' | 'medium' | 'strong' | 'veryStrong'): DslBuilder;
437
- /** 手机号验证 */
878
+
879
+ /**
880
+ * 手机号验证
881
+ * @deprecated TypeScript 用户请使用 dsl(string).phone()
882
+ */
438
883
  phone(country?: 'cn' | 'us' | 'uk' | 'hk' | 'tw' | 'international'): DslBuilder;
884
+
885
+ /**
886
+ * 设置格式
887
+ * @deprecated TypeScript 用户请使用 dsl(string).format()
888
+ */
889
+ format(format: string): DslBuilder;
890
+
891
+ /**
892
+ * 手机号别名
893
+ * @deprecated TypeScript 用户请使用 dsl(string).phoneNumber()
894
+ */
895
+ phoneNumber(country?: 'cn' | 'us' | 'uk' | 'hk' | 'tw' | 'international'): DslBuilder;
896
+
897
+ /**
898
+ * 身份证验证
899
+ * @deprecated TypeScript 用户请使用 dsl(string).idCard()
900
+ */
901
+ idCard(country?: 'cn'): DslBuilder;
902
+
903
+ /**
904
+ * 信用卡验证
905
+ * @deprecated TypeScript 用户请使用 dsl(string).creditCard()
906
+ */
907
+ creditCard(type?: 'visa' | 'mastercard' | 'amex' | 'discover' | 'jcb' | 'unionpay'): DslBuilder;
908
+
909
+ /**
910
+ * 车牌号验证
911
+ * @deprecated TypeScript 用户请使用 dsl(string).licensePlate()
912
+ */
913
+ licensePlate(country?: 'cn' | 'us' | 'uk'): DslBuilder;
914
+
915
+ /**
916
+ * 邮政编码验证
917
+ * @deprecated TypeScript 用户请使用 dsl(string).postalCode()
918
+ */
919
+ postalCode(country?: 'cn' | 'us' | 'uk'): DslBuilder;
920
+
921
+ /**
922
+ * 护照号码验证
923
+ * @deprecated TypeScript 用户请使用 dsl(string).passport()
924
+ */
925
+ passport(country?: 'cn' | 'us' | 'uk'): DslBuilder;
926
+
927
+ /**
928
+ * String 最小长度
929
+ * @deprecated TypeScript 用户请使用 dsl(string).min()
930
+ */
931
+ min(n: number): DslBuilder;
932
+
933
+ /**
934
+ * String 最大长度
935
+ * @deprecated TypeScript 用户请使用 dsl(string).max()
936
+ */
937
+ max(n: number): DslBuilder;
938
+
939
+
940
+ /**
941
+ * String 只能包含字母和数字
942
+ * @deprecated TypeScript 用户请使用 dsl(string).alphanum()
943
+ */
944
+ alphanum(): DslBuilder;
945
+
946
+ /**
947
+ * String 不能包含前后空格
948
+ * @deprecated TypeScript 用户请使用 dsl(string).trim()
949
+ */
950
+ trim(): DslBuilder;
951
+
952
+ /**
953
+ * String 必须是小写
954
+ * @deprecated TypeScript 用户请使用 dsl(string).lowercase()
955
+ */
956
+ lowercase(): DslBuilder;
957
+
958
+ /**
959
+ * String 必须是大写
960
+ * @deprecated TypeScript 用户请使用 dsl(string).uppercase()
961
+ */
962
+ uppercase(): DslBuilder;
963
+
964
+ /**
965
+ * Number 小数位数限制
966
+ * @deprecated TypeScript 用户请使用 dsl(string).precision()
967
+ */
968
+ precision(n: number): DslBuilder;
969
+
970
+ /**
971
+ * Number 倍数验证
972
+ * @deprecated TypeScript 用户请使用 dsl(string).multiple()
973
+ */
974
+ multiple(n: number): DslBuilder;
975
+
976
+ /**
977
+ * Number 端口号验证
978
+ * @deprecated TypeScript 用户请使用 dsl(string).port()
979
+ */
980
+ port(): DslBuilder;
981
+
982
+ /**
983
+ * Object 要求所有属性都必须存在
984
+ * @deprecated TypeScript 用户请使用 dsl(obj).requireAll()
985
+ */
986
+ requireAll(): DslBuilder;
987
+
988
+ /**
989
+ * Object 严格模式
990
+ * @deprecated TypeScript 用户请使用 dsl(obj).strict()
991
+ */
992
+ strict(): DslBuilder;
993
+
994
+ /**
995
+ * Array 不允许稀疏数组
996
+ * @deprecated TypeScript 用户请使用 dsl(string).noSparse()
997
+ */
998
+ noSparse(): DslBuilder;
999
+
1000
+ /**
1001
+ * Array 必须包含指定元素
1002
+ * @deprecated TypeScript 用户请使用 dsl(string).includesRequired()
1003
+ */
1004
+ includesRequired(items: any[]): DslBuilder;
1005
+
1006
+ /**
1007
+ * Date 自定义日期格式验证
1008
+ * @deprecated TypeScript 用户请使用 dsl(string).dateFormat()
1009
+ */
1010
+ dateFormat(fmt: string): DslBuilder;
1011
+
1012
+ /**
1013
+ * Date 必须晚于指定日期
1014
+ * @deprecated TypeScript 用户请使用 dsl(string).after()
1015
+ */
1016
+ after(date: string): DslBuilder;
1017
+
1018
+ /**
1019
+ * Date 必须早于指定日期
1020
+ * @deprecated TypeScript 用户请使用 dsl(string).before()
1021
+ */
1022
+ before(date: string): DslBuilder;
1023
+
1024
+ /**
1025
+ * Pattern 域名验证
1026
+ * @deprecated TypeScript 用户请使用 dsl(string).domain()
1027
+ */
1028
+ domain(): DslBuilder;
1029
+
1030
+ /**
1031
+ * Pattern IP地址验证
1032
+ * @deprecated TypeScript 用户请使用 dsl(string).ip()
1033
+ */
1034
+ ip(): DslBuilder;
1035
+
1036
+ /**
1037
+ * Pattern Base64编码验证
1038
+ * @deprecated TypeScript 用户请使用 dsl(string).base64()
1039
+ */
1040
+ base64(): DslBuilder;
1041
+
1042
+ /**
1043
+ * Pattern JWT令牌验证
1044
+ * @deprecated TypeScript 用户请使用 dsl(string).jwt()
1045
+ */
1046
+ jwt(): DslBuilder;
1047
+
1048
+ /**
1049
+ * Pattern JSON字符串验证
1050
+ * @deprecated TypeScript 用户请使用 dsl(string).json()
1051
+ */
1052
+ json(): DslBuilder;
1053
+
1054
+ /**
1055
+ * 日期大于验证
1056
+ * @deprecated TypeScript 用户请使用 dsl(string).dateGreater()
1057
+ */
1058
+ dateGreater(date: string): DslBuilder;
1059
+
1060
+ /**
1061
+ * 日期小于验证
1062
+ * @deprecated TypeScript 用户请使用 dsl(string).dateLess()
1063
+ */
1064
+ dateLess(date: string): DslBuilder;
439
1065
  }
440
1066
  }
441
1067
 
@@ -623,8 +1249,25 @@ declare module 'schema-dsl' {
623
1249
  *
624
1250
  * @example
625
1251
  * ```typescript
1252
+ * // 方式 1: 使用 i18n 配置(推荐,v1.0.4+)
1253
+ * dsl.config({
1254
+ * i18n: {
1255
+ * locales: {
1256
+ * 'zh-CN': { required: '必填' },
1257
+ * 'en-US': { required: 'Required' }
1258
+ * }
1259
+ * }
1260
+ * });
1261
+ *
1262
+ * // 方式 2: 使用 locales 配置(向后兼容)
1263
+ * dsl.config({
1264
+ * locales: {
1265
+ * 'zh-CN': { required: '必填' }
1266
+ * }
1267
+ * });
1268
+ *
1269
+ * // 自定义手机号规则
626
1270
  * dsl.config({
627
- * // 自定义手机号规则
628
1271
  * patterns: {
629
1272
  * phone: {
630
1273
  * cn: {
@@ -634,13 +1277,15 @@ declare module 'schema-dsl' {
634
1277
  * key: 'phone.cn'
635
1278
  * }
636
1279
  * }
637
- * },
638
- * // 设置语言包
639
- * locales: 'zh-CN'
1280
+ * }
640
1281
  * });
641
1282
  * ```
642
1283
  */
643
1284
  export function config(options: {
1285
+ /** i18n 配置(推荐,v1.0.4+) */
1286
+ i18n?: I18nConfig;
1287
+ /** 缓存配置 */
1288
+ cache?: CacheConfig;
644
1289
  /** 自定义验证规则 */
645
1290
  patterns?: {
646
1291
  /** 手机号规则 */
@@ -652,7 +1297,7 @@ declare module 'schema-dsl' {
652
1297
  };
653
1298
  /** 手机号规则(兼容旧版) */
654
1299
  phone?: Record<string, { pattern: RegExp; min?: number; max?: number; key?: string }>;
655
- /** 语言包配置 */
1300
+ /** 语言包配置(兼容旧版,推荐使用 i18n.locales) */
656
1301
  locales?: Record<string, ErrorMessages> | string;
657
1302
  }): void;
658
1303
 
@@ -675,24 +1320,53 @@ declare module 'schema-dsl' {
675
1320
  export function match(value: any, cases: Record<string, any>): any;
676
1321
 
677
1322
  /**
678
- * 条件规则
1323
+ * 条件规则 (if)
1324
+ *
1325
+ * @description 根据条件字段的值选择不同的Schema
1326
+ *
1327
+ * JavaScript 中使用: `dsl.if(condition, thenSchema, elseSchema)`
1328
+ * TypeScript 中使用: `dsl['if'](condition, thenSchema, elseSchema)` 或 `dsl._if(...)`
679
1329
  *
680
- * @description 根据条件选择不同的Schema(JavaScript中使用 dsl.if)
1330
+ * @param condition - 条件字段名
1331
+ * @param thenSchema - 条件为 true 时的 Schema
1332
+ * @param elseSchema - 条件为 false 时的 Schema(可选)
681
1333
  *
682
1334
  * @example
683
1335
  * ```typescript
1336
+ * // TypeScript 中因为 if 是保留字,需要用字符串索引或 _if
684
1337
  * const schema = dsl({
1338
+ * isVip: 'boolean',
1339
+ * discount: dsl['if']('isVip', 'number:10-50!', 'number:0-10')
1340
+ * });
1341
+ *
1342
+ * // 或者使用 _if 别名
1343
+ * const schema2 = dsl({
685
1344
  * age: 'number',
686
- * license: dsl._if(
687
- * (data) => data.age >= 18,
688
- * { hasLicense: 'boolean!' },
689
- * { hasLicense: 'boolean' }
690
- * )
1345
+ * license: dsl._if('age', 'boolean!', 'boolean')
1346
+ * });
1347
+ * ```
1348
+ *
1349
+ * @example
1350
+ * ```javascript
1351
+ * // JavaScript 中可以直接使用 dsl.if
1352
+ * const schema = dsl({
1353
+ * isVip: 'boolean',
1354
+ * discount: dsl.if('isVip', 'number:10-50!', 'number:0-10')
691
1355
  * });
692
1356
  * ```
693
1357
  */
694
1358
  export const _if: (condition: any, thenSchema: any, elseSchema?: any) => any;
695
1359
 
1360
+ /**
1361
+ * 条件规则的别名(用于 TypeScript)
1362
+ *
1363
+ * @description 因为 TypeScript 中 `if` 是保留字,提供 `_if` 作为别名
1364
+ *
1365
+ * JavaScript 用户请直接使用 `dsl.if()`
1366
+ * TypeScript 用户可以使用 `dsl['if']()` 或 `dsl._if()`
1367
+ */
1368
+ export { _if as if };
1369
+
696
1370
  /**
697
1371
  * 设置默认语言
698
1372
  *
@@ -826,8 +1500,8 @@ declare module 'schema-dsl' {
826
1500
  }
827
1501
 
828
1502
  /**
829
- * 便捷验证方法(推荐)
830
- *
1503
+ * 便捷验证方法(同步)
1504
+ *
831
1505
  * @description 使用默认的单例Validator,无需new
832
1506
  *
833
1507
  * @example
@@ -844,6 +1518,146 @@ declare module 'schema-dsl' {
844
1518
  */
845
1519
  export function validate<T = any>(schema: JSONSchema | SchemaIO, data: any): ValidationResult<T>;
846
1520
 
1521
+ /**
1522
+ * 便捷异步验证方法(推荐)
1523
+ *
1524
+ * @description
1525
+ * - 异步验证数据,验证失败时抛出 ValidationError
1526
+ * - 推荐在异步场景下使用此方法
1527
+ * - 验证成功返回验证后的数据,失败抛出异常
1528
+ *
1529
+ * @param schema - JSON Schema对象或SchemaIO实例
1530
+ * @param data - 要验证的数据
1531
+ * @param options - 验证选项(可选)
1532
+ * @returns 验证成功返回数据的Promise
1533
+ * @throws {ValidationError} 验证失败时抛出
1534
+ *
1535
+ * @example
1536
+ * ```typescript
1537
+ * import { dsl, validateAsync, ValidationError } from 'schema-dsl';
1538
+ *
1539
+ * const schema = dsl({
1540
+ * email: dsl('email!').label('邮箱'),
1541
+ * username: dsl('string:3-32!').label('用户名')
1542
+ * });
1543
+ *
1544
+ * try {
1545
+ * const validData = await validateAsync(schema, {
1546
+ * email: 'test@example.com',
1547
+ * username: 'testuser'
1548
+ * });
1549
+ * console.log('验证通过:', validData);
1550
+ * } catch (error) {
1551
+ * if (error instanceof ValidationError) {
1552
+ * console.log('验证失败:', error.errors);
1553
+ * error.errors.forEach(err => {
1554
+ * console.log(`${err.path}: ${err.message}`);
1555
+ * });
1556
+ * }
1557
+ * }
1558
+ * ```
1559
+ */
1560
+ export function validateAsync<T = any>(
1561
+ schema: JSONSchema | SchemaIO,
1562
+ data: any,
1563
+ options?: ValidatorOptions
1564
+ ): Promise<T>;
1565
+
1566
+ /**
1567
+ * 验证错误类
1568
+ *
1569
+ * @description 当 validateAsync 验证失败时抛出此错误
1570
+ *
1571
+ * @example
1572
+ * ```typescript
1573
+ * import { ValidationError, validateAsync, dsl } from 'schema-dsl';
1574
+ *
1575
+ * const schema = dsl({
1576
+ * email: dsl('email!').label('邮箱'),
1577
+ * age: dsl('number:18-100').label('年龄')
1578
+ * });
1579
+ *
1580
+ * try {
1581
+ * await validateAsync(schema, { email: 'invalid' });
1582
+ * } catch (error) {
1583
+ * if (error instanceof ValidationError) {
1584
+ * // 获取所有错误
1585
+ * console.log('错误列表:', error.errors);
1586
+ *
1587
+ * // 获取错误数量
1588
+ * console.log('错误数量:', error.errors.length);
1589
+ *
1590
+ * // 遍历处理每个字段错误
1591
+ * error.errors.forEach(err => {
1592
+ * console.log(`字段 ${err.path}: ${err.message}`);
1593
+ * });
1594
+ *
1595
+ * // 转为 JSON 格式
1596
+ * const json = error.toJSON();
1597
+ * console.log('JSON格式:', json);
1598
+ * }
1599
+ * }
1600
+ * ```
1601
+ */
1602
+ export class ValidationError extends Error {
1603
+ /** 错误名称(固定为 'ValidationError') */
1604
+ readonly name: 'ValidationError';
1605
+
1606
+ /** 错误消息 */
1607
+ message: string;
1608
+
1609
+ /** 验证错误列表 */
1610
+ errors: ValidationError[];
1611
+
1612
+ /**
1613
+ * 构造函数
1614
+ * @param errors - 验证错误数组
1615
+ * @param message - 错误消息(可选)
1616
+ */
1617
+ constructor(errors: ValidationError[], message?: string);
1618
+
1619
+ /**
1620
+ * 转为 JSON 格式
1621
+ * @returns JSON 对象
1622
+ */
1623
+ toJSON(): {
1624
+ name: string;
1625
+ message: string;
1626
+ errors: Array<{
1627
+ field: string;
1628
+ message: string;
1629
+ keyword: string;
1630
+ params?: Record<string, any>;
1631
+ }>;
1632
+ };
1633
+
1634
+ /**
1635
+ * 获取指定字段的错误
1636
+ * @param field - 字段路径
1637
+ * @returns 错误对象或 null
1638
+ */
1639
+ getFieldError(field: string): ValidationError | null;
1640
+
1641
+ /**
1642
+ * 获取所有字段的错误映射
1643
+ * @returns 字段错误映射对象
1644
+ */
1645
+ getFieldErrors(): Record<string, ValidationError>;
1646
+
1647
+ /**
1648
+ * 检查指定字段是否有错误
1649
+ * @param field - 字段路径
1650
+ * @returns 是否有错误
1651
+ */
1652
+ hasFieldError(field: string): boolean;
1653
+
1654
+ /**
1655
+ * 获取错误总数
1656
+ * @returns 错误数量
1657
+ */
1658
+ getErrorCount(): number;
1659
+ }
1660
+
847
1661
  /**
848
1662
  * 获取默认Validator实例(单例)
849
1663
  *
@@ -1763,23 +2577,31 @@ declare module 'schema-dsl' {
1763
2577
  * // 添加自定义关键字通常通过 Validator 的 addKeyword 方法
1764
2578
  * const validator = new Validator();
1765
2579
  * const ajv = validator.getAjv();
1766
- *
1767
- * // 使用 ajv.addKeyword() 添加自定义关键字
1768
- * ```
1769
- */
1770
- export const CustomKeywords: any;
1771
2580
 
1772
- // ========== dsl.config 选项(v2.3.0+)==========
2581
+ // ========== dsl.config 选项 ==========
1773
2582
 
1774
2583
  /**
1775
- * i18n 配置选项(v2.3.0+)
2584
+ * i18n 配置选项
2585
+ *
2586
+ * @description 支持两种配置方式
2587
+ *
2588
+ * @example
2589
+ * ```typescript
2590
+ * // 方式1: 直接传字符串路径
2591
+ * dsl.config({
2592
+ * i18n: './i18n/dsl'
2593
+ * });
2594
+ *
2595
+ * // 方式2: 传入语言包对象
2596
+ * dsl.config({
2597
+ * i18n: {
2598
+ * 'zh-CN': { required: '必填' },
2599
+ * 'en-US': { required: 'Required' }
2600
+ * }
2601
+ * });
2602
+ * ```
1776
2603
  */
1777
- export interface I18nConfig {
1778
- /** 语言包目录路径 */
1779
- localesPath?: string;
1780
- /** 直接传入的语言包 */
1781
- locales?: Record<string, ErrorMessages>;
1782
- }
2604
+ export type I18nConfig = string | Record<string, ErrorMessages>;
1783
2605
 
1784
2606
  /**
1785
2607
  * 缓存配置选项(v2.3.0+)
@@ -1981,6 +2803,142 @@ declare module 'schema-dsl' {
1981
2803
 
1982
2804
  // ========== 默认导出 ==========
1983
2805
 
2806
+ // ========== 验证器扩展 ==========
2807
+
2808
+ /**
2809
+ * 自定义关键字
2810
+ *
2811
+ * @description 扩展ajv的自定义验证关键字
2812
+ *
2813
+ * @example
2814
+ * ```typescript
2815
+ * import { CustomKeywords, Validator } from 'schema-dsl';
2816
+ *
2817
+ * const validator = new Validator();
2818
+ * const ajv = validator.getAjv();
2819
+ * CustomKeywords.registerAll(ajv);
2820
+ * ```
2821
+ */
2822
+ export const CustomKeywords: {
2823
+ /**
2824
+ * 注册所有自定义关键字到ajv实例
2825
+ * @param ajv - ajv实例
2826
+ */
2827
+ registerAll(ajv: any): void;
2828
+
2829
+ /**
2830
+ * 注册元数据关键字
2831
+ * @param ajv - ajv实例
2832
+ */
2833
+ registerMetadataKeywords(ajv: any): void;
2834
+
2835
+ /**
2836
+ * 注册字符串验证器
2837
+ * @param ajv - ajv实例
2838
+ */
2839
+ registerStringValidators(ajv: any): void;
2840
+
2841
+ /**
2842
+ * 注册数字验证器
2843
+ * @param ajv - ajv实例
2844
+ */
2845
+ registerNumberValidators(ajv: any): void;
2846
+
2847
+ /**
2848
+ * 注册对象验证器
2849
+ * @param ajv - ajv实例
2850
+ */
2851
+ registerObjectValidators(ajv: any): void;
2852
+
2853
+ /**
2854
+ * 注册数组验证器
2855
+ * @param ajv - ajv实例
2856
+ */
2857
+ registerArrayValidators(ajv: any): void;
2858
+
2859
+ /**
2860
+ * 注册日期验证器
2861
+ * @param ajv - ajv实例
2862
+ */
2863
+ registerDateValidators(ajv: any): void;
2864
+ };
2865
+
2866
+ // ========== 常量 ==========
2867
+
2868
+ /**
2869
+ * SchemaIO 配置常量
2870
+ *
2871
+ * @description 所有魔法数字和配置项的统一定义
2872
+ *
2873
+ * @example
2874
+ * ```typescript
2875
+ * import { CONSTANTS } from 'schema-dsl';
2876
+ *
2877
+ * console.log(CONSTANTS.VALIDATION.MAX_RECURSION_DEPTH); // 100
2878
+ * console.log(CONSTANTS.CACHE.SCHEMA_CACHE.MAX_SIZE); // 5000
2879
+ * ```
2880
+ */
2881
+ export const CONSTANTS: {
2882
+ /** 验证配置 */
2883
+ VALIDATION: {
2884
+ /** 递归深度限制 */
2885
+ MAX_RECURSION_DEPTH: number;
2886
+ /** 数组大小限制 */
2887
+ MAX_ARRAY_SIZE: number;
2888
+ /** 字符串长度限制 */
2889
+ MAX_STRING_LENGTH: number;
2890
+ /** 对象属性数量限制 */
2891
+ MAX_OBJECT_KEYS: number;
2892
+ /** 验证超时时间(ms) */
2893
+ DEFAULT_TIMEOUT: number;
2894
+ /** 正则表达式超时(ms) */
2895
+ REGEX_TIMEOUT: number;
2896
+ /** 自定义验证函数超时(ms) */
2897
+ CUSTOM_VALIDATOR_TIMEOUT: number;
2898
+ /** 默认选项 */
2899
+ DEFAULT_OPTIONS: {
2900
+ abortEarly: boolean;
2901
+ stripUnknown: boolean;
2902
+ convert: boolean;
2903
+ presence: string;
2904
+ allowUnknown: boolean;
2905
+ skipFunctions: boolean;
2906
+ };
2907
+ };
2908
+ /** 缓存配置 */
2909
+ CACHE: {
2910
+ /** 缓存开关 */
2911
+ ENABLED: boolean;
2912
+ /** Schema编译缓存 */
2913
+ SCHEMA_CACHE: {
2914
+ /** 最大缓存条目 */
2915
+ MAX_SIZE: number;
2916
+ /** 缓存过期时间(ms) */
2917
+ TTL: number;
2918
+ };
2919
+ };
2920
+ /** 格式配置 */
2921
+ FORMAT: Record<string, any>;
2922
+ /** 类型配置 */
2923
+ TYPES: Record<string, any>;
2924
+ /** 错误配置 */
2925
+ ERRORS: Record<string, any>;
2926
+ };
2927
+
2928
+ /**
2929
+ * 版本信息
2930
+ *
2931
+ * @description 当前schema-dsl版本号
2932
+ *
2933
+ * @example
2934
+ * ```typescript
2935
+ * import { VERSION } from 'schema-dsl';
2936
+ *
2937
+ * console.log(`schema-dsl version: ${VERSION}`); // schema-dsl version: 1.0.4
2938
+ * ```
2939
+ */
2940
+ export const VERSION: string;
2941
+
1984
2942
  /**
1985
2943
  * 默认导出(dsl函数)
1986
2944
  *