네트워크 요청에서 돌아오는 데이터는 any입니다——TypeScript는 여러분의 캐스트를 믿지만, 서버는 다른 것을 보낼 수 있습니다. $mol은 신뢰할 수 없는 JSON을 타입이 있고 검증된 값으로 바꾸고, 형태가 틀리면 읽을 수 있는 경로와 함께 크게 실패하는 두 개의 작은 런타임 스키마 라이브러리를 제공합니다. 데이터가 앱에 들어오는 바로 그곳, 대개 fetch 응답에서 사용하세요.
$mol_data——간결한 함수형 파서(zod 스타일). 작은 파싱 함수를 조합하고, 그 결과를 값에 대해 호출합니다.$mol_schema——기본값을 갖는 클래스 기반 스키마. 레코드 클래스를 확장하면 .guard(), .cast(), .check(), 그리고 .default를 얻습니다.둘 다 런타임에 검증하고 정적 타입을 여러분을 위해 추론합니다. 빠른 DTO와 (역)직렬화에는 $mol_data를, 기본값과 느슨한 캐스팅을 갖춘 이름 있는 재사용 가능한 스키마 클래스를 원할 때는 $mol_schema를 택하세요.
형태를 필드 파서의 레코드로 기술하세요.1const UserDTO=$mol_data_record({2name:$mol_data_string,3age:$mol_data_optional($mol_data_integer),4mail:$mol_data_email,5})날 것의 값에 대해 호출하세요. 유효한 데이터는 완전히 타입이 붙은 채 통과하고, 잘못된 데이터는 실패한 정확한 경로를 지목하는 $mol_data_error를 던집니다.1const user=UserDTO(json)2// user: { readonly name: string; readonly age?: number; readonly mail: string }34// If json.mail is "</script>", it throws:5// ["mail"] </script> is not a /.+@.+/추론된 타입은 typeof UserDTO.Value로 어디서든 재사용하세요.1function greet(user:typeofUserDTO.Value){2return `Hello, ${ user.name }`3}구성 요소로는 $mol_data_string, $mol_data_number, $mol_data_integer, $mol_data_boolean, $mol_data_email, $mol_data_optional, $mol_data_nullable, $mol_data_variant(여러 타입 중 하나), $mol_data_array, $mol_data_dict, $mol_data_record가 있습니다. $mol_data_pipe는 파싱된 값을 변환으로 넣습니다——예를 들어 ISO 문자열을 $mol_time_moment로——이는 (역)직렬화도 겸합니다.
스키마를 레코드를 확장하는 클래스로 정의하세요.1export class $my_userextends$mol_schema_record({2name:$mol_schema_string,3age:$mol_schema_natural,4}){}그러면 적용하는 세 가지 방법과, 바로 쓸 수 있는 기본값을 갖게 됩니다.1const safe=$my_user.guard(input)// strict: throws on wrong data2const relaxed=$my_user.cast(input)// fills defaults instead of throwing3if ($my_user.check(input)){/* input is $my_user in here */}4const blank=$my_user.default// { name: '', age: 0 }리프 스키마로는 $mol_schema_string, $mol_schema_integer, $mol_schema_natural, $mol_schema_float, $mol_schema_boolean, $mol_schema_enum([...]), $mol_schema_pattern(/re/)가 있습니다. 이들을 $mol_schema_list(Item), $mol_schema_dict([Key,Val]), $mol_schema_maybe(S)(값, null, 또는 undefined), $mol_schema_some([...])(유니온), $mol_schema_partial({...})로 조합하세요. 다른 레코드의 필드는 ...Base.Fields로 펼치세요.1export class $my_accountextends$mol_schema_record({2...$my_user.Fields,3bio:$mol_schema_string,4}){}GitHub에서 이 페이지 편집도움이 되었나요?예아니요이전데이터 가져오기다음Giper Baza이 페이지에서두 라이브러리$mol_data$mol_schemafetch 응답 검증다음Type to search the documentation.