11#![ deny( clippy:: all) ]
22
33mod validators;
4-
54use std:: { error:: Error , fs, path:: PathBuf } ;
65
7- use validators:: { env:: EnvValidator , message:: ValidateResult , recommend:: RecommendValidator } ;
6+ use validators:: { env:: EnvValidator , message:: ValidateResult , recommend:: RecommendValidator , common :: fetch_data_text } ;
87
98use crate :: validators:: {
109 common:: Validator ,
@@ -17,8 +16,8 @@ use crate::validators::{
1716extern crate napi_derive;
1817
1918#[ napi]
20- pub fn validate_config ( config_str : String ) -> ValidateResult {
21- let result = validate_config_core ( config_str) ;
19+ pub async fn validate_config ( config_str : String , remote_schema_url : String , use_remote_schema : bool ) -> ValidateResult {
20+ let result = validate_config_core ( config_str, remote_schema_url , use_remote_schema ) . await ;
2221 let messages = match result {
2322 Ok ( messages) => messages,
2423 Err ( e) => {
@@ -42,8 +41,8 @@ pub fn validate_config(config_str: String) -> ValidateResult {
4241}
4342
4443#[ napi]
45- pub fn validate_config_print ( config_str : String ) -> bool {
46- let result = validate_config_core ( config_str) ;
44+ pub async fn validate_config_print ( config_str : String , remote_schema_url : String , use_remote_schema : bool ) -> bool {
45+ let result = validate_config_core ( config_str, remote_schema_url , use_remote_schema ) . await ;
4746 let messages = match result {
4847 Ok ( messages) => messages,
4948 Err ( e) => {
@@ -220,14 +219,35 @@ pub fn validate_recommend_print(app_path: String) -> bool {
220219 is_valid
221220}
222221
223- fn validate_config_core ( config_str : String ) -> Result < Vec < Message > , Box < dyn Error > > {
224- let tip = Message {
222+ async fn validate_config_core ( config_str : String , remote_schema_url : String , use_remote_schema : bool ) -> Result < Vec < Message > , Box < dyn Error > > {
223+ let mut tip = vec ! [ Message {
225224 kind: MessageKind :: Info ,
226225 content: String :: from( "验证项目配置 (/config/index.js) !" ) ,
227226 solution: None ,
227+ } ] ;
228+ let schema_str = if use_remote_schema {
229+ match fetch_data_text ( & remote_schema_url) . await {
230+ Ok ( schema_str) => {
231+ tip. push ( Message {
232+ kind : MessageKind :: Success ,
233+ content : String :: from ( format ! ( "成功获取远程配置验证文件:{}" , remote_schema_url) ) ,
234+ solution : None ,
235+ } ) ;
236+ schema_str
237+ } ,
238+ Err ( _) => {
239+ tip. push ( Message {
240+ kind : MessageKind :: Warning ,
241+ content : String :: from ( "无法获取远程配置验证文件,将使用本地配置验证文件!" ) ,
242+ solution : None ,
243+ } ) ;
244+ include_str ! ( "../assets/config_schema.json" ) . to_string ( )
245+ }
246+ }
247+ } else {
248+ include_str ! ( "../assets/config_schema.json" ) . to_string ( )
228249 } ;
229- let schema_str = include_str ! ( "../assets/config_schema.json" ) ;
230- let config_validator_result = ConfigValidator :: build ( String :: from ( schema_str) , config_str) ;
250+ let config_validator_result = ConfigValidator :: build ( schema_str, config_str) ;
231251 let mut messages = match config_validator_result {
232252 Ok ( config_validator) => config_validator. validate ( ) ,
233253 Err ( e) => vec ! [ Message {
@@ -243,7 +263,7 @@ fn validate_config_core(config_str: String) -> Result<Vec<Message>, Box<dyn Erro
243263 solution : None ,
244264 } )
245265 }
246- messages. insert ( 0 , tip) ;
266+ messages. splice ( 0 .. 0 , tip) ;
247267 Ok ( messages)
248268}
249269
0 commit comments