MCPcopy Create free account
hub / github.com/Effect-TS/effect / schema

Function schema

packages/effect/src/Config.ts:870–900  ·  view source on GitHub ↗
(codec: Schema.ConstraintCodec<T, unknown>, path?: string | ConfigProvider.Path)

Source from the content-addressed store, hash-verified

868 * @since 4.0.0
869 */
870export function schema<T>(codec: Schema.ConstraintCodec<T, unknown>, path?: string | ConfigProvider.Path): Config<T> {
871 const codecStringTree = Schema.toCodecStringTree(codec)
872 const encodedAst = SchemaAST.toEncoded(codecStringTree.ast)
873 const decodeCursor = SchemaParser.decodeUnknownEffect(
874 Schema.make<Schema.Codec<T, ConfigCursor>>(toConfigCursorAST(codecStringTree.ast))
875 )
876 const localPath = typeof path === "string" ? [path] : path ?? []
877 return make((provider, pathPrefix) => {
878 const fullPath = [...pathPrefix, ...localPath]
879 return loadCursor(provider, fullPath).pipe(
880 Effect.flatMapEager((cursor) =>
881 decodeCursor(cursor).pipe(
882 Effect.mapEager(Result.succeed),
883 Effect.catchEager((issue) => {
884 const error = new ConfigError(
885 new Schema.SchemaError(fullPath.length > 0 ? new SchemaIssue.Pointer(fullPath, issue) : issue)
886 )
887 return hasProviderInput(encodedAst, cursor.node)
888 ? Effect.fail(error)
889 : Effect.succeed(Result.fail(error))
890 })
891 )
892 ),
893 mapReasons((reason) =>
894 Cause.isDieReason(reason) && isSourceError(reason.defect)
895 ? Cause.makeFailReason(new ConfigError(reason.defect)).annotate(Cause.reasonAnnotations(reason))
896 : reason
897 )
898 )
899 })
900}
901
902const PortSchema = Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 65535 }))
903

Callers 15

StringFunction · 0.70
NonEmptyStringFunction · 0.70
NumberFunction · 0.70
FiniteFunction · 0.70
IntFunction · 0.70
LiteralFunction · 0.70
LiteralsFunction · 0.70
ArrayFunction · 0.70
RecordFunction · 0.70
BooleanFunction · 0.70
DurationFunction · 0.70
ByteSizeFunction · 0.70

Calls 11

loadCursorFunction · 0.85
hasProviderInputFunction · 0.85
mapReasonsFunction · 0.85
isSourceErrorFunction · 0.85
makeFunction · 0.70
toCodecStringTreeMethod · 0.65
makeMethod · 0.65
pipeMethod · 0.65
failMethod · 0.65
annotateMethod · 0.65
succeedMethod · 0.45

Tested by 1

assertMemoizedFunction · 0.40